xref: /llvm-project/flang/include/flang/Parser/parsing.h (revision 4dfed691a9f846b1ff773e28b878404b78559890)
1 //===-- include/flang/Parser/parsing.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_PARSING_H_
10 #define FORTRAN_PARSER_PARSING_H_
11 
12 #include "characters.h"
13 #include "instrumented-parser.h"
14 #include "message.h"
15 #include "parse-tree.h"
16 #include "provenance.h"
17 #include "flang/Common/Fortran-features.h"
18 #include "flang/Parser/preprocessor.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <optional>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 namespace Fortran::parser {
26 
27 struct Options {
28   Options() {}
29 
30   using Predefinition = std::pair<std::string, std::optional<std::string>>;
31 
32   bool isFixedForm{false};
33   int fixedFormColumns{72};
34   common::LanguageFeatureControl features;
35   std::vector<std::string> searchDirectories;
36   std::vector<std::string> intrinsicModuleDirectories;
37   std::vector<Predefinition> predefinitions;
38   bool instrumentedParse{false};
39   bool isModuleFile{false};
40   bool needProvenanceRangeToCharBlockMappings{false};
41   Fortran::parser::Encoding encoding{Fortran::parser::Encoding::UTF_8};
42   bool prescanAndReformat{false}; // -E
43   bool expandIncludeLinesInPreprocessedOutput{true};
44   bool showColors{false};
45 };
46 
47 class Parsing {
48 public:
49   explicit Parsing(AllCookedSources &);
50   ~Parsing();
51 
52   bool consumedWholeFile() const { return consumedWholeFile_; }
53   const char *finalRestingPlace() const { return finalRestingPlace_; }
54   AllCookedSources &allCooked() { return allCooked_; }
55   const AllCookedSources &allCooked() const { return allCooked_; }
56   Messages &messages() { return messages_; }
57   std::optional<Program> &parseTree() { return parseTree_; }
58 
59   const CookedSource &cooked() const { return DEREF(currentCooked_); }
60 
61   const SourceFile *Prescan(const std::string &path, Options);
62   void EmitPreprocessedSource(
63       llvm::raw_ostream &, bool lineDirectives = true) const;
64   void EmitPreprocessorMacros(llvm::raw_ostream &) const;
65   void DumpCookedChars(llvm::raw_ostream &) const;
66   void DumpProvenance(llvm::raw_ostream &) const;
67   void DumpParsingLog(llvm::raw_ostream &) const;
68   void Parse(llvm::raw_ostream &debugOutput);
69   void ClearLog();
70 
71   void EmitMessage(llvm::raw_ostream &o, const char *at,
72       const std::string &message, const std::string &prefix,
73       llvm::raw_ostream::Colors color = llvm::raw_ostream::SAVEDCOLOR,
74       bool echoSourceLine = false) const {
75     allCooked_.allSources().EmitMessage(o,
76         allCooked_.GetProvenanceRange(CharBlock(at)), message, prefix, color,
77         echoSourceLine);
78   }
79 
80 private:
81   Options options_;
82   AllCookedSources &allCooked_;
83   CookedSource *currentCooked_{nullptr};
84   Messages messages_;
85   bool consumedWholeFile_{false};
86   const char *finalRestingPlace_{nullptr};
87   std::optional<Program> parseTree_;
88   ParsingLog log_;
89   Preprocessor preprocessor_{allCooked_.allSources()};
90 };
91 } // namespace Fortran::parser
92 #endif // FORTRAN_PARSER_PARSING_H_
93