source: src/Parser/DeclarationNode.cc@ 1fbab5a

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 1fbab5a was fb04321, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more cleanup for bit-field type usage

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