# dmd.cparse

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

Specification: C11

#### Authors

Walter Bright

#### License

Boost License 1.0

Source: cparse.d

Documentation: https://dlang.org/phobos/dmd_cparse.html

Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/cparse.d

- `CParser`

  #### Declaration

  `class `CParser`(AST): Parser!AST;`

  - `addFuncName`

    #### Declaration

    `bool `addFuncName`;`

    add declaration of _func__ to function symbol table

  - `importBuiltins`

    #### Declaration

    `bool `importBuiltins`;`

    seen use of C compiler builtins, so import _builtins;

  - `typedefTab`

    #### Declaration

    `Array!(void*) `typedefTab`;`

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

  - `parseModule`

    #### 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

  - `cparseStatement`

    #### 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

  - `cparseExpression`

    #### Declaration

    `AST.Expression `cparseExpression`();`

    Expression Parser

    #### Discussion

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

  - `cparsePrimaryExp`

    #### Declaration

    `AST.Expression `cparsePrimaryExp`();`

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

  - `cparseAssignExp`

    #### Declaration

    `AST.Expression `cparseAssignExp`();`

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

    #### Discussion

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

  - `cparseDeclaration`

    #### 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

  - `cparseFunctionDefinition`

    #### 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

  - `cparseInitializer`

    #### 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

  - `cparseTypeQualifierList`

    #### Declaration

    `MOD `cparseTypeQualifierList`();`

    C11 6.7.3 type-qualifier: const restrict volatile Atomic _stdcall

  - `cparseTypeName`

    #### Declaration

    `AST.Type `cparseTypeName`();`

    C11 6.7.7

  - `cparseSpecifierQualifierList`

    #### 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

  - `cparseParameterList`

    #### Declaration

    `AST.ParameterList `cparseParameterList`();`

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

  - `cparseStructDeclaration`

    #### 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`)

  - `isDeclarationList`

    #### 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

    - `global`

      #### Declaration

      ``global``

      `global`

    - `parameter`

      #### Declaration

      ``parameter``

      function `parameter` (declarations for function identifier-list)

    - `prototype`

      #### Declaration

      ``prototype``

      function `prototype`

    - `local`

      #### Declaration

      ``local``

      `local`

    - `member`

      #### Declaration

      ``member``

      struct `member`

  - `DTR`

    #### Declaration

    `enum `DTR`;`

    Types of declarator to parse

    - `xdirect`

      #### Declaration

      ``xdirect``

      C11 6.7.6 direct-declarator

    - `xabstract`

      #### Declaration

      ``xabstract``

      C11 6.7.7 abstract-declarator

    - `xparameter`

      #### 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

  - `Specifier`

    #### Declaration

    `struct `Specifier`;`

    Aggregate for all the various specifiers

    - `noreturn`

      #### Declaration

      `bool `noreturn`;`

      `noreturn` attribute

    - `naked`

      #### Declaration

      `bool `naked`;`

      `naked` attribute

    - `_nothrow`

      #### Declaration

      `bool `_nothrow`;`

      nothrow attribute

    - `_pure`

      #### Declaration

      `bool `_pure`;`

      pure attribute

    - `dllimport`

      #### Declaration

      `bool `dllimport`;`

      `dllimport` attribute

    - `dllexport`

      #### Declaration

      `bool `dllexport`;`

      `dllexport` attribute

    - `_deprecated`

      #### Declaration

      `bool `_deprecated`;`

      deprecated attribute

    - `depMsg`

      #### Declaration

      `AST.Expression `depMsg`;`

      deprecated message

    - `vector_size`

      #### Declaration

      `uint `vector_size`;`

      positive power of 2 multipe of base type size

    - `scw`

      #### Declaration

      `SCW `scw`;`

      storage-class specifiers

    - `mod`

      #### Declaration

      `MOD `mod`;`

      type qualifiers

    - `alignExps`

      #### Declaration

      `AST.Expressions* `alignExps`;`

      alignment

    - `packalign`

      #### Declaration

      `structalign_t `packalign`;`

      #pragma pack alignment value

  - `specifiersToSTC`

    #### 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

  - `specifiersToFuncDeclaration`

    #### 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

  - `specifiersToVarDeclaration`

    #### 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

  - `panic`

    #### Declaration

    `void `panic`();`

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

  - `isFunctionTypedef`

    #### 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

  - `isTypedef`

    #### 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

  - `insertIdToTypedefTab`

    #### 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

  - `insertTypedefToTypedefTab`

    #### 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

  - `lookupTypedef`

    #### 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.

  - `parseSpecialTokenSequence`

    #### Declaration

    `bool `parseSpecialTokenSequence`();`

    Directive Parser

  - `addDefines`

    #### 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[]