f4a85288db
Add characters.h to consolidate all per-character functions and reduce dependence on <cctype>. Add omitted DIRECT= and UNFORMATTED= specifiers to INQUIRE statement. Add Interval<>, rebase ProvenanceRange upon it, rebase CharPointerWithLength and rename it ContiguousChars. Reformat C++. Original-commit: flang-compiler/f18@78c73c62ba Reviewed-on: https://github.com/flang-compiler/f18/pull/19 Tree-same-pre-rewrite: false
88 lines
3 KiB
C++
88 lines
3 KiB
C++
#ifndef FORTRAN_PARSER_PREPROCESSOR_H_
|
|
#define FORTRAN_PARSER_PREPROCESSOR_H_
|
|
|
|
// A Fortran-aware preprocessing module used by the prescanner to implement
|
|
// preprocessing directives and macro replacement. Intended to be efficient
|
|
// enough to always run on all source files even when no preprocessing is
|
|
// performed, so that special compiler command options &/or source file name
|
|
// extensions for preprocessing will not be necessary.
|
|
|
|
#include "provenance.h"
|
|
#include "token-sequence.h"
|
|
#include <list>
|
|
#include <stack>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
namespace Fortran {
|
|
namespace parser {
|
|
|
|
class Prescanner;
|
|
|
|
// Defines a macro
|
|
class Definition {
|
|
public:
|
|
Definition(const TokenSequence &, size_t firstToken, size_t tokens);
|
|
Definition(const std::vector<std::string> &argNames, const TokenSequence &,
|
|
size_t firstToken, size_t tokens, bool isVariadic = false);
|
|
Definition(const std::string &predefined, AllSources *);
|
|
|
|
bool isFunctionLike() const { return isFunctionLike_; }
|
|
size_t argumentCount() const { return argumentCount_; }
|
|
bool isVariadic() const { return isVariadic_; }
|
|
bool isDisabled() const { return isDisabled_; }
|
|
bool isPredefined() const { return isPredefined_; }
|
|
const TokenSequence &replacement() const { return replacement_; }
|
|
|
|
bool set_isDisabled(bool disable);
|
|
|
|
TokenSequence Apply(const std::vector<TokenSequence> &args, AllSources *);
|
|
|
|
private:
|
|
static TokenSequence Tokenize(const std::vector<std::string> &argNames,
|
|
const TokenSequence &token, size_t firstToken, size_t tokens);
|
|
|
|
bool isFunctionLike_{false};
|
|
size_t argumentCount_{0};
|
|
bool isVariadic_{false};
|
|
bool isDisabled_{false};
|
|
bool isPredefined_{false};
|
|
TokenSequence replacement_;
|
|
};
|
|
|
|
// Preprocessing state
|
|
class Preprocessor {
|
|
public:
|
|
explicit Preprocessor(AllSources *);
|
|
|
|
// When the input contains macros to be replaced, the new token sequence
|
|
// is appended to the output and the returned value is true. When
|
|
// no macro replacement is necessary, the output is unmodified and the
|
|
// return value is false.
|
|
bool MacroReplacement(
|
|
const TokenSequence &, const Prescanner &, TokenSequence *);
|
|
|
|
// Implements a preprocessor directive; returns true when no fatal error.
|
|
bool Directive(const TokenSequence &, Prescanner *);
|
|
|
|
private:
|
|
enum class IsElseActive { No, Yes };
|
|
enum class CanDeadElseAppear { No, Yes };
|
|
|
|
ContiguousChars SaveTokenAsName(const ContiguousChars &);
|
|
bool IsNameDefined(const ContiguousChars &);
|
|
TokenSequence ReplaceMacros(const TokenSequence &, const Prescanner &);
|
|
bool SkipDisabledConditionalCode(
|
|
const std::string &, IsElseActive, Prescanner *);
|
|
bool IsIfPredicateTrue(
|
|
const TokenSequence &expr, size_t first, size_t exprTokens, Prescanner *);
|
|
|
|
AllSources *allSources_;
|
|
std::list<std::string> names_;
|
|
std::unordered_map<ContiguousChars, Definition> definitions_;
|
|
std::stack<CanDeadElseAppear> ifStack_;
|
|
};
|
|
} // namespace parser
|
|
} // namespace Fortran
|
|
#endif // FORTRAN_PARSER_PREPROCESSOR_H_
|