summaryrefslogtreecommitdiff
path: root/include/flang/Parse/Parser.h
blob: f94b8b83ef95bfa13c4f095d3aececdf73849e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//===-- Parser.h - Fortran Parser Interface ---------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// The Fortran parser interface.
//
//===----------------------------------------------------------------------===//

#ifndef FLANG_PARSER_PARSER_H__
#define FLANG_PARSER_PARSER_H__

#include "flang/AST/ASTContext.h"
#include "flang/AST/Stmt.h"
#include "flang/Basic/Diagnostic.h"
#include "flang/Basic/IdentifierTable.h"
#include "flang/Basic/LangOptions.h"
#include "flang/Basic/TokenKinds.h"
#include "flang/Parse/FixedForm.h"
#include "flang/Parse/Lexer.h"
#include "flang/Sema/DeclSpec.h"
#include "flang/Sema/Ownership.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/Twine.h"
#include <vector>

namespace llvm {
  class SourceMgr;
} // end namespace llvm

namespace flang {

class Action;
class VarExpr;
class ConstantExpr;
class DeclGroupRef;
class Expr;
class ArraySpec;
class Parser;
class Selector;
class Sema;
class UnitSpec;
class FormatSpec;

/// PrettyStackTraceParserEntry - If a crash happens while the parser is active,
/// an entry is printed for it.
class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
  const Parser &FP;
public:
  PrettyStackTraceParserEntry(const Parser &fp) : FP(fp) {}
  virtual void print(llvm::raw_ostream &OS) const;
};

/// Parser - This implements a parser for the Fortran family of languages. After
/// parsing units of the grammar, productions are invoked to handle whatever has
/// been read.
class Parser {
public:
  enum RetTy {
    Success,                //< The construct was parsed successfully
    WrongConstruct,         //< The construct we wanted to parse wasn't present
    Error                   //< There was an error parsing
  };
private:

  Lexer TheLexer;
  LangOptions Features;
  PrettyStackTraceParserEntry CrashInfo;
  llvm::SourceMgr &SrcMgr;

  /// LexerBufferContext - This is a stack of lexing contexts
  /// for files lower in the include stack
  std::vector<const char*> LexerBufferContext;

  /// This is the current buffer index we're lexing from as managed by the
  /// SourceMgr object.
  int CurBuffer;

  ASTContext &Context;

  /// Diag - Diagnostics for parsing errors.
  DiagnosticsEngine &Diag;

  /// Actions - These are the callbacks we invoke as we parse various constructs
  /// in the file. 
  Sema &Actions;

  /// FirstLoc - The location of the first token in the given statement.
  SourceLocation LocFirstStmtToken;

  /// Tok - The current token we are parsing. All parsing methods assume that
  /// this is valid.
  Token Tok;

  /// NextTok - The next token so that we can do one level of lookahead.
  Token NextTok;

  /// StmtLabel - If set, this is the statement label for the statement.
  Expr *StmtLabel;

  /// ConstructName - If set, this is the construct name for the construct.
  ConstructName StmtConstructName;

  unsigned short ParenCount, ParenSlashCount,
                 BracketCount, BraceCount;

  fixedForm::CommonAmbiguities FixedFormAmbiguities;

  // PrevTokLocation - The location of the token we previously consumed. This
  // token is used for diagnostics where we expected to see a token following
  // another token.
  SourceLocation PrevTokLocation;

  /// Returns the end of location of the previous token.
  SourceLocation PrevTokLocEnd;

  /// DontResolveIdentifiers - if set, the identifier tokens create
  /// an UnresolvedIdentifierExpr, instead of resolving the identifier.
  /// As a of this result Subscript, Substring and function call
  /// expressions aren't parsed.
  bool DontResolveIdentifiers;

  /// DontResolveIdentifiersInSubExpressions - if set,
  /// ParsePrimaryExpression will set the DontResolveIdentifiers
  /// to true when parsing any subexpressions like array subscripts,
  /// etc.
  bool DontResolveIdentifiersInSubExpressions;

  /// LexFORMATTokens - if set,
  /// The lexer will lex the format descriptor tokens instead
  /// of normal tokens.
  bool LexFORMATTokens;

  /// Identifiers - This is mapping/lookup information for all identifiers in
  /// the program, including program keywords.
  mutable IdentifierTable Identifiers;

  /// PrevStmtWasSelectCase - if set, this indicates that the last statement
  /// was select case and a case or an end select statement is expected.
  bool PrevStmtWasSelectCase;

private:

  /// getIdentifierInfo - Return information about the specified identifier
  /// token.
  IdentifierInfo *getIdentifierInfo(std::string &Name) const {
    return &Identifiers.get(Name);
  }

  /// Returns the maximum location of the current token
  SourceLocation getMaxLocationOfCurrentToken() {
    return SourceLocation::getFromPointer(Tok.getLocation().getPointer() +
                                          Tok.getLength());
  }

  /// CleanLiteral - Cleans up a literal if it needs cleaning. It removes the
  /// continuation contexts and comments. Cleaning a dirty literal is SLOW!
  void CleanLiteral(Token T, std::string &NameStr);

  bool EnterIncludeFile(const std::string &Filename);
  bool LeaveIncludeFile();

  /// IsNextToken - Returns true if the next token is a token kind
  bool IsNextToken(tok::TokenKind TokKind);

  void Lex();
  void ClassifyToken(Token &T);
public:

  typedef OpaquePtr<DeclGroupRef> DeclGroupPtrTy;

  typedef flang::ExprResult ExprResult;
  typedef flang::StmtResult StmtResult;
  typedef flang::FormatItemResult FormatItemResult;

  bool isaIdentifier(const llvm::StringRef &ID) const {
    return Identifiers.isaIdentifier(ID);
  }
  bool isaKeyword(const llvm::StringRef &KW) const {
    return Identifiers.isaKeyword(KW);
  }

  Parser(llvm::SourceMgr &SrcMgr, const LangOptions &Opts,
         DiagnosticsEngine &D, Sema &actions);

  llvm::SourceMgr &getSourceManager() { return SrcMgr; }

  const Token &getCurToken() const { return Tok; }
  const Lexer &getLexer() const { return TheLexer; }
  Lexer &getLexer() { return TheLexer; }

  bool ParseProgramUnits();

  ExprResult ExprError() { return ExprResult(true); }
  StmtResult StmtError() { return StmtResult(true); }

private:

  /// getExpectedLoc - returns the location
  /// for the expected token.
  SourceLocation getExpectedLoc() const;

  /// getExpectedLocForFixIt - returns the location
  /// in which the expected token should be inserted in.
  inline SourceLocation getExpectedLocForFixIt() const {
    return PrevTokLocEnd;
  }

  /// getTokenRange - returns the range of the token at
  /// the given location.
  SourceRange getTokenRange(SourceLocation Loc) const;

  /// getTokenRange - returns the range of the current token.
  SourceRange getTokenRange() const;

  //===--------------------------------------------------------------------===//
  // Low-Level token peeking and consumption methods.
  //

  /// isTokenParen - Return true if the cur token is '(' or ')'.
  bool isTokenParen() const {
    return Tok.getKind() == tok::l_paren || Tok.getKind() == tok::r_paren;
  }

  /// isTokenParenSlash - Return true if the cur token is '(/' or '/)'.
  bool isTokenParenSlash() const {
    return Tok.getKind() == tok::l_parenslash || Tok.getKind() == tok::slashr_paren;
  }

  /// isTokenIdentifier - Return true if the cur token is a usable
  /// identifier (this can also be a keyword).
  bool isTokenIdentifier() const {
    if (Tok.is(tok::identifier) ||
        (Tok.getIdentifierInfo() &&
          isaKeyword(Tok.getIdentifierInfo()->getName()))
        )
      return true;
    return false;
  }

  /// ConsumeToken - Consume the current token and lex the next one. This
  /// returns the location of the consumed token.
  SourceLocation ConsumeToken() {
    auto Loc = Tok.getLocation();
    Lex();
    return Loc;
  }

  /// ConsumeAnyToken - Dispatch to the right Consume* method based on the
  /// current token type.  This should only be used in cases where the type of
  /// the token really isn't known, e.g. in error recovery.
  SourceLocation ConsumeAnyToken(bool ConsumeCodeCompletionTok = false) {
    if (isTokenParen())
      return ConsumeParen();
    else if (isTokenParenSlash())
      return ConsumeParenSlash();
    else return ConsumeToken();
  }

  /// ConsumeParen - This consume method keeps the paren count up-to-date.
  ///
  SourceLocation ConsumeParen() {
    assert(isTokenParen() && "wrong consume method");
    auto Loc = Tok.getLocation();
    if (Tok.getKind() == tok::l_paren)
      ++ParenCount;
    else if (ParenCount)
      --ParenCount;       // Don't let unbalanced )'s drive the count negative.
    Lex();
    return Loc;
  }

  /// ConsumeParenSlash - This consume method keeps the paren slash count up-to-date.
  ///
  SourceLocation ConsumeParenSlash() {
    assert(isTokenParenSlash() && "wrong consume method");
    auto Loc = Tok.getLocation();
    if (Tok.getKind() == tok::l_parenslash)
      ++ParenSlashCount;
    else if (ParenSlashCount)
      --ParenSlashCount;       // Don't let unbalanced /)'s drive the count negative.
    Lex();
    return Loc;
  }

  /// IsPresent - Returns true if the next token is Tok.
  bool IsPresent(tok::TokenKind TokKind, bool InSameStatement = true);

  /// ConsumeIfPresent - Consumes the token if it's present. Return 'true' if it was
  /// delicious.
  bool ConsumeIfPresent(tok::TokenKind OptionalTok, bool InSameStatement = true);

  /// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
  /// input.  If so, it is consumed and true is returned.
  ///
  /// If the input is malformed, this emits the specified diagnostic.  Next, if
  /// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, false is
  /// returned.
  ///
  /// If the diagnostic isn't specified, the appropriate diagnostic for
  /// the given token type is emitted.
  bool ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned Diag = 0,
                        const char *DiagMsg = "",
                        tok::TokenKind SkipToTok = tok::unknown,
                        bool InSameStatement = true);

  bool ExpectAndConsumeFixedFormAmbiguous(tok::TokenKind ExpectedTok, unsigned Diag = 0,
                                          const char *DiagMsg = "");

  /// ExpectStatementEnd - The parser expects that the next token is in the
  /// next statement.
  bool ExpectStatementEnd();

  /// SkipUntil - Read tokens until we get to the specified token, then consume
  /// it.  Because we cannot guarantee that the
  /// token will ever occur, this skips to the next token, or to some likely
  /// good stopping point.  If StopAtNextStatement is true,
  /// skipping will stop when the next statement is reached.
  ///
  /// If SkipUntil finds the specified token, it returns true, otherwise it
  /// returns false.
  bool SkipUntil(tok::TokenKind T, bool StopAtNextStatement = true,
                 bool DontConsume = false) {
    return SkipUntil(llvm::makeArrayRef(T), StopAtNextStatement,
                     DontConsume);
  }
  bool SkipUntil(tok::TokenKind T1, tok::TokenKind T2, bool StopAtNextStatement = true,
                 bool DontConsume = false) {
    tok::TokenKind TokArray[] = {T1, T2};
    return SkipUntil(TokArray, StopAtNextStatement, DontConsume);
  }
  bool SkipUntil(tok::TokenKind T1, tok::TokenKind T2, tok::TokenKind T3,
                 bool StopAtNextStatement = true, bool DontConsume = false) {
    tok::TokenKind TokArray[] = {T1, T2, T3};
    return SkipUntil(TokArray, StopAtNextStatement, DontConsume);
  }
  bool SkipUntil(ArrayRef<tok::TokenKind> Toks, bool StopAtNextStatement = true,
                 bool DontConsume = false);

  /// SkipUntilNextStatement - Consume tokens until the next statement is reached.
  /// Returns true.
  bool SkipUntilNextStatement();

  /// Certain fixed-form are ambigous, and might need to be reparsed
  void StartStatementReparse(SourceLocation Where = SourceLocation());

  StmtResult ReparseAmbiguousAssignmentStatement(SourceLocation Where = SourceLocation());

  void ReLexAmbiguousIdentifier(const fixedForm::KeywordMatcher &Matcher);

  // High-level parsing methods.
  bool ParseInclude();
  bool ParseProgramUnit();
  bool ParseMainProgram();
  bool ParseExternalSubprogram();
  bool ParseExternalSubprogram(DeclSpec &ReturnType, int Attr);
  bool ParseTypedExternalSubprogram(int Attr);
  bool ParseRecursiveExternalSubprogram();
  bool ParseExecutableSubprogramBody(tok::TokenKind EndKw);
  bool ParseModule();
  bool ParseBlockData();

  bool ParseRESULT();

  StmtResult ParseStatementFunction();

  bool ParseSpecificationPart();
  bool ParseImplicitPartList();
  StmtResult ParseImplicitPart();
  bool ParseExecutionPart();

  bool ParseDeclarationConstructList();
  bool ParseDeclarationConstruct();
  bool ParseForAllConstruct();
  void CheckStmtOrder(SourceLocation Loc, StmtResult SR);
  StmtResult ParseExecutableConstruct();

  bool ParseTypeDeclarationStmt(SmallVectorImpl<DeclResult> &Decls);

  /// ParseDeclarationTypeSpec - returns true if a parsing error
  /// occurred.
  bool ParseDeclarationTypeSpec(DeclSpec &DS, bool AllowSelectors = true,
                                bool AllowOptionalCommaAfterCharLength = true);

  /// ParseTypeOrClassDeclTypeSpec - parses a TYPE(..) or CLASS(..) type
  /// specification. Returns true if a parsing error ocurred.
  bool ParseTypeOrClassDeclTypeSpec(DeclSpec &DS);

  /// ParseEntityDeclarationList - returns true if a parsing error
  /// occurred.
  bool ParseEntityDeclarationList(DeclSpec &DS,
                                SmallVectorImpl<DeclResult> &Decls);

  /// ParseDimensionAttributeSpec - parses the DIMENSION attribute
  /// for the given declaration and returns true if a parsing error
  /// occurred.
  bool ParseDimensionAttributeSpec(SourceLocation Loc, DeclSpec &DS);

  /// ParseObjectArraySpec - parses the optional array specification
  /// which comes after an object name and returns true if a parsing
  /// error occurred.
  bool ParseObjectArraySpec(SourceLocation Loc, DeclSpec &DS);

  /// ParseObjectCharLength - parses the optional character length
  /// specification which comes after an object name and returns
  /// true if a parsing error occurred.
  bool ParseObjectCharLength(SourceLocation Loc, DeclSpec &DS);

  bool ParseProcedureDeclStmt();
  bool ParseSpecificationStmt();
  StmtResult ParseActionStmt();

  // Designator parsing methods.
  ExprResult ParseDesignator(bool IsLvalue);
  ExprResult ParseNameOrCall();
  ExprResult ParseArrayElement();
  ExprResult ParseArraySection();
  ExprResult ParseCoindexedNamedObject();
  ExprResult ParseComplexPartDesignator();
  ExprResult ParseStructureComponent(ExprResult Target);
  ExprResult ParseSubstring(ExprResult Target);
  ExprResult ParseArraySubscript(ExprResult Target);
  ExprResult ParseArraySection(const char *PunctuationTok);
  ExprResult ParseDataReference();
  ExprResult ParsePartReference();

  // Stmt-level parsing methods.
  void LookForTopLevelStmtKeyword();
  StmtResult ParsePROGRAMStmt();
  StmtResult ParseENDStmt(tok::TokenKind EndKw);
  StmtResult ParseUSEStmt();
  StmtResult ParseIMPORTStmt();
  StmtResult ParseIMPLICITStmt();
  StmtResult ParsePARAMETERStmt();
  StmtResult ParseFORMATStmt();
  StmtResult ParseFORMATSpec(SourceLocation Loc);
  FormatItemResult ParseFORMATItems(bool IsOuter = false);
  FormatItemResult ParseFORMATItem();
  StmtResult ParseENTRYStmt();

  // Specification statement's contents.
  void LookForSpecificationStmtKeyword();
  StmtResult ParseACCESSStmt();
  StmtResult ParseALLOCATABLEStmt();
  StmtResult ParseASYNCHRONOUSStmt();
  StmtResult ParseBINDStmt();
  StmtResult ParseCOMMONStmt();
  StmtResult ParseDATAStmt();
  StmtResult ParseDATAStmtPart(SourceLocation Loc);
  ExprResult ParseDATAStmtImpliedDo();
  StmtResult ParseDIMENSIONStmt();
  StmtResult ParseEQUIVALENCEStmt();
  StmtResult ParseEXTERNALStmt();
  StmtResult ParseINTENTStmt();
  StmtResult ParseINTRINSICStmt(bool IsActuallyExternal = false);
  StmtResult ParseNAMELISTStmt();
  StmtResult ParseOPTIONALStmt();
  StmtResult ParsePOINTERStmt();
  StmtResult ParsePROTECTEDStmt();
  StmtResult ParseSAVEStmt();
  StmtResult ParseTARGETStmt();
  StmtResult ParseVALUEStmt();
  StmtResult ParseVOLATILEStmt();

  // Dynamic association.
  StmtResult ParseALLOCATEStmt();
  StmtResult ParseNULLIFYStmt();
  StmtResult ParseDEALLOCATEStmt();

  StmtResult ParseFORALLStmt();
  StmtResult ParseEND_FORALLStmt();

  // Executable statements
  void LookForExecutableStmtKeyword(bool AtStartOfStatement = true);
  StmtResult ParseAssignStmt();
  StmtResult ParseGotoStmt();

  StmtResult ParseIfStmt();
  StmtResult ParseElseIfStmt();
  StmtResult ParseElseStmt();
  StmtResult ParseEndIfStmt();
  ExprResult ParseExpectedConditionExpression(const char *DiagAfter);

  StmtResult ParseDoStmt();
  StmtResult ParseDoWhileStmt(bool isDo);
  StmtResult ReparseAmbiguousDoWhileStatement();
  StmtResult ParseEndDoStmt();
  StmtResult ParseCycleStmt();
  StmtResult ParseExitStmt();

  StmtResult ParseSelectCaseStmt();
  StmtResult ParseCaseStmt();
  StmtResult ParseEndSelectStmt();

  StmtResult ParseWhereStmt();
  StmtResult ParseElseWhereStmt();
  StmtResult ParseEndWhereStmt();

  StmtResult ParseContinueStmt();
  StmtResult ParseStopStmt();
  StmtResult ParseReturnStmt();
  StmtResult ParseCallStmt();
  StmtResult ParseAmbiguousAssignmentStmt();
  StmtResult ParseAssignmentStmt();
  StmtResult ParsePrintStmt();
  StmtResult ParseWriteStmt();
  UnitSpec *ParseUNITSpec(bool IsLabeled);
  FormatSpec *ParseFMTSpec(bool IsLabeled);
  void ParseIOList(SmallVectorImpl<ExprResult> &List);

  // Helper functions.
  ExprResult ParseLevel5Expr();
  ExprResult ParseEquivOperand();
  ExprResult ParseOrOperand();
  ExprResult ParseAndOperand();
  ExprResult ParseLevel4Expr();
  ExprResult ParseLevel3Expr();
  ExprResult ParseLevel2Expr();
  ExprResult ParseAddOperand();
  ExprResult ParseMultOperand();
  ExprResult ParseLevel1Expr();
  ExprResult ParsePrimaryExpr(bool IsLvalue = false);
  ExprResult ParseExpression();
  ExprResult ParseFunctionCallArgumentList(SmallVectorImpl<Expr*> &Args,
                                           SourceLocation &RParenLoc);
  ExprResult ParseRecursiveCallExpression(SourceRange IDRange);
  ExprResult ParseCallExpression(SourceLocation IDLoc, FunctionDecl *Function);
  ExprResult ParseArrayConstructor();
  ExprResult ParseTypeConstructor(SourceLocation IDLoc, RecordDecl *Record);

  /// \brief Looks at the next token to see if it's an expression
  /// and calls ParseExpression if it is, or reports an expected expression
  /// error.
  ExprResult ParseExpectedExpression();

  /// \brief Looks at the next token to see if it's an expression
  /// and calls ParseExpression if it is, or reports an expected expression
  /// error.
  ExprResult ParseExpectedFollowupExpression(const char *DiagAfter = "");

  void ParseStatementLabel();
  ExprResult ParseStatementLabelReference(bool Consume = true);

  /// ParseConstructNameLabel - Parses an optional construct-name ':' label.
  /// If the construct name isn't there, then set the ConstructName to null.
  void ParseConstructNameLabel();

  /// ParseTrailingConstructName - Parses an optional trailing construct-name identifier.
  /// If the construct name isn't there, then set the ConstructName to null.
  void ParseTrailingConstructName();

  // Declaration construct functions
  bool ParseDerivedTypeDefinitionStmt();
  void ParseEndTypeStmt();
  bool ParseDerivedTypeComponentStmt();

  ExprResult ParseSelector(bool IsKindSel);
  bool ParseArraySpec(llvm::SmallVectorImpl<ArraySpec*> &Dims);
  bool ParseCharacterStarLengthSpec(DeclSpec &DS);

  void SetKindSelector(ConstantExpr *E, StringRef Kind);

};

} // end flang namespace

#endif