source: src/Parser/DeclarationNode.cc@ c443d1d

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since c443d1d was dd020c0, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

first attempt to create function specifiers

  • Property mode set to 100644
File size: 34.9 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[974906e2]7// DeclarationNode.cc --
[b87a5ed]8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
[8e9cbb2]11// Last Modified By : Peter A. Buhr
[dd020c0]12// Last Modified On : Fri Mar 3 21:38:34 2017
13// Update Count : 862
[b87a5ed]14//
15
[51b73452]16#include <string>
17#include <list>
18#include <iterator>
19#include <algorithm>
20#include <cassert>
21
22#include "TypeData.h"
[bdd516a]23
[44a81853]24#include "SynTree/Attribute.h"
[68cd1ce]25#include "SynTree/Declaration.h"
26#include "SynTree/Expression.h"
[51b73452]27
[de62360d]28#include "TypedefTable.h"
29extern TypedefTable typedefTable;
30
[51b73452]31using namespace std;
32
[bdd516a]33// These must remain in the same order as the corresponding DeclarationNode enumerations.
[dd020c0]34const char * DeclarationNode::storageClassNames[] = { "extern", "static", "auto", "register", "_Thread_local", "inline", "fortran", "_Noreturn", "NoStorageClassNames" };
35const char * DeclarationNode::funcSpecifierNames[] = { "inline", "fortran", "_Noreturn", "NoFunctionSpecifierNames" };
36const char * DeclarationNode::typeQualifierNames[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoTypeQualifierNames" };
37const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicTypeNames" };
38const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
39const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
40const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
41const char * DeclarationNode::aggregateNames[] = { "struct", "union", "context", "NoAggregateNames" };
42const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
43const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "NoBuiltinTypeNames" };
[51b73452]44
45UniqueName DeclarationNode::anonymous( "__anonymous" );
46
[8b7ee09]47extern LinkageSpec::Spec linkage; // defined in parser.yy
[51b73452]48
[7d05e7e]49DeclarationNode::DeclarationNode() :
[2298f728]50 type( nullptr ),
[7d05e7e]51 storageClass( NoStorageClass ),
[58dd019]52 bitfieldWidth( nullptr ),
[7d05e7e]53 hasEllipsis( false ),
54 linkage( ::linkage ),
[58dd019]55 asmName( nullptr ),
56 initializer( nullptr ),
[e994912]57 extension( false ),
58 asmStmt( nullptr ) {
[2298f728]59
[faddbd8]60// variable.name = nullptr;
61 variable.tyClass = NoTypeClass;
[28307be]62 variable.assertions = nullptr;
[7d05e7e]63
[faddbd8]64// attr.name = nullptr;
[7d05e7e]65 attr.expr = nullptr;
66 attr.type = nullptr;
[28307be]67}
68
69DeclarationNode::~DeclarationNode() {
[faddbd8]70// delete attr.name;
[28307be]71 delete attr.expr;
72 delete attr.type;
[2298f728]73
[faddbd8]74// delete variable.name;
[2298f728]75 delete variable.assertions;
76
[28307be]77 delete type;
78 delete bitfieldWidth;
[e994912]79
80 delete asmStmt;
[58dd019]81 // asmName, no delete, passed to next stage
[28307be]82 delete initializer;
83}
84
[ba7aa2d]85DeclarationNode * DeclarationNode::clone() const {
86 DeclarationNode * newnode = new DeclarationNode;
[c0aa336]87 newnode->set_next( maybeClone( get_next() ) );
[2298f728]88 newnode->name = name ? new string( *name ) : nullptr;
[c0aa336]89
90 newnode->type = maybeClone( type );
[13e3b50]91 newnode->storageClass = storageClass;
[c0aa336]92 newnode->bitfieldWidth = maybeClone( bitfieldWidth );
[dd020c0]93 newnode->funcSpec = funcSpec;
[c0aa336]94 newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
[b87a5ed]95 newnode->hasEllipsis = hasEllipsis;
96 newnode->linkage = linkage;
[58dd019]97 newnode->asmName = maybeClone( asmName );
[c0aa336]98 cloneAll( attributes, newnode->attributes );
99 newnode->initializer = maybeClone( initializer );
100 newnode->extension = extension;
[e994912]101 newnode->asmStmt = maybeClone( asmStmt );
[c0aa336]102 newnode->error = error;
[3848e0e]103
[faddbd8]104// newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
[28307be]105 newnode->variable.tyClass = variable.tyClass;
[fb114fa1]106 newnode->variable.assertions = maybeClone( variable.assertions );
[3848e0e]107
[faddbd8]108// newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
[28307be]109 newnode->attr.expr = maybeClone( attr.expr );
110 newnode->attr.type = maybeClone( attr.type );
111 return newnode;
112} // DeclarationNode::clone
[3848e0e]113
114bool DeclarationNode::get_hasEllipsis() const {
[b87a5ed]115 return hasEllipsis;
[3848e0e]116}
117
[dd020c0]118void DeclarationNode::print_FuncSpec( std::ostream & output, DeclarationNode::FuncSpec funcSpec ) {
119 if ( funcSpec.any() ) { // function specifiers?
120 for ( unsigned int i = 0; i < DeclarationNode::NoFuncSpecifier; i += 1 ) {
121 if ( funcSpec[i] ) {
122 output << DeclarationNode::funcSpecifierNames[i] << ' ';
123 } // if
124 } // for
125 } // if
126} // print_FuncSpec
127
[3848e0e]128void DeclarationNode::print( std::ostream &os, int indent ) const {
[59db689]129 os << string( indent, ' ' );
[2298f728]130 if ( name ) {
131 os << *name << ": ";
[b87a5ed]132 } else {
[2298f728]133 os << "unnamed: ";
[68cd1ce]134 } // if
[51b73452]135
[b87a5ed]136 if ( linkage != LinkageSpec::Cforall ) {
[faddbd8]137 os << LinkageSpec::linkageName( linkage ) << " ";
[68cd1ce]138 } // if
[3848e0e]139
[dd020c0]140 if ( storageClass != NoStorageClass ) os << DeclarationNode::storageClassNames[storageClass] << ' ';
141 print_FuncSpec( os, funcSpec );
142
[b87a5ed]143 if ( type ) {
144 type->print( os, indent );
145 } else {
146 os << "untyped entity ";
[68cd1ce]147 } // if
[3848e0e]148
[b87a5ed]149 if ( bitfieldWidth ) {
[59db689]150 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
[b87a5ed]151 bitfieldWidth->printOneLine( os );
[68cd1ce]152 } // if
[3848e0e]153
[2298f728]154 if ( initializer ) {
[59db689]155 os << endl << string( indent + 2, ' ' ) << "with initializer ";
[b87a5ed]156 initializer->printOneLine( os );
[974906e2]157 os << " maybe constructed? " << initializer->get_maybeConstructed();
158
[68cd1ce]159 } // if
[3848e0e]160
[b87a5ed]161 os << endl;
[51b73452]162}
163
[3848e0e]164void DeclarationNode::printList( std::ostream &os, int indent ) const {
[b87a5ed]165 ParseNode::printList( os, indent );
166 if ( hasEllipsis ) {
167 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
[68cd1ce]168 } // if
[51b73452]169}
170
[fb114fa1]171DeclarationNode * DeclarationNode::newFunction( string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle ) {
[ba7aa2d]172 DeclarationNode * newnode = new DeclarationNode;
[2298f728]173 newnode->name = name;
[b87a5ed]174 newnode->type = new TypeData( TypeData::Function );
[8f6f47d7]175 newnode->type->function.params = param;
176 newnode->type->function.newStyle = newStyle;
177 newnode->type->function.body = body;
[c0aa336]178
[2298f728]179 // ignore unnamed routine declarations: void p( int (*)(int) );
180 if ( newnode->name ) {
181 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
182 } // if
[3848e0e]183
[b87a5ed]184 if ( ret ) {
185 newnode->type->base = ret->type;
[2298f728]186 ret->type = nullptr;
[b87a5ed]187 delete ret;
[68cd1ce]188 } // if
[51b73452]189
[b87a5ed]190 return newnode;
[984dce6]191} // DeclarationNode::newFunction
[3848e0e]192
[dd020c0]193
194DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
[ba7aa2d]195 DeclarationNode * newnode = new DeclarationNode;
[dd020c0]196 newnode->storageClass = sc;
[b87a5ed]197 return newnode;
[dd020c0]198} // DeclarationNode::newStorageClass
[3848e0e]199
[dd020c0]200DeclarationNode * DeclarationNode::newFuncSpecifier( DeclarationNode::FuncSpecifier fs ) {
[ba7aa2d]201 DeclarationNode * newnode = new DeclarationNode;
[dd020c0]202 newnode->funcSpec[ fs ] = true;
[c1c1112]203 return newnode;
[dd020c0]204} // DeclarationNode::newFuncSpecifier
[c1c1112]205
[dd020c0]206DeclarationNode * DeclarationNode::newTypeQualifier( TypeQualifier tq ) {
[ba7aa2d]207 DeclarationNode * newnode = new DeclarationNode;
[dd020c0]208 newnode->type = new TypeData();
209 newnode->type->typeQualifiers[ tq ] = true;
[b87a5ed]210 return newnode;
[dd020c0]211} // DeclarationNode::newQualifier
[3848e0e]212
[c1c1112]213DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
[ba7aa2d]214 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]215 newnode->type = new TypeData( TypeData::Basic );
[5b639ee]216 newnode->type->basictype = bt;
[b87a5ed]217 return newnode;
[984dce6]218} // DeclarationNode::newBasicType
[3848e0e]219
[5b639ee]220DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
[ba7aa2d]221 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]222 newnode->type = new TypeData( TypeData::Basic );
[5b639ee]223 newnode->type->complextype = ct;
[b87a5ed]224 return newnode;
[5b639ee]225} // DeclarationNode::newComplexType
226
227DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
[ba7aa2d]228 DeclarationNode * newnode = new DeclarationNode;
[5b639ee]229 newnode->type = new TypeData( TypeData::Basic );
230 newnode->type->signedness = sn;
231 return newnode;
232} // DeclarationNode::newSignedNess
233
234DeclarationNode * DeclarationNode::newLength( Length lnth ) {
[ba7aa2d]235 DeclarationNode * newnode = new DeclarationNode;
[5b639ee]236 newnode->type = new TypeData( TypeData::Basic );
237 newnode->type->length = lnth;
238 return newnode;
239} // DeclarationNode::newLength
[3848e0e]240
[dd020c0]241DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
242 DeclarationNode * newnode = new DeclarationNode;
243 newnode->type = new TypeData( TypeData::Unknown );
244 newnode->type->forall = forall;
245 return newnode;
246} // DeclarationNode::newForall
247
[fb114fa1]248DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
[ba7aa2d]249 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]250 newnode->type = new TypeData( TypeData::SymbolicInst );
[fb114fa1]251 newnode->type->symbolic.name = name;
[8f6f47d7]252 newnode->type->symbolic.isTypedef = true;
[2298f728]253 newnode->type->symbolic.params = nullptr;
[b87a5ed]254 return newnode;
[984dce6]255} // DeclarationNode::newFromTypedef
[3848e0e]256
[fb114fa1]257DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
[ba7aa2d]258 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]259 newnode->type = new TypeData( TypeData::Aggregate );
[8f6f47d7]260 newnode->type->aggregate.kind = kind;
[2298f728]261 if ( name ) {
[fb114fa1]262 newnode->type->aggregate.name = name;
[2298f728]263 } else { // anonymous aggregate ?
264 newnode->type->aggregate.name = new string( anonymous.newName() );
[8f6f47d7]265 } // if
266 newnode->type->aggregate.actuals = actuals;
267 newnode->type->aggregate.fields = fields;
268 newnode->type->aggregate.body = body;
[b87a5ed]269 return newnode;
[984dce6]270} // DeclarationNode::newAggregate
[3848e0e]271
[ca1a547]272DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants, bool body ) {
[ba7aa2d]273 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]274 newnode->type = new TypeData( TypeData::Enum );
[2298f728]275 if ( name ) {
[fb114fa1]276 newnode->type->enumeration.name = name;
[2298f728]277 } else { // anonymous aggregate ?
278 newnode->type->enumeration.name = new string( anonymous.newName() );
[68cd1ce]279 } // if
[8f6f47d7]280 newnode->type->enumeration.constants = constants;
[ca1a547]281 newnode->type->enumeration.body = body;
[b87a5ed]282 return newnode;
[984dce6]283} // DeclarationNode::newEnum
[3848e0e]284
[fb114fa1]285DeclarationNode * DeclarationNode::newEnumConstant( string * name, ExpressionNode * constant ) {
[ba7aa2d]286 DeclarationNode * newnode = new DeclarationNode;
[2298f728]287 newnode->name = name;
[4f147cc]288 newnode->enumeratorValue.reset( constant );
[2298f728]289 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
[b87a5ed]290 return newnode;
[984dce6]291} // DeclarationNode::newEnumConstant
[3848e0e]292
[fb114fa1]293DeclarationNode * DeclarationNode::newName( string * name ) {
[ba7aa2d]294 DeclarationNode * newnode = new DeclarationNode;
[2298f728]295 newnode->name = name;
[b87a5ed]296 return newnode;
[984dce6]297} // DeclarationNode::newName
[3848e0e]298
[fb114fa1]299DeclarationNode * DeclarationNode::newFromTypeGen( string * name, ExpressionNode * params ) {
[ba7aa2d]300 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]301 newnode->type = new TypeData( TypeData::SymbolicInst );
[fb114fa1]302 newnode->type->symbolic.name = name;
[8f6f47d7]303 newnode->type->symbolic.isTypedef = false;
304 newnode->type->symbolic.actuals = params;
[b87a5ed]305 return newnode;
[984dce6]306} // DeclarationNode::newFromTypeGen
[3848e0e]307
[fb114fa1]308DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, string * name ) {
[ba7aa2d]309 DeclarationNode * newnode = new DeclarationNode;
[2298f728]310 newnode->type = nullptr;
[148f7290]311 assert( ! newnode->name );
[faddbd8]312// newnode->variable.name = name;
313 newnode->name = name;
[28307be]314 newnode->variable.tyClass = tc;
[faddbd8]315 newnode->variable.assertions = nullptr;
[b87a5ed]316 return newnode;
[984dce6]317} // DeclarationNode::newTypeParam
[3848e0e]318
[fb114fa1]319DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
[ba7aa2d]320 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]321 newnode->type = new TypeData( TypeData::Aggregate );
[2298f728]322 newnode->type->aggregate.name = name;
[8f6f47d7]323 newnode->type->aggregate.kind = Trait;
324 newnode->type->aggregate.params = params;
325 newnode->type->aggregate.fields = asserts;
[b87a5ed]326 return newnode;
[984dce6]327} // DeclarationNode::newTrait
[3848e0e]328
[fb114fa1]329DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
[ba7aa2d]330 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]331 newnode->type = new TypeData( TypeData::AggregateInst );
[8f6f47d7]332 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
333 newnode->type->aggInst.aggregate->aggregate.kind = Trait;
[2298f728]334 newnode->type->aggInst.aggregate->aggregate.name = name;
[8f6f47d7]335 newnode->type->aggInst.params = params;
[b87a5ed]336 return newnode;
[984dce6]337} // DeclarationNode::newTraitUse
[3848e0e]338
[fb114fa1]339DeclarationNode * DeclarationNode::newTypeDecl( string * name, DeclarationNode * typeParams ) {
[ba7aa2d]340 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]341 newnode->type = new TypeData( TypeData::Symbolic );
[8f6f47d7]342 newnode->type->symbolic.isTypedef = false;
343 newnode->type->symbolic.params = typeParams;
[fb114fa1]344 newnode->type->symbolic.name = name;
[b87a5ed]345 return newnode;
[984dce6]346} // DeclarationNode::newTypeDecl
[3848e0e]347
[ba7aa2d]348DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers ) {
349 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]350 newnode->type = new TypeData( TypeData::Pointer );
351 return newnode->addQualifiers( qualifiers );
[984dce6]352} // DeclarationNode::newPointer
[3848e0e]353
[ba7aa2d]354DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
355 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]356 newnode->type = new TypeData( TypeData::Array );
[8f6f47d7]357 newnode->type->array.dimension = size;
358 newnode->type->array.isStatic = isStatic;
[2298f728]359 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
[8f6f47d7]360 newnode->type->array.isVarLen = false;
[71bd8c6]361 } else {
[8f6f47d7]362 newnode->type->array.isVarLen = true;
[71bd8c6]363 } // if
[b87a5ed]364 return newnode->addQualifiers( qualifiers );
[984dce6]365} // DeclarationNode::newArray
[3848e0e]366
[ba7aa2d]367DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
368 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]369 newnode->type = new TypeData( TypeData::Array );
[2298f728]370 newnode->type->array.dimension = nullptr;
[8f6f47d7]371 newnode->type->array.isStatic = false;
372 newnode->type->array.isVarLen = true;
[b87a5ed]373 return newnode->addQualifiers( qualifiers );
[3848e0e]374}
375
[ba7aa2d]376DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
377 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]378 newnode->bitfieldWidth = size;
379 return newnode;
[3848e0e]380}
381
[ba7aa2d]382DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
383 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]384 newnode->type = new TypeData( TypeData::Tuple );
[8f6f47d7]385 newnode->type->tuple = members;
[b87a5ed]386 return newnode;
[3848e0e]387}
388
[ba7aa2d]389DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
390 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]391 newnode->type = new TypeData( TypeData::Typeof );
[8f6f47d7]392 newnode->type->typeexpr = expr;
[b87a5ed]393 return newnode;
[3848e0e]394}
395
[8f6f47d7]396DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
[ba7aa2d]397 DeclarationNode * newnode = new DeclarationNode;
[8f6f47d7]398 newnode->type = new TypeData( TypeData::Builtin );
399 newnode->builtin = bt;
[148f7290]400 newnode->type->builtintype = newnode->builtin;
[8f6f47d7]401 return newnode;
402} // DeclarationNode::newBuiltinType
403
[fb114fa1]404DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) {
[ba7aa2d]405 DeclarationNode * newnode = new DeclarationNode;
[2298f728]406 newnode->type = nullptr;
[faddbd8]407// newnode->attr.name = name;
408 newnode->name = name;
[28307be]409 newnode->attr.expr = expr;
[b87a5ed]410 return newnode;
[3848e0e]411}
412
[fb114fa1]413DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) {
[ba7aa2d]414 DeclarationNode * newnode = new DeclarationNode;
[2298f728]415 newnode->type = nullptr;
[faddbd8]416// newnode->attr.name = name;
417 newnode->name = name;
[28307be]418 newnode->attr.type = type;
[b87a5ed]419 return newnode;
[3848e0e]420}
421
[44a81853]422DeclarationNode * DeclarationNode::newAttribute( string * name, ExpressionNode * expr ) {
423 DeclarationNode * newnode = new DeclarationNode;
424 newnode->type = nullptr;
425 std::list< Expression * > exprs;
426 buildList( expr, exprs );
427 newnode->attributes.push_back( new Attribute( *name, exprs ) );
428 delete name;
429 return newnode;
430}
431
[e994912]432DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
433 DeclarationNode * newnode = new DeclarationNode;
434 newnode->asmStmt = stmt;
435 return newnode;
436}
437
[5b639ee]438void appendError( string & dst, const string & src ) {
439 if ( src.empty() ) return;
440 if ( dst.empty() ) { dst = src; return; }
441 dst += ", " + src;
442} // appendError
443
[ba7aa2d]444void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
[dd020c0]445 const TypeData::TypeQualifiers &qsrc = src->typeQualifiers, &qdst = dst->typeQualifiers; // optimization
[c1c1112]446
[dd020c0]447 if ( (qsrc & qdst).any() ) { // common qualifier ?
448 for ( unsigned int i = 0; i < NoTypeQualifier; i += 1 ) { // find common qualifiers
[5b639ee]449 if ( qsrc[i] && qdst[i] ) {
[dd020c0]450 appendError( error, string( "duplicate " ) + DeclarationNode::typeQualifierNames[i] );
[c1c1112]451 } // if
452 } // for
453 } // if
454} // DeclarationNode::checkQualifiers
455
[ba7aa2d]456void DeclarationNode::checkStorageClasses( DeclarationNode * q ) {
[dd020c0]457 const FuncSpec &src = funcSpec, &dst = q->funcSpec; // optimization
458 if ( (src & dst).any() ) { // common specifier ?
459 for ( unsigned int i = 0; i < NoFuncSpecifier; i += 1 ) { // find common specifier
460 if ( src[i] && dst[i] ) {
461 appendError( error, string( "duplicate " ) + DeclarationNode::funcSpecifierNames[i] );
462 } // if
463 } // for
464 } // if
465
[b6424d9]466 if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
467 if ( storageClass == q->storageClass ) { // duplicate qualifier
[dd020c0]468 appendError( error, string( "duplicate " ) + storageClassNames[ storageClass ] );
[b6424d9]469 } else { // only one storage class
[dd020c0]470 appendError( error, string( "conflicting " ) + storageClassNames[ storageClass ] + " & " + storageClassNames[ q->storageClass ] );
[5b639ee]471 q->storageClass = storageClass; // FIX ERROR, prevent assertions from triggering
[b6424d9]472 } // if
473 } // if
[dd020c0]474
[5b639ee]475 appendError( error, q->error );
[c0aa336]476} // DeclarationNode::checkStorageClasses
[b6424d9]477
[ba7aa2d]478DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) {
[dd020c0]479 funcSpec = funcSpec | q->funcSpec;
480
[b6424d9]481 // do not overwrite an existing value with NoStorageClass
482 if ( q->storageClass != NoStorageClass ) {
483 assert( storageClass == NoStorageClass || storageClass == q->storageClass );
484 storageClass = q->storageClass;
485 } // if
[c0aa336]486
487 for ( Attribute *attr: reverseIterate( q->attributes ) ) {
488 attributes.push_front( attr->clone() );
489 } // for
[b6424d9]490 return this;
491} // DeclarationNode::copyStorageClasses
492
[ba7aa2d]493static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
[101e0bd]494 if ( src->forall && dst->kind == TypeData::Function ) {
495 if ( dst->forall ) {
496 dst->forall->appendList( src->forall );
497 } else {
498 dst->forall = src->forall;
499 } // if
[2298f728]500 src->forall = nullptr;
[101e0bd]501 } // if
502 if ( dst->base ) {
503 addQualifiersToType( src, dst->base );
504 } else if ( dst->kind == TypeData::Function ) {
505 dst->base = src;
[2298f728]506 src = nullptr;
[101e0bd]507 } else {
[dd020c0]508 dst->typeQualifiers |= src->typeQualifiers;
[101e0bd]509 } // if
510} // addQualifiersToType
511
[ba7aa2d]512DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
[2298f728]513 if ( ! q ) { delete q; return this; }
[101e0bd]514
515 checkStorageClasses( q );
516 copyStorageClasses( q );
517
[1b772749]518 if ( ! q->type ) {
519 delete q;
520 return this;
521 } // if
[101e0bd]522
523 if ( ! type ) {
[1b772749]524 type = q->type; // reuse this structure
525 q->type = nullptr;
526 delete q;
[101e0bd]527 return this;
528 } // if
529
530 if ( q->type->forall ) {
531 if ( type->forall ) {
532 type->forall->appendList( q->type->forall );
533 } else {
534 if ( type->kind == TypeData::Aggregate ) {
535 type->aggregate.params = q->type->forall;
536 // change implicit typedef from TYPEDEFname to TYPEGENname
[2298f728]537 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG );
[c1c1112]538 } else {
[101e0bd]539 type->forall = q->type->forall;
[68cd1ce]540 } // if
541 } // if
[2298f728]542 q->type->forall = nullptr;
[68cd1ce]543 } // if
[6ef2d81]544
545 checkQualifiers( q->type, type );
546 addQualifiersToType( q->type, type );
547
[b87a5ed]548 delete q;
549 return this;
[101e0bd]550} // addQualifiers
[3848e0e]551
552static void addTypeToType( TypeData *&src, TypeData *&dst ) {
[101e0bd]553 if ( src->forall && dst->kind == TypeData::Function ) {
554 if ( dst->forall ) {
555 dst->forall->appendList( src->forall );
[b87a5ed]556 } else {
[101e0bd]557 dst->forall = src->forall;
558 } // if
[2298f728]559 src->forall = nullptr;
[101e0bd]560 } // if
561 if ( dst->base ) {
562 addTypeToType( src, dst->base );
563 } else {
564 switch ( dst->kind ) {
565 case TypeData::Unknown:
[dd020c0]566 src->typeQualifiers |= dst->typeQualifiers;
[101e0bd]567 dst = src;
[2298f728]568 src = nullptr;
[101e0bd]569 break;
570 case TypeData::Basic:
[dd020c0]571 dst->typeQualifiers |= src->typeQualifiers;
[101e0bd]572 if ( src->kind != TypeData::Unknown ) {
573 assert( src->kind == TypeData::Basic );
574
575 if ( dst->basictype == DeclarationNode::NoBasicType ) {
576 dst->basictype = src->basictype;
577 } else if ( src->basictype != DeclarationNode::NoBasicType )
[dd020c0]578 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: ", src );
[101e0bd]579
580 if ( dst->complextype == DeclarationNode::NoComplexType ) {
581 dst->complextype = src->complextype;
582 } else if ( src->complextype != DeclarationNode::NoComplexType )
[dd020c0]583 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: ", src );
[101e0bd]584
585 if ( dst->signedness == DeclarationNode::NoSignedness ) {
586 dst->signedness = src->signedness;
587 } else if ( src->signedness != DeclarationNode::NoSignedness )
[dd020c0]588 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: ", src );
[101e0bd]589
590 if ( dst->length == DeclarationNode::NoLength ) {
591 dst->length = src->length;
592 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
593 dst->length = DeclarationNode::LongLong;
594 } else if ( src->length != DeclarationNode::NoLength )
[dd020c0]595 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: ", src );
[101e0bd]596 } // if
597 break;
598 default:
599 switch ( src->kind ) {
600 case TypeData::Aggregate:
601 case TypeData::Enum:
602 dst->base = new TypeData( TypeData::AggregateInst );
603 dst->base->aggInst.aggregate = src;
604 if ( src->kind == TypeData::Aggregate ) {
605 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
[68cd1ce]606 } // if
[dd020c0]607 dst->base->typeQualifiers |= src->typeQualifiers;
[2298f728]608 src = nullptr;
[b87a5ed]609 break;
610 default:
[101e0bd]611 if ( dst->forall ) {
612 dst->forall->appendList( src->forall );
613 } else {
614 dst->forall = src->forall;
615 } // if
[2298f728]616 src->forall = nullptr;
[101e0bd]617 dst->base = src;
[2298f728]618 src = nullptr;
[68cd1ce]619 } // switch
[101e0bd]620 } // switch
[68cd1ce]621 } // if
[3848e0e]622}
623
[ba7aa2d]624DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
[b87a5ed]625 if ( o ) {
[b6424d9]626 checkStorageClasses( o );
[13e3b50]627 copyStorageClasses( o );
[b87a5ed]628 if ( o->type ) {
629 if ( ! type ) {
630 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
631 type = new TypeData( TypeData::AggregateInst );
[8f6f47d7]632 type->aggInst.aggregate = o->type;
[b87a5ed]633 if ( o->type->kind == TypeData::Aggregate ) {
[43c89a7]634 type->aggInst.hoistType = o->type->aggregate.body;
[8f6f47d7]635 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
[43c89a7]636 } else {
637 type->aggInst.hoistType = o->type->enumeration.body;
[68cd1ce]638 } // if
[dd020c0]639 type->typeQualifiers |= o->type->typeQualifiers;
[b87a5ed]640 } else {
641 type = o->type;
[68cd1ce]642 } // if
[2298f728]643 o->type = nullptr;
[b87a5ed]644 } else {
645 addTypeToType( o->type, type );
[68cd1ce]646 } // if
647 } // if
[b87a5ed]648 if ( o->bitfieldWidth ) {
649 bitfieldWidth = o->bitfieldWidth;
[68cd1ce]650 } // if
[71bd8c6]651
652 // there may be typedefs chained onto the type
[1d4580a]653 if ( o->get_next() ) {
654 set_last( o->get_next()->clone() );
[1db21619]655 } // if
[68cd1ce]656 } // if
[b87a5ed]657 delete o;
658 return this;
[3848e0e]659}
660
[ba7aa2d]661DeclarationNode * DeclarationNode::addTypedef() {
662 TypeData * newtype = new TypeData( TypeData::Symbolic );
[2298f728]663 newtype->symbolic.params = nullptr;
[8f6f47d7]664 newtype->symbolic.isTypedef = true;
[2298f728]665 newtype->symbolic.name = name ? new string( *name ) : nullptr;
[b87a5ed]666 newtype->base = type;
667 type = newtype;
668 return this;
[3848e0e]669}
670
[ba7aa2d]671DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
[faddbd8]672 if ( variable.tyClass != NoTypeClass ) {
[2298f728]673 if ( variable.assertions ) {
674 variable.assertions->appendList( assertions );
675 } else {
676 variable.assertions = assertions;
677 } // if
678 return this;
679 } // if
680
[b87a5ed]681 assert( type );
682 switch ( type->kind ) {
683 case TypeData::Symbolic:
[8f6f47d7]684 if ( type->symbolic.assertions ) {
685 type->symbolic.assertions->appendList( assertions );
[b87a5ed]686 } else {
[8f6f47d7]687 type->symbolic.assertions = assertions;
[68cd1ce]688 } // if
[b87a5ed]689 break;
690 default:
691 assert( false );
[68cd1ce]692 } // switch
[974906e2]693
[b87a5ed]694 return this;
[51b73452]695}
696
[fb114fa1]697DeclarationNode * DeclarationNode::addName( string * newname ) {
[2298f728]698 assert( ! name );
699 name = newname;
[b87a5ed]700 return this;
[51b73452]701}
702
[c0aa336]703DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
[58dd019]704 assert( ! asmName );
[c0aa336]705 asmName = newname ? newname->asmName : nullptr;
706 return this->addQualifiers( newname );
[58dd019]707}
708
[ba7aa2d]709DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
[b87a5ed]710 bitfieldWidth = size;
711 return this;
[51b73452]712}
713
[ba7aa2d]714DeclarationNode * DeclarationNode::addVarArgs() {
[b87a5ed]715 assert( type );
716 hasEllipsis = true;
717 return this;
[51b73452]718}
719
[ba7aa2d]720DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body ) {
[b87a5ed]721 assert( type );
722 assert( type->kind == TypeData::Function );
[2298f728]723 assert( ! type->function.body );
[8f6f47d7]724 type->function.body = body;
[b87a5ed]725 return this;
[51b73452]726}
727
[ba7aa2d]728DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
[b87a5ed]729 assert( type );
730 assert( type->kind == TypeData::Function );
[2298f728]731 assert( ! type->function.oldDeclList );
[8f6f47d7]732 type->function.oldDeclList = list;
[b87a5ed]733 return this;
[51b73452]734}
735
[c0aa336]736DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
[b87a5ed]737 if ( type ) {
[ba7aa2d]738 TypeData * prevBase = type;
739 TypeData * curBase = type->base;
[2298f728]740 while ( curBase != nullptr ) {
[b87a5ed]741 prevBase = curBase;
742 curBase = curBase->base;
[68cd1ce]743 } // while
[b87a5ed]744 prevBase->base = newType;
745 } else {
746 type = newType;
[68cd1ce]747 } // if
[c0aa336]748 return this;
[3848e0e]749}
750
[c0aa336]751DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
752 if ( a ) {
753 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
754 attributes.push_front( attr );
755 } // for
756 a->attributes.clear();
757 } // if
758 return this;
759} // copyAttribute
760
[ba7aa2d]761DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
[b87a5ed]762 if ( p ) {
763 assert( p->type->kind == TypeData::Pointer );
[c0aa336]764 setBase( p->type );
[2298f728]765 p->type = nullptr;
[c0aa336]766 copyAttribute( p );
[b87a5ed]767 delete p;
[68cd1ce]768 } // if
[b87a5ed]769 return this;
[3848e0e]770}
771
[ba7aa2d]772DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
[b87a5ed]773 if ( a ) {
774 assert( a->type->kind == TypeData::Array );
[c0aa336]775 setBase( a->type );
[2298f728]776 a->type = nullptr;
[c0aa336]777 copyAttribute( a );
[b87a5ed]778 delete a;
[68cd1ce]779 } // if
[b87a5ed]780 return this;
[51b73452]781}
782
[ba7aa2d]783DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
[b87a5ed]784 if ( p ) {
785 assert( p->type->kind == TypeData::Pointer );
786 if ( type ) {
787 switch ( type->kind ) {
788 case TypeData::Aggregate:
789 case TypeData::Enum:
790 p->type->base = new TypeData( TypeData::AggregateInst );
[8f6f47d7]791 p->type->base->aggInst.aggregate = type;
[b87a5ed]792 if ( type->kind == TypeData::Aggregate ) {
[8f6f47d7]793 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
[68cd1ce]794 } // if
[dd020c0]795 p->type->base->typeQualifiers |= type->typeQualifiers;
[b87a5ed]796 break;
797
798 default:
799 p->type->base = type;
[68cd1ce]800 } // switch
[2298f728]801 type = nullptr;
[68cd1ce]802 } // if
[b87a5ed]803 delete this;
804 return p;
805 } else {
806 return this;
[68cd1ce]807 } // if
[51b73452]808}
809
[ba7aa2d]810static TypeData * findLast( TypeData * a ) {
[b87a5ed]811 assert( a );
[ba7aa2d]812 TypeData * cur = a;
[a32b204]813 while ( cur->base ) {
[b87a5ed]814 cur = cur->base;
[68cd1ce]815 } // while
[b87a5ed]816 return cur;
[3848e0e]817}
818
[ba7aa2d]819DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
[b87a5ed]820 if ( a ) {
821 assert( a->type->kind == TypeData::Array );
[ba7aa2d]822 TypeData * lastArray = findLast( a->type );
[974906e2]823 if ( type ) {
[b87a5ed]824 switch ( type->kind ) {
825 case TypeData::Aggregate:
826 case TypeData::Enum:
827 lastArray->base = new TypeData( TypeData::AggregateInst );
[8f6f47d7]828 lastArray->base->aggInst.aggregate = type;
[b87a5ed]829 if ( type->kind == TypeData::Aggregate ) {
[8f6f47d7]830 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
[68cd1ce]831 } // if
[dd020c0]832 lastArray->base->typeQualifiers |= type->typeQualifiers;
[b87a5ed]833 break;
834 default:
835 lastArray->base = type;
[68cd1ce]836 } // switch
[2298f728]837 type = nullptr;
[68cd1ce]838 } // if
[b87a5ed]839 delete this;
840 return a;
841 } else {
842 return this;
[68cd1ce]843 } // if
[51b73452]844}
[3848e0e]845
[ba7aa2d]846DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
847 TypeData * ftype = new TypeData( TypeData::Function );
[8f6f47d7]848 ftype->function.params = params;
[c0aa336]849 setBase( ftype );
[b87a5ed]850 return this;
[3848e0e]851}
852
[ba7aa2d]853static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
[b87a5ed]854 if ( type ) {
855 if ( type->kind != TypeData::Function ) {
856 type->base = addIdListToType( type->base, ids );
857 } else {
[8f6f47d7]858 type->function.idList = ids;
[68cd1ce]859 } // if
[b87a5ed]860 return type;
[3848e0e]861 } else {
[ba7aa2d]862 TypeData * newtype = new TypeData( TypeData::Function );
[8f6f47d7]863 newtype->function.idList = ids;
[b87a5ed]864 return newtype;
[68cd1ce]865 } // if
[2298f728]866} // addIdListToType
[974906e2]867
[ba7aa2d]868DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
[b87a5ed]869 type = addIdListToType( type, ids );
870 return this;
[3848e0e]871}
872
[ba7aa2d]873DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
[b87a5ed]874 initializer = init;
875 return this;
[3848e0e]876}
877
[ba7aa2d]878DeclarationNode * DeclarationNode::cloneType( string * newName ) {
879 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]880 newnode->type = maybeClone( type );
[b6424d9]881 assert( storageClass == NoStorageClass );
[13e3b50]882 newnode->copyStorageClasses( this );
[ba7aa2d]883 assert( newName );
[2298f728]884 newnode->name = newName;
[b87a5ed]885 return newnode;
[3848e0e]886}
887
[2298f728]888DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
889 if ( ! o ) return nullptr;
890
891 o->copyStorageClasses( this );
892 if ( type ) {
893 TypeData * srcType = type;
894
[c0aa336]895 // search for the base type by scanning off pointers and array designators
[2298f728]896 while ( srcType->base ) {
897 srcType = srcType->base;
898 } // while
899
900 TypeData * newType = srcType->clone();
901 if ( newType->kind == TypeData::AggregateInst ) {
902 // don't duplicate members
903 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
904 delete newType->aggInst.aggregate->enumeration.constants;
905 newType->aggInst.aggregate->enumeration.constants = nullptr;
[b87a5ed]906 } else {
[2298f728]907 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
908 delete newType->aggInst.aggregate->aggregate.fields;
909 newType->aggInst.aggregate->aggregate.fields = nullptr;
[68cd1ce]910 } // if
[43c89a7]911 // don't hoist twice
912 newType->aggInst.hoistType = false;
[68cd1ce]913 } // if
[2298f728]914
915 newType->forall = maybeClone( type->forall );
916 if ( ! o->type ) {
917 o->type = newType;
918 } else {
919 addTypeToType( newType, o->type );
920 delete newType;
921 } // if
[68cd1ce]922 } // if
[b87a5ed]923 return o;
[51b73452]924}
925
[ba7aa2d]926DeclarationNode * DeclarationNode::extractAggregate() const {
[b87a5ed]927 if ( type ) {
[ba7aa2d]928 TypeData * ret = typeextractAggregate( type );
[b87a5ed]929 if ( ret ) {
[ba7aa2d]930 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]931 newnode->type = ret;
932 return newnode;
[843054c2]933 } // if
934 } // if
[2298f728]935 return nullptr;
[3848e0e]936}
937
[ba7aa2d]938void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
[b87a5ed]939 SemanticError errors;
[7880579]940 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
[2298f728]941
[3a5131ed]942 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
[b87a5ed]943 try {
[ba7aa2d]944 if ( DeclarationNode * extr = cur->extractAggregate() ) {
[b87a5ed]945 // handle the case where a structure declaration is contained within an object or type declaration
[ba7aa2d]946 Declaration * decl = extr->build();
[b87a5ed]947 if ( decl ) {
[294647b]948 decl->location = cur->location;
[ba7aa2d]949 * out++ = decl;
[843054c2]950 } // if
[f39096c]951 delete extr;
[843054c2]952 } // if
[2298f728]953
[ba7aa2d]954 Declaration * decl = cur->build();
[b87a5ed]955 if ( decl ) {
[294647b]956 decl->location = cur->location;
[ba7aa2d]957 * out++ = decl;
[843054c2]958 } // if
[b87a5ed]959 } catch( SemanticError &e ) {
[294647b]960 e.set_location( cur->location );
[b87a5ed]961 errors.append( e );
[843054c2]962 } // try
963 } // while
[2298f728]964
[b87a5ed]965 if ( ! errors.isEmpty() ) {
966 throw errors;
[843054c2]967 } // if
[2298f728]968} // buildList
[3848e0e]969
[ba7aa2d]970void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
[b87a5ed]971 SemanticError errors;
[7880579]972 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
[43c89a7]973
[3a5131ed]974 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
[b87a5ed]975 try {
[ba7aa2d]976 Declaration * decl = cur->build();
[b87a5ed]977 if ( decl ) {
[ba7aa2d]978 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
[294647b]979 dwt->location = cur->location;
[ba7aa2d]980 * out++ = dwt;
981 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
982 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
[294647b]983 auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
984 obj->location = cur->location;
[43c89a7]985 * out++ = obj;
[b87a5ed]986 delete agg;
[ba7aa2d]987 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
988 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
[294647b]989 auto obj = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
990 obj->location = cur->location;
991 * out++ = obj;
[843054c2]992 } // if
993 } // if
[b87a5ed]994 } catch( SemanticError &e ) {
[294647b]995 e.set_location( cur->location );
[b87a5ed]996 errors.append( e );
[843054c2]997 } // try
[3a5131ed]998 } // for
999
[b87a5ed]1000 if ( ! errors.isEmpty() ) {
1001 throw errors;
[843054c2]1002 } // if
[2298f728]1003} // buildList
[3848e0e]1004
[ba7aa2d]1005void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
[b87a5ed]1006 SemanticError errors;
[7880579]1007 std::back_insert_iterator< std::list< Type * > > out( outputList );
[ba7aa2d]1008 const DeclarationNode * cur = firstNode;
[2298f728]1009
[a32b204]1010 while ( cur ) {
[b87a5ed]1011 try {
[ba7aa2d]1012 * out++ = cur->buildType();
[b87a5ed]1013 } catch( SemanticError &e ) {
[138e29e]1014 e.set_location( cur->location );
[b87a5ed]1015 errors.append( e );
[843054c2]1016 } // try
[7880579]1017 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
[843054c2]1018 } // while
[2298f728]1019
[b87a5ed]1020 if ( ! errors.isEmpty() ) {
1021 throw errors;
[843054c2]1022 } // if
[2298f728]1023} // buildTypeList
[51b73452]1024
[ba7aa2d]1025Declaration * DeclarationNode::build() const {
[7d05e7e]1026 if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this );
[2298f728]1027
[e994912]1028 if ( asmStmt ) {
1029 return new AsmDecl( safe_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1030 } // if
1031
[faddbd8]1032 if ( variable.tyClass != NoTypeClass ) {
[8f60f0b]1033 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1034 assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." );
1035 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
[faddbd8]1036 TypeDecl * ret = new TypeDecl( *name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
[2298f728]1037 buildList( variable.assertions, ret->get_assertions() );
1038 return ret;
1039 } // if
1040
[843054c2]1041 if ( type ) {
[dd020c0]1042 // Function specifiers can only appear on a function definition/declaration.
1043 //
1044 // inline _Noreturn int f(); // allowed
1045 // inline _Noreturn int g( int i ); // allowed
1046 // inline _Noreturn int i; // disallowed
1047 if ( type->kind != TypeData::Function && funcSpec.any() ) {
1048 throw SemanticError( "invalid function specifier for ", this );
1049 } // if
1050 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), funcSpec, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
[843054c2]1051 } // if
[2298f728]1052
[dd020c0]1053 // SUE's cannot have function specifiers, either
1054 //
1055 // inlne _Noreturn struct S { ... }; // disallowed
1056 // inlne _Noreturn enum E { ... }; // disallowed
1057 if ( funcSpec.any() ) {
1058 throw SemanticError( "invalid function specifier for ", this );
[843054c2]1059 } // if
[dd020c0]1060 assertf( name, "ObjectDecl must a have name\n" );
1061 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
[51b73452]1062}
1063
[ba7aa2d]1064Type * DeclarationNode::buildType() const {
[b87a5ed]1065 assert( type );
[974906e2]1066
[faddbd8]1067 if ( attr.expr ) {
[c0aa336]1068 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
[faddbd8]1069 } else if ( attr.type ) {
[c0aa336]1070 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
[2298f728]1071 } // if
1072
[b87a5ed]1073 switch ( type->kind ) {
[43c89a7]1074 case TypeData::Enum:
[b87a5ed]1075 case TypeData::Aggregate: {
[43c89a7]1076 ReferenceToType * ret = buildComAggInst( type, attributes );
[8f6f47d7]1077 buildList( type->aggregate.actuals, ret->get_parameters() );
[b87a5ed]1078 return ret;
1079 }
1080 case TypeData::Symbolic: {
[c0aa336]1081 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
[8f6f47d7]1082 buildList( type->symbolic.actuals, ret->get_parameters() );
[b87a5ed]1083 return ret;
1084 }
1085 default:
[c0aa336]1086 Type * simpletypes = typebuild( type );
1087 simpletypes->get_attributes() = attributes; // copy because member is const
1088 return simpletypes;
[b87a5ed]1089 } // switch
[3848e0e]1090}
1091
[b87a5ed]1092// Local Variables: //
1093// tab-width: 4 //
1094// mode: c++ //
1095// compile-command: "make install" //
1096// End: //
Note: See TracBrowser for help on using the repository browser.