dmd.cparse
Takes a token stream from the lexer, and parses it into an abstract syntax tree.
Specification: C11
License
Source: cparse.d
Documentation: https://dlang.org/phobos/dmd_cparse.html
-
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
(intflags
, 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
(LVLlevel
);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 = initializerParameters
LVL
level
declaration context
-
Declaration
AST.Dsymbol
cparseFunctionDefinition
(Identifierid
, AST.TypeFunctionft
, ref Specifierspecifier
);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 ] . identifierReturn 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
(LVLlevel
, ref Specifierspecifier
);C11 6.7.2.1
specifier
-qualifier-list: type-specifier
specifier
-qualifier-list (opt) type-qualifierspecifier
-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-expressionParameters
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 -
Declaration
enum
LVL
; -
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
-
-
Declaration
enum
SCW
: uint;C11 6.7.1 Storage-class specifiers
-
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
-
Declaration
uint
vector_size
;positive power of 2 multipe of base type size
-
Declaration
SCW
scw
;storage-class specifiers
-
Declaration
MOD
mod
;type qualifiers
-
Declaration
AST.Expressions*
alignExps
;alignment
-
Declaration
structalign_t
packalign
;#pragma pack alignment value
-
-
Declaration
StorageClass
specifiersToSTC
(LVLlevel
, const ref Specifierspecifier
);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.FuncDeclarationfd
, const ref Specifierspecifier
);Add attributes from Specifier to function
Parameters
AST.FuncDeclaration
fd
function to apply them to
Specifier
specifier
specifiers
-
Declaration
void
specifiersToVarDeclaration
(AST.VarDeclarationvd
, const ref Specifierspecifier
);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.Typet
);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
(Identifierid
);Determine if
is a symbol for a Typedef.id
Parameters
Identifier
id
possible typedef
Return Value
true
ifid
is a Type -
Declaration
void
insertIdToTypedefTab
(Identifierid
);Add
to typedefTab[], but only if it will mask an existing typedef.id
Parameters
Identifier
id
identifier for non-typedef symbol
-
Declaration
void
insertTypedefToTypedefTab
(Identifierid
, AST.Typet
);Add
to typedefTab[]id
Parameters
Identifier
id
identifier for typedef symbol
AST.Type
t
type of the typedef symbol
-
Declaration
AST.Type*
lookupTypedef
(Identifierid
);Lookup
id
in typedefTab[].Return Value
if not found, then
null
. if found, then Type*. Deferencing it will yieldnull
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[]
-