1 2/* Compiler implementation of the D programming language 3 * Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved 4 * written by Walter Bright 5 * http://www.digitalmars.com 6 * Distributed under the Boost Software License, Version 1.0. 7 * http://www.boost.org/LICENSE_1_0.txt 8 * https://github.com/D-Programming-Language/dmd/blob/master/src/parse.h 9 */ 10 11#pragma once 12 13#include "arraytypes.h" 14#include "lexer.h" 15#include "enum.h" 16 17class Type; 18class TypeQualified; 19class Expression; 20class Declaration; 21class Statement; 22class Import; 23class Initializer; 24class FuncDeclaration; 25class CtorDeclaration; 26class PostBlitDeclaration; 27class DtorDeclaration; 28class StaticCtorDeclaration; 29class StaticDtorDeclaration; 30class SharedStaticCtorDeclaration; 31class SharedStaticDtorDeclaration; 32class ConditionalDeclaration; 33class InvariantDeclaration; 34class UnitTestDeclaration; 35class NewDeclaration; 36class DeleteDeclaration; 37class Condition; 38class Module; 39struct ModuleDeclaration; 40class TemplateDeclaration; 41class TemplateInstance; 42class StaticAssert; 43struct PrefixAttributes; 44 45/************************************ 46 * These control how parseStatement() works. 47 */ 48 49enum ParseStatementFlags 50{ 51 PSsemi = 1, // empty ';' statements are allowed, but deprecated 52 PSscope = 2, // start a new scope 53 PScurly = 4, // { } statement is required 54 PScurlyscope = 8, // { } starts a new scope 55 PSsemi_ok = 0x10 // empty ';' are really ok 56}; 57 58 59class Parser : public Lexer 60{ 61public: 62 Module *mod; 63 ModuleDeclaration *md; 64 LINK linkage; 65 CPPMANGLE cppmangle; 66 Loc endloc; // set to location of last right curly 67 int inBrackets; // inside [] of array index or slice 68 Loc lookingForElse; // location of lonely if looking for an else 69 70 Parser(Loc loc, Module *module, const utf8_t *base, size_t length, bool doDocComment); 71 Parser(Module *module, const utf8_t *base, size_t length, bool doDocComment); 72 73 Dsymbols *parseModule(); 74 Dsymbols *parseDeclDefs(int once, Dsymbol **pLastDecl = NULL, PrefixAttributes *pAttrs = NULL); 75 Dsymbols *parseAutoDeclarations(StorageClass storageClass, const utf8_t *comment); 76 Dsymbols *parseBlock(Dsymbol **pLastDecl, PrefixAttributes *pAttrs = NULL); 77 StorageClass appendStorageClass(StorageClass storageClass, StorageClass stc, bool deprec = false); 78 StorageClass parseAttribute(Expressions **pexps); 79 StorageClass parsePostfix(StorageClass storageClass, Expressions **pudas); 80 StorageClass parseTypeCtor(); 81 Expression *parseConstraint(); 82 TemplateDeclaration *parseTemplateDeclaration(bool ismixin = false); 83 TemplateParameters *parseTemplateParameterList(int flag = 0); 84 Dsymbol *parseMixin(); 85 Objects *parseTemplateArguments(); 86 Objects *parseTemplateArgumentList(); 87 Objects *parseTemplateSingleArgument(); 88 StaticAssert *parseStaticAssert(); 89 TypeQualified *parseTypeof(); 90 Type *parseVector(); 91 LINK parseLinkage(Identifiers **, CPPMANGLE *, bool *); 92 Identifiers *parseQualifiedIdentifier(const char *entity); 93 Condition *parseDebugCondition(); 94 Condition *parseVersionCondition(); 95 Condition *parseStaticIfCondition(); 96 Dsymbol *parseCtor(PrefixAttributes *pAttrs); 97 Dsymbol *parseDtor(PrefixAttributes *pAttrs); 98 Dsymbol *parseStaticCtor(PrefixAttributes *pAttrs); 99 Dsymbol *parseStaticDtor(PrefixAttributes *pAttrs); 100 Dsymbol *parseSharedStaticCtor(PrefixAttributes *pAttrs); 101 Dsymbol *parseSharedStaticDtor(PrefixAttributes *pAttrs); 102 Dsymbol *parseInvariant(PrefixAttributes *pAttrs); 103 Dsymbol *parseUnitTest(PrefixAttributes *pAttrs); 104 Dsymbol *parseNew(PrefixAttributes *pAttrs); 105 Dsymbol *parseDelete(PrefixAttributes *pAttrs); 106 Parameters *parseParameters(int *pvarargs, TemplateParameters **tpl = NULL); 107 EnumDeclaration *parseEnum(); 108 Dsymbol *parseAggregate(); 109 BaseClasses *parseBaseClasses(); 110 Dsymbols *parseImport(); 111 Type *parseType(Identifier **pident = NULL, TemplateParameters **ptpl = NULL); 112 Type *parseBasicType(bool dontLookDotIdents = false); 113 Type *parseBasicTypeStartingAt(TypeQualified *tid, bool dontLookDotIdents); 114 Type *parseBasicType2(Type *t); 115 Type *parseDeclarator(Type *t, int *alt, Identifier **pident, 116 TemplateParameters **tpl = NULL, StorageClass storage_class = 0, int *pdisable = NULL, Expressions **pudas = NULL); 117 void parseStorageClasses(StorageClass &storage_class, LINK &link, bool &setAlignment, Expression *&ealign, Expressions *&udas); 118 Dsymbols *parseDeclarations(bool autodecl, PrefixAttributes *pAttrs, const utf8_t *comment); 119 Dsymbol *parseFunctionLiteral(); 120 FuncDeclaration *parseContracts(FuncDeclaration *f); 121 void checkDanglingElse(Loc elseloc); 122 void checkCstyleTypeSyntax(Loc loc, Type *t, int alt, Identifier *ident); 123 Statement *parseForeach(Loc loc, bool *isRange, bool isDecl); 124 Dsymbol *parseForeachStaticDecl(Loc loc, Dsymbol **pLastDecl); 125 Statement *parseForeachStatic(Loc loc); 126 /** endPtr used for documented unittests */ 127 Statement *parseStatement(int flags, const utf8_t** endPtr = NULL, Loc *pEndloc = NULL); 128 Initializer *parseInitializer(); 129 Expression *parseDefaultInitExp(); 130 void check(Loc loc, TOK value); 131 void check(TOK value); 132 void check(TOK value, const char *string); 133 void checkParens(TOK value, Expression *e); 134 bool isDeclaration(Token *t, int needId, TOK endtok, Token **pt); 135 bool isBasicType(Token **pt); 136 bool isDeclarator(Token **pt, int *haveId, int *haveTpl, TOK endtok, bool allowAltSyntax = true); 137 bool isParameters(Token **pt); 138 bool isExpression(Token **pt); 139 bool skipParens(Token *t, Token **pt); 140 bool skipParensIf(Token *t, Token **pt); 141 bool skipAttributes(Token *t, Token **pt); 142 143 Expression *parseExpression(); 144 Expression *parsePrimaryExp(); 145 Expression *parseUnaryExp(); 146 Expression *parsePostExp(Expression *e); 147 Expression *parseMulExp(); 148 Expression *parseAddExp(); 149 Expression *parseShiftExp(); 150 Expression *parseCmpExp(); 151 Expression *parseAndExp(); 152 Expression *parseXorExp(); 153 Expression *parseOrExp(); 154 Expression *parseAndAndExp(); 155 Expression *parseOrOrExp(); 156 Expression *parseCondExp(); 157 Expression *parseAssignExp(); 158 159 Expressions *parseArguments(); 160 161 Expression *parseNewExp(Expression *thisexp); 162 163 void addComment(Dsymbol *s, const utf8_t *blockComment); 164}; 165 166// Operator precedence - greater values are higher precedence 167 168enum PREC 169{ 170 PREC_zero, 171 PREC_expr, 172 PREC_assign, 173 PREC_cond, 174 PREC_oror, 175 PREC_andand, 176 PREC_or, 177 PREC_xor, 178 PREC_and, 179 PREC_equal, 180 PREC_rel, 181 PREC_shift, 182 PREC_add, 183 PREC_mul, 184 PREC_pow, 185 PREC_unary, 186 PREC_primary 187}; 188 189extern PREC precedence[TOKMAX]; 190 191void initPrecedence(); 192