llvm/flang/lib/parser/preprocessor.h
peter klausler 75b29335ab [flang] Clean out obsolete parsing code. Handle !cdir$ fixed and free in parser.
Work on scanning compiler directive lines.

Fix glitch in earlier change to parse-state.h.

Add ClassifyLine(), simplify some token sequence data lifetimes and interfaces.

Handle continued directives.  Obey !DIR$ FIXED and FREE in prescanner.

Some refactoring of TokenSequence API, then support initial directives (FIXED, FREE, IGNORE_TKR).

Fix !DIR$ IGNORE_TKR syntax, manual was wrong.

Debugging directive scanning & parsing.

Profiling-directed speed-up - do not map cooked source locations to Provenance until a Message is emitted.  Turn some non-nullable pointers into references.

Debugging.

Handle !DIR$ IVDEP too, it is in a test.

Accept directives in the execution part.

Original-commit: flang-compiler/f18@fb2ff367ec
Reviewed-on: https://github.com/flang-compiler/f18/pull/34
Tree-same-pre-rewrite: false
2018-03-28 15:04:36 -07:00

89 lines
2.9 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 "char-block.h"
#include "provenance.h"
#include "token-sequence.h"
#include <cstddef>
#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 &, std::size_t firstToken, std::size_t tokens);
Definition(const std::vector<std::string> &argNames, const TokenSequence &,
std::size_t firstToken, std::size_t tokens, bool isVariadic = false);
Definition(const std::string &predefined, AllSources &);
bool isFunctionLike() const { return isFunctionLike_; }
std::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, std::size_t firstToken, std::size_t tokens);
bool isFunctionLike_{false};
std::size_t argumentCount_{0};
bool isVariadic_{false};
bool isDisabled_{false};
bool isPredefined_{false};
TokenSequence replacement_;
};
// Preprocessing state
class Preprocessor {
public:
explicit Preprocessor(AllSources &);
void Define(std::string macro, std::string value);
void Undefine(std::string macro);
std::optional<TokenSequence> MacroReplacement(
const TokenSequence &, const Prescanner &);
// Implements a preprocessor directive.
void Directive(const TokenSequence &, Prescanner *);
private:
enum class IsElseActive { No, Yes };
enum class CanDeadElseAppear { No, Yes };
CharBlock SaveTokenAsName(const CharBlock &);
bool IsNameDefined(const CharBlock &);
TokenSequence ReplaceMacros(const TokenSequence &, const Prescanner &);
void SkipDisabledConditionalCode(
const std::string &, IsElseActive, Prescanner *, Provenance);
bool IsIfPredicateTrue(const TokenSequence &expr, std::size_t first,
std::size_t exprTokens, Prescanner *);
AllSources &allSources_;
std::list<std::string> names_;
std::unordered_map<CharBlock, Definition> definitions_;
std::stack<CanDeadElseAppear> ifStack_;
};
} // namespace parser
} // namespace Fortran
#endif // FORTRAN_PARSER_PREPROCESSOR_H_