dmd.cparse

Takes a token stream from the lexer, and parses it into an abstract syntax tree.

Specification: C11

Authors

Walter Bright

Source: cparse.d

  • Declaration

    class CParser(AST): Parser!AST;

    • Declaration

      bool addFuncName;

      add declaration of _func__ to function symbol table

    • Declaration

      bool importBuiltins;

      seen use of C compiler builtins, so import _builtins;

    • Declaration

      Array!(void*) typedefTab;

      Array of AST.Type[Identifier], typedef's indexed by Identifier

    • Declaration

      AST.Dsymbols* parseModule();

      Parse translation unit. C11 6.9 translation-unit: external-declaration translation-unit external-declaration

      Discussion

      external-declaration: function-definition declaration

      Return Value

      array of Dsymbols that were declared

    • Declaration

      AST.Statement cparseStatement(int flags, const(char)** endPtr = null, Loc* pEndloc = null);

      Statement Parser

      Discussion

      C11 6.8 statement: labeled-statement compound-statement expression-statement selection-statement iteration-statement jump-statement

      Parameters

      int flags

      PSxxxx

      const(char)** endPtr

      store location of closing brace

      Loc* pEndloc

      if { ... statements ... }, store location of closing brace, otherwise loc of last token of statement

      Return Value

      parsed statement

    • Declaration

      AST.Expression cparseExpression();

      Expression Parser

      Discussion

      C11 6.5.17 expression: assignment-expression expression , assignment-expression

    • Declaration

      AST.Expression cparsePrimaryExp();

      C11 6.5.1 primary-expression: identifier constant string-literal ( expression ) generic-selection _builtin_va_arg(assign_expression, type)

    • Declaration

      AST.Expression cparseAssignExp();

      C11 6.5.16 assignment-expression: conditional-expression unary-expression assignment-operator assignment-expression

      Discussion

      assignment-operator: = *= /= %= += -= <<= >>= &= ^= |=

    • Declaration

      void cparseDeclaration(LVL level);

      Declaration Parser

      Discussion

      C11 6.7 declaration: declaration-specifiers init-declarator-list (opt) ; static_assert-declaration

      init-declarator-list: init-declarator init-declarator-list , init-declarator

      init-declarator: declarator declarator = initializer

      Parameters

      LVL level

      declaration context

    • Declaration

      AST.Dsymbol cparseFunctionDefinition(Identifier id, AST.TypeFunction ft, ref Specifier specifier);

      C11 Function Definitions function-definition declaration-specifiers declarator declaration-list (opt) compound-statement

      Discussion

      declaration-list: declaration declaration-list declaration

      It's already been parsed up to the declaration-list (opt). Pick it up from there.

      Parameters

      Identifier id

      function identifier

      AST.TypeFunction ft

      function type

      Specifier specifier

      function specifiers

      Return Value

      Dsymbol for the function

    • Declaration

      AST.Initializer cparseInitializer();

      C11 Initialization initializer: assignment-expression { initializer-list } { initializer-list , }

      Discussion

      initializer-list: designation (opt) initializer initializer-list , designation (opt) initializer

      designation: designator-list =

      designator-list: designator designator-list designator

      designator: [ constant-expression ] . identifier

      Return Value

      initializer

    • Declaration

      MOD cparseTypeQualifierList();

      C11 6.7.3 type-qualifier: const restrict volatile Atomic _stdcall

    • Declaration

      AST.Type cparseTypeName();

      C11 6.7.7

    • Declaration

      AST.Type cparseSpecifierQualifierList(LVL level, ref Specifier specifier);

      C11 6.7.2.1 specifier-qualifier-list: type-specifier specifier-qualifier-list (opt) type-qualifier specifier-qualifier-list (opt)

      Parameters

      LVL level

      declaration context

      Specifier specifier

      specifiers in and out

      Return Value

      resulting type, null if not specified

    • Declaration

      AST.ParameterList cparseParameterList();

      C11 6.7.6.3 ( parameter-type-list ) ( identifier-list (opt) )

    • Declaration

      void cparseStructDeclaration(AST.Dsymbols* members);

      C11 6.7.2.1 Parse a struct declaration member. struct-declaration: specifier-qualifier-list struct-declarator-list (opt) ; static_assert-declaration

      Discussion

      struct-declarator-list: struct-declarator struct-declarator-list , struct-declarator

      struct-declarator: declarator declarator (opt) : constant-expression

      Parameters

      AST.Dsymbols* members

      where to put the fields (members)

    • Declaration

      bool isDeclarationList(ref Token* pt);

      See if declaration-list is present.

      Return Value

      true if declaration-list is present, even an empty one

    • LVL

      Declaration

      enum LVL;

      More

      Discussion

      Declaration context

      • Declaration

        global

        global

      • Declaration

        parameter

        function parameter (declarations for function identifier-list)

      • Declaration

        prototype

        function prototype

      • Declaration

        local

        local

      • Declaration

        member

        struct member

    • DTR

      Declaration

      enum DTR;

      Types of declarator to parse

      • Declaration

        xdirect

        C11 6.7.6 direct-declarator

      • Declaration

        xabstract

        C11 6.7.7 abstract-declarator

      • Declaration

        xparameter

        parameter declarator may be either direct or abstract

    • SCW

      Declaration

      enum SCW: uint;

      C11 6.7.1 Storage-class specifiers

    • MOD

      Declaration

      enum MOD: uint;

      C11 6.7.3 Type qualifiers

    • Declaration

      struct Specifier;

      Aggregate for all the various specifiers

      • Declaration

        bool noreturn;

        noreturn attribute

      • Declaration

        bool naked;

        naked attribute

      • Declaration

        bool _nothrow;

        nothrow attribute

      • Declaration

        bool _pure;

        pure attribute

      • Declaration

        bool dllimport;

        dllimport attribute

      • Declaration

        bool dllexport;

        dllexport attribute

      • Declaration

        bool _deprecated;

        deprecated attribute

      • Declaration

        AST.Expression depMsg;

        deprecated message

      • scw

        Declaration

        SCW scw;

        storage-class specifiers

      • mod

        Declaration

        MOD mod;

        type qualifiers

      • Declaration

        AST.Expressions* alignExps;

        alignment

      • Declaration

        structalign_t packalign;

        #pragma pack alignment value

    • Declaration

      StorageClass specifiersToSTC(LVL level, const ref Specifier specifier);

      Convert from C specifiers to D storage class

      Parameters

      LVL level

      declaration context

      Specifier specifier

      specifiers, context, etc.

      Return Value

      corresponding D storage class

    • Declaration

      void specifiersToFuncDeclaration(AST.FuncDeclaration fd, const ref Specifier specifier);

      Add attributes from Specifier to function

      Parameters

      AST.FuncDeclaration fd

      function to apply them to

      Specifier specifier

      specifiers

    • Declaration

      void specifiersToVarDeclaration(AST.VarDeclaration vd, const ref Specifier specifier);

      Add attributes from Specifier to variable

      Parameters

      AST.VarDeclaration vd

      function to apply them to

      Specifier specifier

      specifiers

    • Declaration

      void panic();

      After encountering an error, scan forward until a right brace or ; is found or the end of the file.

    • Declaration

      bool isFunctionTypedef(AST.Type t);

      typedefTab symbol table

      Discussion

      Determines if type t is a function type.

      Parameters

      AST.Type t

      type to test

      Return Value

      true if it represents a function

    • Declaration

      bool isTypedef(Identifier id);

      Determine if id is a symbol for a Typedef.

      Parameters

      Identifier id

      possible typedef

      Return Value

      true if id is a Type

    • Declaration

      void insertIdToTypedefTab(Identifier id);

      Add id to typedefTab[], but only if it will mask an existing typedef.

      Parameters

      Identifier id

      identifier for non-typedef symbol

    • Declaration

      void insertTypedefToTypedefTab(Identifier id, AST.Type t);

      Add id to typedefTab[]

      Parameters

      Identifier id

      identifier for typedef symbol

      AST.Type t

      type of the typedef symbol

    • Declaration

      AST.Type* lookupTypedef(Identifier id);

      Lookup id in typedefTab[].

      Return Value

      if not found, then null. if found, then Type*. Deferencing it will yield null if it is not a typedef, and a type if it is a typedef.

    • Declaration

      bool parseSpecialTokenSequence();

      Directive Parser

    • Declaration

      void addDefines();

      #define Parser

      Discussion

      Go through the #define's in the defines buffer and see what we can convert to Dsymbols, which are then appended to symbols[]