Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

dmd.cparse

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

Specification C11

Authors:

Source cparse.d

class CParser(AST): Parser!AST;
bool addFuncName;
add declaration of _func__ to function symbol table
bool importBuiltins;
seen use of C compiler builtins, so import _importc_builtins;
Array!(void*) typedefTab;
Array of AST.Type[Identifier], typedef's indexed by Identifier
AST.Dsymbols* parseModule();
Parse translation unit. C11 6.9 translation-unit: external-declaration translation-unit external-declaration
external-declaration: function-definition declaration
Returns:
array of Dsymbols that were declared
AST.Statement cparseStatement(int flags, const(char)** endPtr = null, Loc* pEndloc = null);
Statement Parser
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
Returns:
parsed statement
AST.Expression cparseExpression();
Expression Parser
C11 6.5.17 expression: assignment-expression expression , assignment-expression
AST.Expression cparsePrimaryExp();
C11 6.5.1 primary-expression: identifier constant string-literal ( expression ) generic-selection _builtin_va_arg(assign_expression, type)
AST.Expression cparseAssignExp();
C11 6.5.16 assignment-expression: conditional-expression unary-expression assignment-operator assignment-expression
assignment-operator: = *= /= %= += -= <<= >>= &= ^= |=
void cparseDeclaration(LVL level);
Declaration Parser
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
AST.Dsymbol cparseFunctionDefinition(Identifier id, AST.TypeFunction ft, ref Specifier specifier);
C11 Function Definitions function-definition declaration-specifiers declarator declaration-list (opt) compound-statement
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
Returns:
Dsymbol for the function
AST.Initializer cparseInitializer();
C11 Initialization initializer: assignment-expression { } // C23 6.7.10 addition { initializer-list } { initializer-list , }
initializer-list: designation (opt) initializer initializer-list , designation (opt) initializer
designation: designator-list =
designator-list: designator designator-list designator
designator: [ constant-expression ] . identifier
Returns:
initializer
MOD cparseTypeQualifierList();
C11 6.7.3 type-qualifier: const restrict volatile Atomic _stdcall
AST.Type cparseTypeName();
C11 6.7.7
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
Returns:
resulting type, null if not specified
AST.ParameterList cparseParameterList();
C11 6.7.6.3 ( parameter-type-list ) ( identifier-list (opt) )
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
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)
bool isDeclarationList(ref Token* pt);
See if declaration-list is present.
Returns:
true if declaration-list is present, even an empty one
enum LVL;
More
Declaration context
global
global
parameter
function parameter (declarations for function identifier-list)
prototype
function prototype
local
local
member
struct member
enum DTR;
Types of declarator to parse
xdirect
C11 6.7.6 direct-declarator
xabstract
C11 6.7.7 abstract-declarator
xparameter
parameter declarator may be either direct or abstract
enum SCW: uint;
C11 6.7.1 Storage-class specifiers
enum MOD: uint;
C11 6.7.3 Type qualifiers
struct Specifier;
Aggregate for all the various specifiers
bool noreturn;
noreturn attribute
bool naked;
naked attribute
bool _nothrow;
nothrow attribute
bool _pure;
pure attribute
bool dllimport;
dllimport attribute
bool dllexport;
dllexport attribute
bool _deprecated;
deprecated attribute
AST.Expression depMsg;
deprecated message
uint vector_size;
positive power of 2 multipe of base type size
SCW scw;
storage-class specifiers
MOD mod;
type qualifiers
AST.Expressions* alignExps;
alignment
structalign_t packalign;
#pragma pack alignment value
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.
Returns:
corresponding D storage class
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
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
void panic();
After encountering an error, scan forward until a right brace or ; is found or the end of the file.
bool isFunctionTypedef(AST.Type t);
typedefTab symbol table
Determines if type t is a function type.
Parameters:
AST.Type t type to test
Returns:
true if it represents a function
bool isTypedef(Identifier id);
Determine if id is a symbol for a Typedef.
Parameters:
Identifier id possible typedef
Returns:
true if id is a Type
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
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
AST.Type* lookupTypedef(Identifier id);
Lookup id in typedefTab[].
Returns:
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.
bool parseSpecialTokenSequence();
Directive Parser
void addDefines();
#define Parser
Go through the #define's in the defines buffer and see what we can convert to Dsymbols, which are then appended to symbols[]