FLANG
preprocessor.h
1//===-- lib/Parser/preprocessor.h -------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_PARSER_PREPROCESSOR_H_
10#define FORTRAN_PARSER_PREPROCESSOR_H_
11
12// A Fortran-aware preprocessing module used by the prescanner to implement
13// preprocessing directives and macro replacement. Intended to be efficient
14// enough to always run on all source files even when no preprocessing is
15// performed, so that special compiler command options &/or source file name
16// extensions for preprocessing will not be necessary.
17
18#include "flang/Parser/char-block.h"
19#include "flang/Parser/provenance.h"
20#include "flang/Parser/token-sequence.h"
21#include "llvm/Support/raw_ostream.h"
22#include <cstddef>
23#include <list>
24#include <stack>
25#include <string>
26#include <unordered_map>
27#include <vector>
28
29namespace Fortran::parser {
30
31class Prescanner;
32class Preprocessor;
33
34// Defines a macro
35class Definition {
36public:
37 Definition(const TokenSequence &, std::size_t firstToken, std::size_t tokens);
38 Definition(const std::vector<std::string> &argNames, const TokenSequence &,
39 std::size_t firstToken, std::size_t tokens, bool isVariadic = false);
40 Definition(const std::string &predefined, AllSources &);
41 Definition(const TokenSequence &predefined);
42
43 bool isFunctionLike() const { return isFunctionLike_; }
44 std::size_t argumentCount() const { return argNames_.size(); }
45 bool isVariadic() const { return isVariadic_; }
46 bool isDisabled() const { return isDisabled_; }
47 bool isPredefined() const { return isPredefined_; }
48 const TokenSequence &replacement() const { return replacement_; }
49
50 bool set_isDisabled(bool disable);
51
52 TokenSequence Apply(const std::vector<TokenSequence> &args, Prescanner &,
53 bool inIfExpression = false);
54
55 void Print(llvm::raw_ostream &out, const char *macroName = "") const;
56
57private:
58 static TokenSequence Tokenize(const std::vector<std::string> &argNames,
59 const TokenSequence &token, std::size_t firstToken, std::size_t tokens);
60 // For a given token, return the index of the argument to which the token
61 // corresponds, or `argumentCount` if the token does not correspond to any
62 // argument.
63 std::size_t GetArgumentIndex(const CharBlock &token) const;
64
65 bool isFunctionLike_{false};
66 bool isVariadic_{false};
67 bool isDisabled_{false};
68 bool isPredefined_{false};
69 std::vector<std::string> argNames_;
70 TokenSequence replacement_;
71};
72
73// Preprocessing state
74class Preprocessor {
75public:
76 explicit Preprocessor(AllSources &);
77
78 const AllSources &allSources() const { return allSources_; }
79 AllSources &allSources() { return allSources_; }
80
81 void DefineStandardMacros();
82 void Define(const std::string &macro, const std::string &value);
83 void Undefine(std::string macro);
84 bool IsNameDefined(const CharBlock &);
85 bool IsNameDefinedEmpty(const CharBlock &);
86 bool IsFunctionLikeDefinition(const CharBlock &);
87 bool AnyDefinitions() const { return !definitions_.empty(); }
88 bool InConditional() const { return !ifStack_.empty(); }
89
90 // When called with partialFunctionLikeMacro not null, MacroReplacement()
91 // and ReplaceMacros() handle an unclosed function-like macro reference
92 // by terminating macro replacement at the name of the FLM and returning
93 // its index in the result. This allows the recursive call sites in
94 // MacroReplacement to append any remaining tokens in their inputs to
95 // that result and try again. All other Fortran preprocessors share this
96 // behavior.
97 std::optional<TokenSequence> MacroReplacement(const TokenSequence &,
98 Prescanner &,
99 std::optional<std::size_t> *partialFunctionLikeMacro = nullptr,
100 bool inIfExpression = false);
101
102 // Implements a preprocessor directive.
103 void Directive(const TokenSequence &, Prescanner &);
104
105 void PrintMacros(llvm::raw_ostream &out) const;
106
107private:
108 enum class IsElseActive { No, Yes };
109 enum class CanDeadElseAppear { No, Yes };
110
111 CharBlock SaveTokenAsName(const CharBlock &);
112 TokenSequence ReplaceMacros(const TokenSequence &, Prescanner &,
113 std::optional<std::size_t> *partialFunctionLikeMacro = nullptr,
114 bool inIfExpression = false);
115 void SkipDisabledConditionalCode(
116 const std::string &, IsElseActive, Prescanner &, ProvenanceRange);
117 bool IsIfPredicateTrue(const TokenSequence &expr, std::size_t first,
118 std::size_t exprTokens, Prescanner &);
119 void LineDirective(const TokenSequence &, std::size_t, Prescanner &);
120 TokenSequence TokenizeMacroBody(const std::string &);
121
122 AllSources &allSources_;
123 std::list<std::string> names_;
124 std::unordered_map<CharBlock, Definition> definitions_;
125 std::stack<CanDeadElseAppear> ifStack_;
126
127 unsigned int counterVal_{0};
128};
129} // namespace Fortran::parser
130#endif // FORTRAN_PARSER_PREPROCESSOR_H_
Definition provenance.h:139
Definition char-block.h:28
Definition preprocessor.h:74
Definition prescan.h:34
Definition token-sequence.h:35
Definition check-expression.h:19