FLANG
prescan.h
1//===-- lib/Parser/prescan.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_PRESCAN_H_
10#define FORTRAN_PARSER_PRESCAN_H_
11
12// Defines a fast Fortran source prescanning phase that implements some
13// character-level features of the language that can be inefficient to
14// support directly in a backtracking parser. This phase handles Fortran
15// line continuation, comment removal, card image margins, padding out
16// fixed form character literals on truncated card images, file
17// inclusion, and driving the Fortran source preprocessor.
18
19#include "flang/Parser/characters.h"
20#include "flang/Parser/message.h"
21#include "flang/Parser/provenance.h"
22#include "flang/Parser/token-sequence.h"
23#include "flang/Support/Fortran-features.h"
24#include <bitset>
25#include <optional>
26#include <string>
27#include <unordered_set>
28
29namespace Fortran::parser {
30
31class Messages;
32class Preprocessor;
33
34class Prescanner {
35public:
36 Prescanner(Messages &, CookedSource &, Preprocessor &,
38 Prescanner(
39 const Prescanner &, Preprocessor &, bool isNestedInIncludeDirective);
40 Prescanner(const Prescanner &) = delete;
41 Prescanner(Prescanner &&) = delete;
42
43 const AllSources &allSources() const { return allSources_; }
44 AllSources &allSources() { return allSources_; }
45 const Messages &messages() const { return messages_; }
46 Messages &messages() { return messages_; }
47 const Preprocessor &preprocessor() const { return preprocessor_; }
48 Preprocessor &preprocessor() { return preprocessor_; }
49 common::LanguageFeatureControl &features() { return features_; }
50
51 Prescanner &set_preprocessingOnly(bool yes) {
52 preprocessingOnly_ = yes;
53 return *this;
54 }
55 Prescanner &set_expandIncludeLines(bool yes) {
56 expandIncludeLines_ = yes;
57 return *this;
58 }
59 Prescanner &set_fixedForm(bool yes) {
60 inFixedForm_ = yes;
61 return *this;
62 }
63 Prescanner &set_encoding(Encoding code) {
64 encoding_ = code;
65 return *this;
66 }
67 Prescanner &set_fixedFormColumnLimit(int limit) {
68 fixedFormColumnLimit_ = limit;
69 return *this;
70 }
71
72 Prescanner &AddCompilerDirectiveSentinel(const std::string &);
73
74 void Prescan(ProvenanceRange);
75 void Statement();
76 void NextLine();
77
78 // Callbacks for use by Preprocessor.
79 bool IsAtEnd() const { return nextLine_ >= limit_; }
80 bool IsNextLinePreprocessorDirective() const;
81 TokenSequence TokenizePreprocessorDirective();
82 Provenance GetCurrentProvenance() const { return GetProvenance(at_); }
83
84 const char *IsCompilerDirectiveSentinel(const char *, std::size_t) const;
85 const char *IsCompilerDirectiveSentinel(CharBlock) const;
86 // 'first' is the sentinel, 'second' is beginning of payload
87 std::optional<std::pair<const char *, const char *>>
88 IsCompilerDirectiveSentinel(const char *p) const;
89
90 template <typename... A> Message &Say(A &&...a) {
91 return messages_.Say(std::forward<A>(a)...);
92 }
93
94 template <typename... A>
95 Message *Warn(common::UsageWarning warning, A &&...a) {
96 return messages_.Warn(false, features_, warning, std::forward<A>(a)...);
97 }
98 template <typename... A>
99 Message *Warn(common::LanguageFeature feature, A &&...a) {
100 return messages_.Warn(false, features_, feature, std::forward<A>(a)...);
101 }
102
103private:
104 struct LineClassification {
105 enum class Kind {
106 Comment,
107 ConditionalCompilationDirective,
108 IncludeDirective, // #include
109 DefinitionDirective, // #define & #undef
110 PreprocessorDirective,
111 IncludeLine, // Fortran INCLUDE
113 Source
114 };
115 LineClassification(Kind k, std::size_t po = 0, const char *s = nullptr)
116 : kind{k}, payloadOffset{po}, sentinel{s} {}
117 LineClassification(LineClassification &&) = default;
118 LineClassification &operator=(LineClassification &&) = default;
119 Kind kind;
120 std::size_t payloadOffset; // byte offset of content
121 const char *sentinel; // if it's a compiler directive
122 };
123
124 void BeginSourceLine(const char *at) {
125 at_ = at;
126 column_ = 1;
127 tabInCurrentLine_ = false;
128 }
129
130 void BeginSourceLineAndAdvance() {
131 BeginSourceLine(nextLine_);
132 NextLine();
133 }
134
135 void BeginStatementAndAdvance() {
136 BeginSourceLineAndAdvance();
137 slashInCurrentStatement_ = false;
138 preventHollerith_ = false;
139 parenthesisNesting_ = 0;
140 continuationLines_ = 0;
141 isPossibleMacroCall_ = false;
142 disableSourceContinuation_ = false;
143 }
144
145 Provenance GetProvenance(const char *sourceChar) const {
146 return startProvenance_ + (sourceChar - start_);
147 }
148
149 ProvenanceRange GetProvenanceRange(
150 const char *first, const char *afterLast) const {
151 std::size_t bytes = afterLast - first;
152 return {startProvenance_ + (first - start_), bytes};
153 }
154
155 void EmitChar(TokenSequence &tokens, char ch) {
156 tokens.PutNextTokenChar(ch, GetCurrentProvenance());
157 }
158
159 void EmitInsertedChar(TokenSequence &tokens, char ch) {
160 Provenance provenance{allSources_.CompilerInsertionProvenance(ch)};
161 tokens.PutNextTokenChar(ch, provenance);
162 }
163
164 char EmitCharAndAdvance(TokenSequence &tokens, char ch) {
165 EmitChar(tokens, ch);
166 NextChar();
167 return *at_;
168 }
169
170 bool InCompilerDirective() const { return directiveSentinel_ != nullptr; }
171 bool InOpenMPConditionalLine() const {
172 return directiveSentinel_ && directiveSentinel_[0] == '$' &&
173 !directiveSentinel_[1];
174 ;
175 }
176 bool InFixedFormSource() const {
177 return inFixedForm_ && !inPreprocessorDirective_ && !InCompilerDirective();
178 }
179
180 bool IsCComment(const char *p) const {
181 return p[0] == '/' && p[1] == '*' &&
182 (inPreprocessorDirective_ ||
183 (!inCharLiteral_ &&
184 features_.IsEnabled(
185 common::LanguageFeature::ClassicCComments)));
186 }
187
188 void CheckAndEmitLine(TokenSequence &, Provenance newlineProvenance);
189 void LabelField(TokenSequence &);
190 void EnforceStupidEndStatementRules(const TokenSequence &);
191 void SkipToEndOfLine();
192 bool MustSkipToEndOfLine() const;
193 void NextChar();
194 // True when input flowed to a continuation line
195 bool SkipToNextSignificantCharacter();
196 void SkipCComments();
197 void SkipSpaces();
198 static const char *SkipWhiteSpace(const char *);
199 const char *SkipWhiteSpaceIncludingEmptyMacros(const char *) const;
200 const char *SkipWhiteSpaceAndCComments(const char *) const;
201 const char *SkipCComment(const char *) const;
202 bool NextToken(TokenSequence &);
203 bool HandleExponent(TokenSequence &);
204 bool HandleKindSuffix(TokenSequence &);
205 bool HandleExponentAndOrKindSuffix(TokenSequence &);
206 void QuotedCharacterLiteral(TokenSequence &, const char *start);
207 void Hollerith(TokenSequence &, int count, const char *start);
208 bool PadOutCharacterLiteral(TokenSequence &);
209 bool SkipCommentLine(bool afterAmpersand);
210 bool IsFixedFormCommentLine(const char *) const;
211 const char *IsFreeFormComment(const char *) const;
212 std::optional<std::size_t> IsIncludeLine(const char *) const;
213 void FortranInclude(const char *quote);
214 const char *IsPreprocessorDirectiveLine(const char *) const;
215 const char *FixedFormContinuationLine(bool atNewline);
216 const char *FreeFormContinuationLine(bool ampersand);
217 bool IsImplicitContinuation() const;
218 bool FixedFormContinuation(bool atNewline);
219 bool FreeFormContinuation();
220 bool Continuation(bool mightNeedFixedFormSpace);
221 std::optional<LineClassification> IsFixedFormCompilerDirectiveLine(
222 const char *) const;
223 std::optional<LineClassification> IsFreeFormCompilerDirectiveLine(
224 const char *) const;
225 LineClassification ClassifyLine(const char *) const;
226 LineClassification ClassifyLine(
227 TokenSequence &, Provenance newlineProvenance) const;
228 void SourceFormChange(std::string &&);
229 bool CompilerDirectiveContinuation(TokenSequence &, const char *sentinel);
230 bool SourceLineContinuation(TokenSequence &);
231
232 Messages &messages_;
233 CookedSource &cooked_;
234 Preprocessor &preprocessor_;
235 AllSources &allSources_;
237 bool preprocessingOnly_{false};
238 bool expandIncludeLines_{true};
239 bool isNestedInIncludeDirective_{false};
240 bool backslashFreeFormContinuation_{false};
241 bool inFixedForm_{false};
242 int fixedFormColumnLimit_{72};
243 Encoding encoding_{Encoding::UTF_8};
244 int parenthesisNesting_{0};
245 int prescannerNesting_{0};
246 int continuationLines_{0};
247 bool isPossibleMacroCall_{false};
248 bool afterPreprocessingDirective_{false};
249 bool disableSourceContinuation_{false};
250
251 Provenance startProvenance_;
252 const char *start_{nullptr}; // beginning of current source file content
253 const char *limit_{nullptr}; // first address after end of current source
254 const char *nextLine_{nullptr}; // next line to process; <= limit_
255 const char *directiveSentinel_{nullptr}; // current compiler directive
256
257 // These data members are state for processing the source line containing
258 // "at_", which goes to up to the newline character before "nextLine_".
259 const char *at_{nullptr}; // next character to process; < nextLine_
260 int column_{1}; // card image column position of next character
261 bool tabInCurrentLine_{false};
262 bool slashInCurrentStatement_{false};
263 bool preventHollerith_{false}; // CHARACTER*4HIMOM not Hollerith
264 bool inCharLiteral_{false};
265 bool continuationInCharLiteral_{false};
266 bool inPreprocessorDirective_{false};
267
268 // True after processing a continuation that can't be allowed
269 // to appear in the middle of an identifier token, but is fixed form,
270 // or is free form and doesn't have a space character handy to use as
271 // a separator when:
272 // a) (standard) doesn't begin with a leading '&' on the continuation
273 // line, but has a non-blank in column 1, or
274 // b) (extension) does have a leading '&', but didn't have one
275 // on the continued line.
276 bool brokenToken_{false};
277
278 // When a free form continuation marker (&) appears at the end of a line
279 // before a INCLUDE or #include, we delete it and omit the newline, so
280 // that the first line of the included file is truly a continuation of
281 // the line before. Also used when the & appears at the end of the last
282 // line in an include file.
283 bool omitNewline_{false};
284 bool skipLeadingAmpersand_{false};
285
286 const std::size_t firstCookedCharacterOffset_{cooked_.BufferedBytes()};
287
288 const Provenance spaceProvenance_{
289 allSources_.CompilerInsertionProvenance(' ')};
290 const Provenance backslashProvenance_{
291 allSources_.CompilerInsertionProvenance('\\')};
292
293 // To avoid probing the set of active compiler directive sentinel strings
294 // on every comment line, they're checked first with a cheap Bloom filter.
295 static const int prime1{1019}, prime2{1021};
296 std::bitset<prime2> compilerDirectiveBloomFilter_; // 128 bytes
297 std::unordered_set<std::string> compilerDirectiveSentinels_;
298};
299} // namespace Fortran::parser
300#endif // FORTRAN_PARSER_PRESCAN_H_
Definition Fortran-features.h:90
Definition provenance.h:139
Definition char-block.h:28
Definition provenance.h:233
Definition message.h:188
Definition message.h:320
Definition preprocessor.h:73
Definition provenance.h:52
Definition token-sequence.h:35
Definition check-expression.h:19
Definition parse-tree.h:3358