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