1 //===-- lib/Parser/parsing.cpp --------------------------------------------===//
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 #include "flang/Parser/parsing.h"
10 #include "preprocessor.h"
11 #include "prescan.h"
12 #include "type-parsers.h"
13 #include "flang/Parser/message.h"
14 #include "flang/Parser/provenance.h"
15 #include "flang/Parser/source.h"
16 #include "llvm/Support/raw_ostream.h"
17 
18 namespace Fortran::parser {
19 
Parsing(AllCookedSources & allCooked)20 Parsing::Parsing(AllCookedSources &allCooked) : allCooked_{allCooked} {}
~Parsing()21 Parsing::~Parsing() {}
22 
Prescan(const std::string & path,Options options)23 const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
24   options_ = options;
25   AllSources &allSources{allCooked_.allSources()};
26   if (options.isModuleFile) {
27     for (const auto &path : options.searchDirectories) {
28       allSources.PushSearchPathDirectory(path);
29     }
30   }
31 
32   std::string buf;
33   llvm::raw_string_ostream fileError{buf};
34   const SourceFile *sourceFile;
35   if (path == "-") {
36     sourceFile = allSources.ReadStandardInput(fileError);
37   } else {
38     sourceFile = allSources.Open(path, fileError);
39   }
40   if (!fileError.str().empty()) {
41     ProvenanceRange range{allSources.AddCompilerInsertion(path)};
42     messages_.Say(range, "%s"_err_en_US, fileError.str());
43     return sourceFile;
44   }
45   CHECK(sourceFile);
46 
47   if (!options.isModuleFile) {
48     // For .mod files we always want to look in the search directories.
49     // For normal source files we don't push them until after the primary
50     // source file has been opened.  If foo.f is missing from the current
51     // working directory, we don't want to accidentally read another foo.f
52     // from another directory that's on the search path.
53     for (const auto &path : options.searchDirectories) {
54       allSources.PushSearchPathDirectory(path);
55     }
56   }
57 
58   Preprocessor preprocessor{allSources};
59   for (const auto &predef : options.predefinitions) {
60     if (predef.second) {
61       preprocessor.Define(predef.first, *predef.second);
62     } else {
63       preprocessor.Undefine(predef.first);
64     }
65   }
66   currentCooked_ = &allCooked_.NewCookedSource();
67   Prescanner prescanner{
68       messages_, *currentCooked_, preprocessor, options.features};
69   prescanner.set_fixedForm(options.isFixedForm)
70       .set_fixedFormColumnLimit(options.fixedFormColumns)
71       .AddCompilerDirectiveSentinel("dir$");
72   if (options.features.IsEnabled(LanguageFeature::OpenACC)) {
73     prescanner.AddCompilerDirectiveSentinel("$acc");
74   }
75   if (options.features.IsEnabled(LanguageFeature::OpenMP)) {
76     prescanner.AddCompilerDirectiveSentinel("$omp");
77     prescanner.AddCompilerDirectiveSentinel("$"); // OMP conditional line
78   }
79   ProvenanceRange range{allSources.AddIncludedFile(
80       *sourceFile, ProvenanceRange{}, options.isModuleFile)};
81   prescanner.Prescan(range);
82   if (currentCooked_->BufferedBytes() == 0 && !options.isModuleFile) {
83     // Input is empty.  Append a newline so that any warning
84     // message about nonstandard usage will have provenance.
85     currentCooked_->Put('\n', range.start());
86   }
87   currentCooked_->Marshal(allSources);
88   if (options.needProvenanceRangeToCharBlockMappings) {
89     currentCooked_->CompileProvenanceRangeToOffsetMappings(allSources);
90   }
91   return sourceFile;
92 }
93 
DumpCookedChars(llvm::raw_ostream & out) const94 void Parsing::DumpCookedChars(llvm::raw_ostream &out) const {
95   UserState userState{allCooked_, common::LanguageFeatureControl{}};
96   ParseState parseState{cooked()};
97   parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState);
98   while (std::optional<const char *> p{parseState.GetNextChar()}) {
99     out << **p;
100   }
101 }
102 
DumpProvenance(llvm::raw_ostream & out) const103 void Parsing::DumpProvenance(llvm::raw_ostream &out) const {
104   allCooked_.Dump(out);
105 }
106 
DumpParsingLog(llvm::raw_ostream & out) const107 void Parsing::DumpParsingLog(llvm::raw_ostream &out) const {
108   log_.Dump(out, allCooked_);
109 }
110 
Parse(llvm::raw_ostream & out)111 void Parsing::Parse(llvm::raw_ostream &out) {
112   UserState userState{allCooked_, options_.features};
113   userState.set_debugOutput(out)
114       .set_instrumentedParse(options_.instrumentedParse)
115       .set_log(&log_);
116   ParseState parseState{cooked()};
117   parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState);
118   parseTree_ = program.Parse(parseState);
119   CHECK(
120       !parseState.anyErrorRecovery() || parseState.messages().AnyFatalError());
121   consumedWholeFile_ = parseState.IsAtEnd();
122   messages_.Annex(std::move(parseState.messages()));
123   finalRestingPlace_ = parseState.GetLocation();
124 }
125 
ClearLog()126 void Parsing::ClearLog() { log_.clear(); }
127 
128 } // namespace Fortran::parser
129