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 
20 Parsing::Parsing(AllCookedSources &allCooked) : allCooked_{allCooked} {}
21 Parsing::~Parsing() {}
22 
23 const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
24   options_ = options;
25   AllSources &allSources{allCooked_.allSources()};
26   allSources.ClearSearchPath();
27   if (options.isModuleFile) {
28     for (const auto &path : options.searchDirectories) {
29       allSources.AppendSearchPathDirectory(path);
30     }
31   }
32 
33   std::string buf;
34   llvm::raw_string_ostream fileError{buf};
35   const SourceFile *sourceFile;
36   if (path == "-") {
37     sourceFile = allSources.ReadStandardInput(fileError);
38   } else {
39     std::optional<std::string> currentDirectory{"."};
40     sourceFile = allSources.Open(path, fileError, std::move(currentDirectory));
41   }
42   if (!fileError.str().empty()) {
43     ProvenanceRange range{allSources.AddCompilerInsertion(path)};
44     messages_.Say(range, "%s"_err_en_US, fileError.str());
45     return sourceFile;
46   }
47   CHECK(sourceFile);
48 
49   if (!options.isModuleFile) {
50     // For .mod files we always want to look in the search directories.
51     // For normal source files we don't add them until after the primary
52     // source file has been opened.  If foo.f is missing from the current
53     // working directory, we don't want to accidentally read another foo.f
54     // from another directory that's on the search path.
55     for (const auto &path : options.searchDirectories) {
56       allSources.AppendSearchPathDirectory(path);
57     }
58   }
59 
60   Preprocessor preprocessor{allSources};
61   if (!options.predefinitions.empty()) {
62     preprocessor.DefineStandardMacros();
63     for (const auto &predef : options.predefinitions) {
64       if (predef.second) {
65         preprocessor.Define(predef.first, *predef.second);
66       } else {
67         preprocessor.Undefine(predef.first);
68       }
69     }
70   }
71   currentCooked_ = &allCooked_.NewCookedSource();
72   Prescanner prescanner{
73       messages_, *currentCooked_, preprocessor, options.features};
74   prescanner.set_fixedForm(options.isFixedForm)
75       .set_fixedFormColumnLimit(options.fixedFormColumns)
76       .AddCompilerDirectiveSentinel("dir$");
77   if (options.features.IsEnabled(LanguageFeature::OpenACC)) {
78     prescanner.AddCompilerDirectiveSentinel("$acc");
79   }
80   if (options.features.IsEnabled(LanguageFeature::OpenMP)) {
81     prescanner.AddCompilerDirectiveSentinel("$omp");
82     prescanner.AddCompilerDirectiveSentinel("$"); // OMP conditional line
83   }
84   ProvenanceRange range{allSources.AddIncludedFile(
85       *sourceFile, ProvenanceRange{}, options.isModuleFile)};
86   prescanner.Prescan(range);
87   if (currentCooked_->BufferedBytes() == 0 && !options.isModuleFile) {
88     // Input is empty.  Append a newline so that any warning
89     // message about nonstandard usage will have provenance.
90     currentCooked_->Put('\n', range.start());
91   }
92   currentCooked_->Marshal(allCooked_);
93   if (options.needProvenanceRangeToCharBlockMappings) {
94     currentCooked_->CompileProvenanceRangeToOffsetMappings(allSources);
95   }
96   return sourceFile;
97 }
98 
99 void Parsing::EmitPreprocessedSource(
100     llvm::raw_ostream &out, bool lineDirectives) const {
101   const SourceFile *sourceFile{nullptr};
102   int sourceLine{0};
103   int column{1};
104   bool inDirective{false};
105   bool inContinuation{false};
106   const AllSources &allSources{allCooked().allSources()};
107   for (const char &atChar : cooked().AsCharBlock()) {
108     char ch{atChar};
109     if (ch == '\n') {
110       out << '\n'; // TODO: DOS CR-LF line ending if necessary
111       column = 1;
112       inDirective = false;
113       inContinuation = false;
114       ++sourceLine;
115     } else {
116       if (ch == '!') {
117         // Other comment markers (C, *, D) in original fixed form source
118         // input card column 1 will have been deleted or normalized to !,
119         // which signifies a comment (directive) in both source forms.
120         inDirective = true;
121       }
122       auto provenance{cooked().GetProvenanceRange(CharBlock{&atChar, 1})};
123       std::optional<SourcePosition> position{provenance
124               ? allSources.GetSourcePosition(provenance->start())
125               : std::nullopt};
126       if (lineDirectives && column == 1 && position) {
127         if (&position->file != sourceFile) {
128           out << "#line \"" << position->file.path() << "\" " << position->line
129               << '\n';
130         } else if (position->line != sourceLine) {
131           if (sourceLine < position->line &&
132               sourceLine + 10 >= position->line) {
133             // Emit a few newlines to catch up when they'll likely
134             // require fewer bytes than a #line directive would have
135             // occupied.
136             while (sourceLine++ < position->line) {
137               out << '\n';
138             }
139           } else {
140             out << "#line " << position->line << '\n';
141           }
142         }
143         sourceFile = &position->file;
144         sourceLine = position->line;
145       }
146       if (column > 72) {
147         // Wrap long lines in a portable fashion that works in both
148         // of the Fortran source forms.  The first free-form continuation
149         // marker ("&") lands in column 73, which begins the card commentary
150         // field of fixed form, and the second one is put in column 6,
151         // where it signifies fixed form line continuation.
152         // The standard Fortran fixed form column limit (72) is used
153         // for output, even if the input was parsed with a nonstandard
154         // column limit override option.
155         out << "&\n     &";
156         column = 7; // start of fixed form source field
157         ++sourceLine;
158         inContinuation = true;
159       } else if (!inDirective && ch != ' ' && (ch < '0' || ch > '9')) {
160         // Put anything other than a label or directive into the
161         // Fortran fixed form source field (columns [7:72]).
162         for (; column < 7; ++column) {
163           out << ' ';
164         }
165       }
166       if (!inContinuation && position && position->column <= 72 && ch != ' ') {
167         // Preserve original indentation
168         for (; column < position->column; ++column) {
169           out << ' ';
170         }
171       }
172       if (ch >= 'a' && ch <= 'z' && provenance && provenance->size() == 1) {
173         // Preserve original case
174         if (const char *orig{allSources.GetSource(*provenance)}) {
175           auto upper{static_cast<char>(ch + 'A' - 'a')};
176           if (*orig == upper) {
177             ch = upper;
178           }
179         }
180       }
181       out << ch;
182       ++column;
183     }
184   }
185 }
186 
187 void Parsing::DumpCookedChars(llvm::raw_ostream &out) const {
188   UserState userState{allCooked_, common::LanguageFeatureControl{}};
189   ParseState parseState{cooked()};
190   parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState);
191   while (std::optional<const char *> p{parseState.GetNextChar()}) {
192     out << **p;
193   }
194 }
195 
196 void Parsing::DumpProvenance(llvm::raw_ostream &out) const {
197   allCooked_.Dump(out);
198 }
199 
200 void Parsing::DumpParsingLog(llvm::raw_ostream &out) const {
201   log_.Dump(out, allCooked_);
202 }
203 
204 void Parsing::Parse(llvm::raw_ostream &out) {
205   UserState userState{allCooked_, options_.features};
206   userState.set_debugOutput(out)
207       .set_instrumentedParse(options_.instrumentedParse)
208       .set_log(&log_);
209   ParseState parseState{cooked()};
210   parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState);
211   parseTree_ = program.Parse(parseState);
212   CHECK(
213       !parseState.anyErrorRecovery() || parseState.messages().AnyFatalError());
214   consumedWholeFile_ = parseState.IsAtEnd();
215   messages_.Annex(std::move(parseState.messages()));
216   finalRestingPlace_ = parseState.GetLocation();
217 }
218 
219 void Parsing::ClearLog() { log_.clear(); }
220 
221 } // namespace Fortran::parser
222