source: src/Parser/DeclarationNode.cc@ 360b2e13

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 360b2e13 was ba01b14, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

update constant parsing add _FloatNN

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