2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Parser/preprocessor.h -------------------------------*- C++ -*-===//
|
2018-05-02 03:50:34 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-05-02 03:50:34 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-05-02 03:50:34 +08:00
|
|
|
|
2018-02-17 03:42:17 +08:00
|
|
|
#ifndef FORTRAN_PARSER_PREPROCESSOR_H_
|
|
|
|
#define FORTRAN_PARSER_PREPROCESSOR_H_
|
2018-01-31 03:54:31 +08:00
|
|
|
|
|
|
|
// 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
|
2018-02-14 04:50:47 +08:00
|
|
|
// performed, so that special compiler command options &/or source file name
|
2018-01-31 03:54:31 +08:00
|
|
|
// extensions for preprocessing will not be necessary.
|
|
|
|
|
2018-02-14 04:50:47 +08:00
|
|
|
#include "token-sequence.h"
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Parser/char-block.h"
|
|
|
|
#include "flang/Parser/provenance.h"
|
2018-03-21 01:59:07 +08:00
|
|
|
#include <cstddef>
|
2018-01-31 03:54:31 +08:00
|
|
|
#include <list>
|
2018-02-06 06:29:26 +08:00
|
|
|
#include <stack>
|
2018-01-31 03:54:31 +08:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-05-03 04:48:12 +08:00
|
|
|
namespace Fortran::parser {
|
2018-01-31 03:54:31 +08:00
|
|
|
|
|
|
|
class Prescanner;
|
2020-09-18 03:19:42 +08:00
|
|
|
class Preprocessor;
|
2018-01-31 03:54:31 +08:00
|
|
|
|
|
|
|
// Defines a macro
|
|
|
|
class Definition {
|
2018-02-06 04:54:36 +08:00
|
|
|
public:
|
2018-03-21 01:59:07 +08:00
|
|
|
Definition(const TokenSequence &, std::size_t firstToken, std::size_t tokens);
|
2018-01-31 03:54:31 +08:00
|
|
|
Definition(const std::vector<std::string> &argNames, const TokenSequence &,
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t firstToken, std::size_t tokens, bool isVariadic = false);
|
2018-03-24 06:14:52 +08:00
|
|
|
Definition(const std::string &predefined, AllSources &);
|
2018-01-31 03:54:31 +08:00
|
|
|
|
|
|
|
bool isFunctionLike() const { return isFunctionLike_; }
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t argumentCount() const { return argumentCount_; }
|
2018-01-31 03:54:31 +08:00
|
|
|
bool isVariadic() const { return isVariadic_; }
|
|
|
|
bool isDisabled() const { return isDisabled_; }
|
2018-02-02 07:01:23 +08:00
|
|
|
bool isPredefined() const { return isPredefined_; }
|
2018-01-31 03:54:31 +08:00
|
|
|
const TokenSequence &replacement() const { return replacement_; }
|
|
|
|
|
|
|
|
bool set_isDisabled(bool disable);
|
|
|
|
|
2020-09-18 03:19:42 +08:00
|
|
|
TokenSequence Apply(const std::vector<TokenSequence> &args, Prescanner &);
|
2018-01-31 03:54:31 +08:00
|
|
|
|
2018-02-06 04:54:36 +08:00
|
|
|
private:
|
2018-01-31 03:54:31 +08:00
|
|
|
static TokenSequence Tokenize(const std::vector<std::string> &argNames,
|
2018-03-21 01:59:07 +08:00
|
|
|
const TokenSequence &token, std::size_t firstToken, std::size_t tokens);
|
2018-01-31 03:54:31 +08:00
|
|
|
|
|
|
|
bool isFunctionLike_{false};
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t argumentCount_{0};
|
2018-01-31 03:54:31 +08:00
|
|
|
bool isVariadic_{false};
|
|
|
|
bool isDisabled_{false};
|
2018-02-02 07:01:23 +08:00
|
|
|
bool isPredefined_{false};
|
2018-01-31 03:54:31 +08:00
|
|
|
TokenSequence replacement_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Preprocessing state
|
|
|
|
class Preprocessor {
|
2018-02-06 04:54:36 +08:00
|
|
|
public:
|
2018-03-24 06:14:52 +08:00
|
|
|
explicit Preprocessor(AllSources &);
|
2018-01-31 03:54:31 +08:00
|
|
|
|
2020-09-18 03:19:42 +08:00
|
|
|
const AllSources &allSources() const { return allSources_; }
|
|
|
|
AllSources &allSources() { return allSources_; }
|
|
|
|
|
2018-03-17 07:13:49 +08:00
|
|
|
void Define(std::string macro, std::string value);
|
|
|
|
void Undefine(std::string macro);
|
2020-08-04 01:50:42 +08:00
|
|
|
bool IsNameDefined(const CharBlock &);
|
2018-03-17 07:13:49 +08:00
|
|
|
|
2018-03-24 06:14:52 +08:00
|
|
|
std::optional<TokenSequence> MacroReplacement(
|
2020-09-18 03:19:42 +08:00
|
|
|
const TokenSequence &, Prescanner &);
|
2018-01-31 03:54:31 +08:00
|
|
|
|
2018-03-17 07:13:49 +08:00
|
|
|
// Implements a preprocessor directive.
|
|
|
|
void Directive(const TokenSequence &, Prescanner *);
|
2018-01-31 03:54:31 +08:00
|
|
|
|
2018-02-06 04:54:36 +08:00
|
|
|
private:
|
2018-02-02 04:08:02 +08:00
|
|
|
enum class IsElseActive { No, Yes };
|
|
|
|
enum class CanDeadElseAppear { No, Yes };
|
2018-02-02 07:01:23 +08:00
|
|
|
|
2018-03-21 01:59:07 +08:00
|
|
|
CharBlock SaveTokenAsName(const CharBlock &);
|
2020-09-18 03:19:42 +08:00
|
|
|
TokenSequence ReplaceMacros(const TokenSequence &, Prescanner &);
|
2018-03-17 07:13:49 +08:00
|
|
|
void SkipDisabledConditionalCode(
|
2018-04-24 03:17:11 +08:00
|
|
|
const std::string &, IsElseActive, Prescanner *, ProvenanceRange);
|
2018-03-21 01:59:07 +08:00
|
|
|
bool IsIfPredicateTrue(const TokenSequence &expr, std::size_t first,
|
|
|
|
std::size_t exprTokens, Prescanner *);
|
2018-02-02 04:08:02 +08:00
|
|
|
|
2018-03-24 06:14:52 +08:00
|
|
|
AllSources &allSources_;
|
2018-01-31 03:54:31 +08:00
|
|
|
std::list<std::string> names_;
|
2018-03-21 01:59:07 +08:00
|
|
|
std::unordered_map<CharBlock, Definition> definitions_;
|
2018-02-02 04:08:02 +08:00
|
|
|
std::stack<CanDeadElseAppear> ifStack_;
|
2018-01-31 03:54:31 +08:00
|
|
|
};
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::parser
|
|
|
|
#endif // FORTRAN_PARSER_PREPROCESSOR_H_
|