source: src/Parser/DeclarationNode.cc@ 6fa409e

new-env
Last change on this file since 6fa409e was 28f3a19, checked in by Aaron Moss <a3moss@…>, 7 years ago

Merge branch 'master' into with_gc

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