source: src/Parser/DeclarationNode.cc@ 77de429

ADT ast-experimental
Last change on this file since 77de429 was 1e30df7, checked in by JiadaL <j82liang@…>, 3 years ago

Supports inline enums

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