source: src/Parser/DeclarationNode.cc@ e9a7e90b

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 with_gc
Last change on this file since e9a7e90b was af9da5f, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

fix warning about cv-qualifiers on zero/one

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