source: src/Parser/DeclarationNode.cc@ fa2c005

ADT
Last change on this file since fa2c005 was fa2c005, checked in by JiadaL <j82liang@…>, 3 years ago

Finish Adt POC

  • Property mode set to 100644
File size: 48.3 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
[0d0931d]11// Last Modified By : Andrew Beach
[45e753c]12// Last Modified On : Thr Apr 20 11:46:00 2023
13// Update Count : 1393
[b87a5ed]14//
15
[c468150]16#include "DeclarationNode.h"
17
[e3e16bc]18#include <cassert> // for assert, assertf, strict_dynamic_cast
[d180746]19#include <iterator> // for back_insert_iterator
20#include <list> // for list
21#include <memory> // for unique_ptr
22#include <ostream> // for operator<<, ostream, basic_ostream
23#include <string> // for string, operator+, allocator, char...
[51b73452]24
[bb7422a]25#include "AST/Attribute.hpp" // for Attribute
26#include "AST/Copy.hpp" // for shallowCopy
27#include "AST/Decl.hpp" // for Decl
28#include "AST/Expr.hpp" // for Expr
29#include "AST/Print.hpp" // for print
30#include "AST/Stmt.hpp" // for AsmStmt, DirectiveStmt
31#include "AST/StorageClasses.hpp" // for Storage::Class
32#include "AST/Type.hpp" // for Type
33#include "Common/CodeLocation.h" // for CodeLocation
34#include "Common/Iterate.hpp" // for reverseIterate
[d180746]35#include "Common/SemanticError.h" // for SemanticError
36#include "Common/UniqueName.h" // for UniqueName
[bb7422a]37#include "Common/utility.h" // for maybeClone
[c468150]38#include "Parser/ExpressionNode.h" // for ExpressionNode
39#include "Parser/InitializerNode.h"// for InitializerNode
40#include "Parser/StatementNode.h" // for StatementNode
[d180746]41#include "TypeData.h" // for TypeData, TypeData::Aggregate_t
[2f0a0678]42#include "TypedefTable.h" // for TypedefTable
[bdd516a]43
[d180746]44class Initializer;
[51b73452]45
[de62360d]46extern TypedefTable typedefTable;
47
[51b73452]48using namespace std;
49
[201aeb9]50// These must harmonize with the corresponding DeclarationNode enumerations.
[702e826]51const char * DeclarationNode::basicTypeNames[] = {
52 "void", "_Bool", "char", "int", "int128",
53 "float", "double", "long double", "float80", "float128",
54 "_float16", "_float32", "_float32x", "_float64", "_float64x", "_float128", "_float128x", "NoBasicTypeNames"
55};
56const char * DeclarationNode::complexTypeNames[] = {
57 "_Complex", "NoComplexTypeNames", "_Imaginary"
58}; // Imaginary unsupported => parse, but make invisible and print error message
59const char * DeclarationNode::signednessNames[] = {
60 "signed", "unsigned", "NoSignednessNames"
61};
62const char * DeclarationNode::lengthNames[] = {
63 "short", "long", "long long", "NoLengthNames"
64};
65const char * DeclarationNode::builtinTypeNames[] = {
66 "__builtin_va_list", "__auto_type", "zero_t", "one_t", "NoBuiltinTypeNames"
67};
[51b73452]68
69UniqueName DeclarationNode::anonymous( "__anonymous" );
70
[bb7422a]71extern ast::Linkage::Spec linkage; // defined in parser.yy
[51b73452]72
[7d05e7e]73DeclarationNode::DeclarationNode() :
[e07caa2]74 linkage( ::linkage ) {
[2298f728]75
[faddbd8]76// variable.name = nullptr;
[bb7422a]77 variable.tyClass = ast::TypeDecl::NUMBER_OF_KINDS;
[28307be]78 variable.assertions = nullptr;
[67cf18c]79 variable.initializer = nullptr;
[7d05e7e]80
[f6e3e34]81 assert.condition = nullptr;
82 assert.message = nullptr;
[28307be]83}
84
85DeclarationNode::~DeclarationNode() {
[faddbd8]86// delete variable.name;
[2298f728]87 delete variable.assertions;
[67cf18c]88 delete variable.initializer;
[2298f728]89
[702e826]90// delete type;
[28307be]91 delete bitfieldWidth;
[e994912]92
93 delete asmStmt;
[58dd019]94 // asmName, no delete, passed to next stage
[28307be]95 delete initializer;
[f6e3e34]96
97 delete assert.condition;
98 delete assert.message;
[28307be]99}
100
[ba7aa2d]101DeclarationNode * DeclarationNode::clone() const {
102 DeclarationNode * newnode = new DeclarationNode;
[c0aa336]103 newnode->set_next( maybeClone( get_next() ) );
[2298f728]104 newnode->name = name ? new string( *name ) : nullptr;
[c0aa336]105
[2e5fa345]106 newnode->builtin = NoBuiltinType;
[c0aa336]107 newnode->type = maybeClone( type );
[679e644]108 newnode->inLine = inLine;
[a7c90d4]109 newnode->storageClasses = storageClasses;
110 newnode->funcSpecs = funcSpecs;
[08d5507b]111 newnode->bitfieldWidth = maybeClone( bitfieldWidth );
[c0aa336]112 newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
[b87a5ed]113 newnode->hasEllipsis = hasEllipsis;
114 newnode->linkage = linkage;
[bb7422a]115 newnode->asmName = maybeCopy( asmName );
116 newnode->attributes = attributes;
[c0aa336]117 newnode->initializer = maybeClone( initializer );
118 newnode->extension = extension;
[e994912]119 newnode->asmStmt = maybeClone( asmStmt );
[c0aa336]120 newnode->error = error;
[3848e0e]121
[faddbd8]122// newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
[28307be]123 newnode->variable.tyClass = variable.tyClass;
[fb114fa1]124 newnode->variable.assertions = maybeClone( variable.assertions );
[67cf18c]125 newnode->variable.initializer = maybeClone( variable.initializer );
[3848e0e]126
[f6e3e34]127 newnode->assert.condition = maybeClone( assert.condition );
[bb7422a]128 newnode->assert.message = maybeCopy( assert.message );
[28307be]129 return newnode;
130} // DeclarationNode::clone
[3848e0e]131
[f2f512ba]132void DeclarationNode::print( std::ostream & os, int indent ) const {
[59db689]133 os << string( indent, ' ' );
[2298f728]134 if ( name ) {
135 os << *name << ": ";
[68cd1ce]136 } // if
[51b73452]137
[bb7422a]138 if ( linkage != ast::Linkage::Cforall ) {
139 os << ast::Linkage::name( linkage ) << " ";
[68cd1ce]140 } // if
[3848e0e]141
[bb7422a]142 ast::print( os, storageClasses );
143 ast::print( os, funcSpecs );
[dd020c0]144
[b87a5ed]145 if ( type ) {
146 type->print( os, indent );
147 } else {
148 os << "untyped entity ";
[68cd1ce]149 } // if
[3848e0e]150
[b87a5ed]151 if ( bitfieldWidth ) {
[59db689]152 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
[b87a5ed]153 bitfieldWidth->printOneLine( os );
[68cd1ce]154 } // if
[3848e0e]155
[2298f728]156 if ( initializer ) {
[59db689]157 os << endl << string( indent + 2, ' ' ) << "with initializer ";
[b87a5ed]158 initializer->printOneLine( os );
[974906e2]159 os << " maybe constructed? " << initializer->get_maybeConstructed();
[68cd1ce]160 } // if
[3848e0e]161
[692c1cc]162 if ( ! attributes.empty() ) {
163 os << string( indent + 2, ' ' ) << "with attributes " << endl;
[bb7422a]164 for ( ast::ptr<ast::Attribute> const & attr : reverseIterate( attributes ) ) {
[692c1cc]165 os << string( indent + 4, ' ' ) << attr->name.c_str() << endl;
166 } // for
167 } // if
[66406f3]168
[b87a5ed]169 os << endl;
[51b73452]170}
171
[f2f512ba]172void DeclarationNode::printList( std::ostream & os, int indent ) const {
[b87a5ed]173 ParseNode::printList( os, indent );
174 if ( hasEllipsis ) {
175 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
[68cd1ce]176 } // if
[51b73452]177}
178
[bb7422a]179DeclarationNode * DeclarationNode::newStorageClass( ast::Storage::Classes sc ) {
[ba7aa2d]180 DeclarationNode * newnode = new DeclarationNode;
[08d5507b]181 newnode->storageClasses = sc;
[b87a5ed]182 return newnode;
[dd020c0]183} // DeclarationNode::newStorageClass
[3848e0e]184
[bb7422a]185DeclarationNode * DeclarationNode::newFuncSpecifier( ast::Function::Specs fs ) {
[ba7aa2d]186 DeclarationNode * newnode = new DeclarationNode;
[08d5507b]187 newnode->funcSpecs = fs;
[c1c1112]188 return newnode;
[dd020c0]189} // DeclarationNode::newFuncSpecifier
[c1c1112]190
[bb7422a]191DeclarationNode * DeclarationNode::newTypeQualifier( ast::CV::Qualifiers tq ) {
[ba7aa2d]192 DeclarationNode * newnode = new DeclarationNode;
[dd020c0]193 newnode->type = new TypeData();
[738e304]194 newnode->type->qualifiers = tq;
[b87a5ed]195 return newnode;
[dd020c0]196} // DeclarationNode::newQualifier
[3848e0e]197
[c1c1112]198DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
[ba7aa2d]199 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]200 newnode->type = new TypeData( TypeData::Basic );
[5b639ee]201 newnode->type->basictype = bt;
[b87a5ed]202 return newnode;
[984dce6]203} // DeclarationNode::newBasicType
[3848e0e]204
[5b639ee]205DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
[ba7aa2d]206 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]207 newnode->type = new TypeData( TypeData::Basic );
[5b639ee]208 newnode->type->complextype = ct;
[b87a5ed]209 return newnode;
[5b639ee]210} // DeclarationNode::newComplexType
211
212DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
[ba7aa2d]213 DeclarationNode * newnode = new DeclarationNode;
[5b639ee]214 newnode->type = new TypeData( TypeData::Basic );
215 newnode->type->signedness = sn;
216 return newnode;
217} // DeclarationNode::newSignedNess
218
219DeclarationNode * DeclarationNode::newLength( Length lnth ) {
[ba7aa2d]220 DeclarationNode * newnode = new DeclarationNode;
[5b639ee]221 newnode->type = new TypeData( TypeData::Basic );
222 newnode->type->length = lnth;
223 return newnode;
224} // DeclarationNode::newLength
[3848e0e]225
[dd020c0]226DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
227 DeclarationNode * newnode = new DeclarationNode;
228 newnode->type = new TypeData( TypeData::Unknown );
229 newnode->type->forall = forall;
230 return newnode;
231} // DeclarationNode::newForall
232
[47498bd]233DeclarationNode * DeclarationNode::newFromGlobalScope() {
234 DeclarationNode * newnode = new DeclarationNode;
235 newnode->type = new TypeData( TypeData::GlobalScope );
236 return newnode;
237}
238
[c5d7701]239DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
[c194661]240 DeclarationNode * newnode = new DeclarationNode;
241 newnode->type = new TypeData( TypeData::Qualified );
242 newnode->type->qualified.parent = parent->type;
243 newnode->type->qualified.child = child->type;
244 parent->type = nullptr;
245 child->type = nullptr;
246 delete parent;
247 delete child;
248 return newnode;
[c5d7701]249}
250
[bb7422a]251DeclarationNode * DeclarationNode::newAggregate( ast::AggregateDecl::Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
[ba7aa2d]252 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]253 newnode->type = new TypeData( TypeData::Aggregate );
[8f6f47d7]254 newnode->type->aggregate.kind = kind;
[692c1cc]255 newnode->type->aggregate.anon = name == nullptr;
256 newnode->type->aggregate.name = newnode->type->aggregate.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
[8f6f47d7]257 newnode->type->aggregate.actuals = actuals;
258 newnode->type->aggregate.fields = fields;
259 newnode->type->aggregate.body = body;
[6ea87486]260 newnode->type->aggregate.tagged = false;
261 newnode->type->aggregate.parent = nullptr;
[b87a5ed]262 return newnode;
[984dce6]263} // DeclarationNode::newAggregate
[3848e0e]264
[e4d7c1c]265DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base, EnumHiding hiding ) {
[ba7aa2d]266 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]267 newnode->type = new TypeData( TypeData::Enum );
[692c1cc]268 newnode->type->enumeration.anon = name == nullptr;
269 newnode->type->enumeration.name = newnode->type->enumeration.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
[8f6f47d7]270 newnode->type->enumeration.constants = constants;
[ca1a547]271 newnode->type->enumeration.body = body;
[b0d9ff7]272 newnode->type->enumeration.typed = typed;
[e4d7c1c]273 newnode->type->enumeration.hiding = hiding;
[78e2fca]274 if ( base && base->type ) {
[ed9a1ae]275 newnode->type->base = base->type;
[9e7236f4]276 } // if
277
[b87a5ed]278 return newnode;
[984dce6]279} // DeclarationNode::newEnum
[3848e0e]280
[561354f]281DeclarationNode * DeclarationNode::newAdt( const string * name, DeclarationNode * constructors ) {
282 assert( name );
283 DeclarationNode * newnode = new DeclarationNode;
284 newnode->type = new TypeData( TypeData::Adt );
285 newnode->type->adt.name = name;
286 newnode->type->adt.data_constructors = constructors;
[28f8f15]287 return newnode;
[561354f]288} // DeclarationNode::newAdt
[28f8f15]289
290
[a46b69c]291DeclarationNode * DeclarationNode::newName( const string * name ) {
[ba7aa2d]292 DeclarationNode * newnode = new DeclarationNode;
[a46b69c]293 assert( ! newnode->name );
[2298f728]294 newnode->name = name;
[a46b69c]295 return newnode;
296} // DeclarationNode::newName
297
[374cb117]298DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
[a46b69c]299 DeclarationNode * newnode = newName( name );
[4f147cc]300 newnode->enumeratorValue.reset( constant );
[b87a5ed]301 return newnode;
[984dce6]302} // DeclarationNode::newEnumConstant
[3848e0e]303
[374cb117]304DeclarationNode * DeclarationNode::newEnumValueGeneric( const string * name, InitializerNode * init ) {
[b0d9ff7]305 if ( init ) {
306 if ( init->get_expression() ) {
[374cb117]307 return newEnumConstant( name, init->get_expression() );
[b0d9ff7]308 } else {
[374cb117]309 DeclarationNode * newnode = newName( name );
310 newnode->initializer = init;
311 return newnode;
312 } // if
313 } else {
[b0d9ff7]314 return newName( name );
[374cb117]315 } // if
[9e7236f4]316} // DeclarationNode::newEnumValueGeneric
[374cb117]317
[28f8f15]318DeclarationNode * DeclarationNode::newDataConstructor( const string * name ) {
319 DeclarationNode * newnode = newName(name);
320 return newnode;
321}
322
[1e30df7]323DeclarationNode * DeclarationNode::newEnumInLine( const string name ) {
324 DeclarationNode * newnode = newName( new std::string(name) );
325 newnode->enumInLine = true;
326 return newnode;
327}
328
[a46b69c]329DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
[ba7aa2d]330 DeclarationNode * newnode = new DeclarationNode;
[a46b69c]331 newnode->type = new TypeData( TypeData::SymbolicInst );
332 newnode->type->symbolic.name = name;
333 newnode->type->symbolic.isTypedef = true;
334 newnode->type->symbolic.params = nullptr;
[b87a5ed]335 return newnode;
[a46b69c]336} // DeclarationNode::newFromTypedef
[3848e0e]337
[25bca42]338DeclarationNode * DeclarationNode::newFromTypeGen( const string * name, ExpressionNode * params ) {
[ba7aa2d]339 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]340 newnode->type = new TypeData( TypeData::SymbolicInst );
[fb114fa1]341 newnode->type->symbolic.name = name;
[8f6f47d7]342 newnode->type->symbolic.isTypedef = false;
343 newnode->type->symbolic.actuals = params;
[b87a5ed]344 return newnode;
[984dce6]345} // DeclarationNode::newFromTypeGen
[3848e0e]346
[bb7422a]347DeclarationNode * DeclarationNode::newTypeParam( ast::TypeDecl::Kind tc, const string * name ) {
[a46b69c]348 DeclarationNode * newnode = newName( name );
[2298f728]349 newnode->type = nullptr;
[28307be]350 newnode->variable.tyClass = tc;
[faddbd8]351 newnode->variable.assertions = nullptr;
[b87a5ed]352 return newnode;
[984dce6]353} // DeclarationNode::newTypeParam
[3848e0e]354
[fb114fa1]355DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
[ba7aa2d]356 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]357 newnode->type = new TypeData( TypeData::Aggregate );
[2298f728]358 newnode->type->aggregate.name = name;
[bb7422a]359 newnode->type->aggregate.kind = ast::AggregateDecl::Trait;
[8f6f47d7]360 newnode->type->aggregate.params = params;
361 newnode->type->aggregate.fields = asserts;
[b87a5ed]362 return newnode;
[984dce6]363} // DeclarationNode::newTrait
[3848e0e]364
[fb114fa1]365DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
[ba7aa2d]366 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]367 newnode->type = new TypeData( TypeData::AggregateInst );
[8f6f47d7]368 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
[bb7422a]369 newnode->type->aggInst.aggregate->aggregate.kind = ast::AggregateDecl::Trait;
[2298f728]370 newnode->type->aggInst.aggregate->aggregate.name = name;
[8f6f47d7]371 newnode->type->aggInst.params = params;
[b87a5ed]372 return newnode;
[984dce6]373} // DeclarationNode::newTraitUse
[3848e0e]374
[25bca42]375DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) {
[a46b69c]376 DeclarationNode * newnode = newName( name );
[b87a5ed]377 newnode->type = new TypeData( TypeData::Symbolic );
[8f6f47d7]378 newnode->type->symbolic.isTypedef = false;
379 newnode->type->symbolic.params = typeParams;
[b87a5ed]380 return newnode;
[984dce6]381} // DeclarationNode::newTypeDecl
[3848e0e]382
[ce8c12f]383DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
[ba7aa2d]384 DeclarationNode * newnode = new DeclarationNode;
[ce8c12f]385 newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
[71d0eab]386 if ( kind == OperKinds::And ) {
387 // T && is parsed as 'And' operator rather than two references => add a second reference type
388 TypeData * td = new TypeData( TypeData::Reference );
389 td->base = newnode->type;
390 newnode->type = td;
391 }
[c3396e0]392 if ( qualifiers ) {
393 return newnode->addQualifiers( qualifiers );
394 } else {
395 return newnode;
396 } // if
[984dce6]397} // DeclarationNode::newPointer
[3848e0e]398
[ba7aa2d]399DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
400 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]401 newnode->type = new TypeData( TypeData::Array );
[8f6f47d7]402 newnode->type->array.dimension = size;
403 newnode->type->array.isStatic = isStatic;
[bb7422a]404 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ast::ConstantExpr *>() ) {
[8f6f47d7]405 newnode->type->array.isVarLen = false;
[71bd8c6]406 } else {
[8f6f47d7]407 newnode->type->array.isVarLen = true;
[71bd8c6]408 } // if
[b87a5ed]409 return newnode->addQualifiers( qualifiers );
[984dce6]410} // DeclarationNode::newArray
[3848e0e]411
[ba7aa2d]412DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
413 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]414 newnode->type = new TypeData( TypeData::Array );
[2298f728]415 newnode->type->array.dimension = nullptr;
[8f6f47d7]416 newnode->type->array.isStatic = false;
417 newnode->type->array.isVarLen = true;
[b87a5ed]418 return newnode->addQualifiers( qualifiers );
[3848e0e]419}
420
[ba7aa2d]421DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
422 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]423 newnode->bitfieldWidth = size;
424 return newnode;
[3848e0e]425}
426
[ba7aa2d]427DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
428 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]429 newnode->type = new TypeData( TypeData::Tuple );
[8f6f47d7]430 newnode->type->tuple = members;
[b87a5ed]431 return newnode;
[3848e0e]432}
433
[f855545]434DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) {
[ba7aa2d]435 DeclarationNode * newnode = new DeclarationNode;
[f855545]436 newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof );
[8f6f47d7]437 newnode->type->typeexpr = expr;
[b87a5ed]438 return newnode;
[3848e0e]439}
440
[93bbbc4]441DeclarationNode * DeclarationNode::newVtableType( DeclarationNode * decl ) {
442 DeclarationNode * newnode = new DeclarationNode;
443 newnode->type = new TypeData( TypeData::Vtable );
444 newnode->setBase( decl->type );
445 return newnode;
446}
447
[8f6f47d7]448DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
[ba7aa2d]449 DeclarationNode * newnode = new DeclarationNode;
[8f6f47d7]450 newnode->type = new TypeData( TypeData::Builtin );
451 newnode->builtin = bt;
[148f7290]452 newnode->type->builtintype = newnode->builtin;
[8f6f47d7]453 return newnode;
454} // DeclarationNode::newBuiltinType
455
[a46b69c]456DeclarationNode * DeclarationNode::newFunction( const string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) {
457 DeclarationNode * newnode = newName( name );
458 newnode->type = new TypeData( TypeData::Function );
459 newnode->type->function.params = param;
460 newnode->type->function.body = body;
461
462 if ( ret ) {
463 newnode->type->base = ret->type;
464 ret->type = nullptr;
465 delete ret;
466 } // if
467
468 return newnode;
469} // DeclarationNode::newFunction
470
[25bca42]471DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
[44a81853]472 DeclarationNode * newnode = new DeclarationNode;
473 newnode->type = nullptr;
[bb7422a]474 std::vector<ast::ptr<ast::Expr>> exprs;
[44a81853]475 buildList( expr, exprs );
[bb7422a]476 newnode->attributes.push_back(
477 new ast::Attribute( *name, std::move( exprs ) ) );
[44a81853]478 delete name;
479 return newnode;
480}
481
[2d019af]482DeclarationNode * DeclarationNode::newDirectiveStmt( StatementNode * stmt ) {
483 DeclarationNode * newnode = new DeclarationNode;
484 newnode->directiveStmt = stmt;
485 return newnode;
486}
487
[e994912]488DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
489 DeclarationNode * newnode = new DeclarationNode;
490 newnode->asmStmt = stmt;
491 return newnode;
492}
493
[bb7422a]494DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, ast::Expr * message ) {
[f6e3e34]495 DeclarationNode * newnode = new DeclarationNode;
496 newnode->assert.condition = condition;
497 newnode->assert.message = message;
498 return newnode;
499}
500
[bb7422a]501static void appendError( string & dst, const string & src ) {
[5b639ee]502 if ( src.empty() ) return;
503 if ( dst.empty() ) { dst = src; return; }
504 dst += ", " + src;
505} // appendError
506
[ba7aa2d]507void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
[bb7422a]508 const ast::CV::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
509 const ast::CV::Qualifiers duplicates = qsrc & qdst;
510
511 if ( duplicates.any() ) {
512 std::stringstream str;
513 str << "duplicate ";
514 ast::print( str, duplicates );
515 str << "qualifier(s)";
516 appendError( error, str.str() );
[a7c90d4]517 } // for
[c1c1112]518} // DeclarationNode::checkQualifiers
519
[a7c90d4]520void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
[bb7422a]521 ast::Function::Specs fsDups = funcSpecs & src->funcSpecs;
522 if ( fsDups.any() ) {
523 std::stringstream str;
524 str << "duplicate ";
525 ast::print( str, fsDups );
526 str << "function specifier(s)";
527 appendError( error, str.str() );
[dd020c0]528 } // if
529
[bb7422a]530 // Skip if everything is unset.
531 if ( storageClasses.any() && src->storageClasses.any() ) {
532 ast::Storage::Classes dups = storageClasses & src->storageClasses;
533 // Check for duplicates.
534 if ( dups.any() ) {
535 std::stringstream str;
536 str << "duplicate ";
537 ast::print( str, dups );
538 str << "storage class(es)";
539 appendError( error, str.str() );
540 // Check for conflicts.
541 } else if ( !src->storageClasses.is_threadlocal_any() ) {
542 std::stringstream str;
543 str << "conflicting ";
544 ast::print( str, ast::Storage::Classes( 1 << storageClasses.ffs() ) );
545 str << "& ";
546 ast::print( str, ast::Storage::Classes( 1 << src->storageClasses.ffs() ) );
547 str << "storage classes";
548 appendError( error, str.str() );
549 // FIX to preserve invariant of one basic storage specifier
550 src->storageClasses.reset();
551 }
[b6424d9]552 } // if
[dd020c0]553
[a7c90d4]554 appendError( error, src->error );
555} // DeclarationNode::checkSpecifiers
[b6424d9]556
[a7c90d4]557DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
[6f95000]558 funcSpecs |= q->funcSpecs;
559 storageClasses |= q->storageClasses;
[c0aa336]560
[bb7422a]561 std::vector<ast::ptr<ast::Attribute>> tmp;
562 tmp.reserve( q->attributes.size() );
563 for ( auto const & attr : q->attributes ) {
564 tmp.emplace_back( ast::shallowCopy( attr.get() ) );
565 }
566 spliceBegin( attributes, tmp );
567
[b6424d9]568 return this;
[a7c90d4]569} // DeclarationNode::copySpecifiers
[b6424d9]570
[f2f512ba]571static void addQualifiersToType( TypeData *& src, TypeData * dst ) {
[101e0bd]572 if ( dst->base ) {
573 addQualifiersToType( src, dst->base );
574 } else if ( dst->kind == TypeData::Function ) {
575 dst->base = src;
[2298f728]576 src = nullptr;
[101e0bd]577 } else {
[6f95000]578 dst->qualifiers |= src->qualifiers;
[101e0bd]579 } // if
580} // addQualifiersToType
581
[ba7aa2d]582DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
[af9da5f]583 if ( ! q ) { return this; } // empty qualifier
[101e0bd]584
[a7c90d4]585 checkSpecifiers( q );
586 copySpecifiers( q );
[101e0bd]587
[c38ae92]588 if ( ! q->type ) { delete q; return this; }
[101e0bd]589
590 if ( ! type ) {
[c38ae92]591 type = q->type; // reuse structure
[1b772749]592 q->type = nullptr;
593 delete q;
[101e0bd]594 return this;
595 } // if
596
[c38ae92]597 if ( q->type->forall ) { // forall qualifier ?
598 if ( type->forall ) { // polymorphic routine ?
599 type->forall->appendList( q->type->forall ); // augment forall qualifier
[101e0bd]600 } else {
[c38ae92]601 if ( type->kind == TypeData::Aggregate ) { // struct/union ?
602 if ( type->aggregate.params ) { // polymorphic ?
603 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
604 } else { // not polymorphic
[a1c9ddd]605 type->aggregate.params = q->type->forall; // set forall qualifier
[c38ae92]606 } // if
607 } else { // not polymorphic
608 type->forall = q->type->forall; // make polymorphic routine
[68cd1ce]609 } // if
610 } // if
[c38ae92]611 q->type->forall = nullptr; // forall qualifier moved
[68cd1ce]612 } // if
[6ef2d81]613
[a7c90d4]614 checkQualifiers( type, q->type );
[78e2fca]615 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.any() && error.length() == 0 ) {
[be00a2d]616 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, builtinTypeNames[builtin] );
[9dc31c10]617 } // if
[6ef2d81]618 addQualifiersToType( q->type, type );
619
[b87a5ed]620 delete q;
621 return this;
[101e0bd]622} // addQualifiers
[3848e0e]623
[f2f512ba]624static void addTypeToType( TypeData *& src, TypeData *& dst ) {
[101e0bd]625 if ( src->forall && dst->kind == TypeData::Function ) {
626 if ( dst->forall ) {
627 dst->forall->appendList( src->forall );
[b87a5ed]628 } else {
[101e0bd]629 dst->forall = src->forall;
630 } // if
[2298f728]631 src->forall = nullptr;
[101e0bd]632 } // if
633 if ( dst->base ) {
634 addTypeToType( src, dst->base );
635 } else {
636 switch ( dst->kind ) {
[0d0931d]637 case TypeData::Unknown:
[6f95000]638 src->qualifiers |= dst->qualifiers;
[101e0bd]639 dst = src;
[2298f728]640 src = nullptr;
[101e0bd]641 break;
[0d0931d]642 case TypeData::Basic:
[6f95000]643 dst->qualifiers |= src->qualifiers;
[101e0bd]644 if ( src->kind != TypeData::Unknown ) {
645 assert( src->kind == TypeData::Basic );
646
647 if ( dst->basictype == DeclarationNode::NoBasicType ) {
648 dst->basictype = src->basictype;
649 } else if ( src->basictype != DeclarationNode::NoBasicType )
[a16764a6]650 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
[101e0bd]651
652 if ( dst->complextype == DeclarationNode::NoComplexType ) {
653 dst->complextype = src->complextype;
654 } else if ( src->complextype != DeclarationNode::NoComplexType )
[a16764a6]655 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
[101e0bd]656
657 if ( dst->signedness == DeclarationNode::NoSignedness ) {
658 dst->signedness = src->signedness;
659 } else if ( src->signedness != DeclarationNode::NoSignedness )
[a16764a6]660 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
[101e0bd]661
662 if ( dst->length == DeclarationNode::NoLength ) {
663 dst->length = src->length;
664 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
665 dst->length = DeclarationNode::LongLong;
666 } else if ( src->length != DeclarationNode::NoLength )
[a16764a6]667 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
[101e0bd]668 } // if
669 break;
[0d0931d]670 default:
[101e0bd]671 switch ( src->kind ) {
[0d0931d]672 case TypeData::Aggregate:
673 case TypeData::Enum:
[101e0bd]674 dst->base = new TypeData( TypeData::AggregateInst );
675 dst->base->aggInst.aggregate = src;
676 if ( src->kind == TypeData::Aggregate ) {
677 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
[68cd1ce]678 } // if
[6f95000]679 dst->base->qualifiers |= src->qualifiers;
[2298f728]680 src = nullptr;
[b87a5ed]681 break;
[0d0931d]682 default:
[101e0bd]683 if ( dst->forall ) {
684 dst->forall->appendList( src->forall );
685 } else {
686 dst->forall = src->forall;
687 } // if
[2298f728]688 src->forall = nullptr;
[101e0bd]689 dst->base = src;
[2298f728]690 src = nullptr;
[68cd1ce]691 } // switch
[101e0bd]692 } // switch
[68cd1ce]693 } // if
[3848e0e]694}
695
[ba7aa2d]696DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
[b87a5ed]697 if ( o ) {
[a7c90d4]698 checkSpecifiers( o );
699 copySpecifiers( o );
[b87a5ed]700 if ( o->type ) {
701 if ( ! type ) {
702 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
703 type = new TypeData( TypeData::AggregateInst );
[8f6f47d7]704 type->aggInst.aggregate = o->type;
[b87a5ed]705 if ( o->type->kind == TypeData::Aggregate ) {
[43c89a7]706 type->aggInst.hoistType = o->type->aggregate.body;
[8f6f47d7]707 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
[43c89a7]708 } else {
709 type->aggInst.hoistType = o->type->enumeration.body;
[68cd1ce]710 } // if
[6f95000]711 type->qualifiers |= o->type->qualifiers;
[b87a5ed]712 } else {
713 type = o->type;
[68cd1ce]714 } // if
[2298f728]715 o->type = nullptr;
[b87a5ed]716 } else {
717 addTypeToType( o->type, type );
[68cd1ce]718 } // if
719 } // if
[b87a5ed]720 if ( o->bitfieldWidth ) {
721 bitfieldWidth = o->bitfieldWidth;
[68cd1ce]722 } // if
[71bd8c6]723
724 // there may be typedefs chained onto the type
[1d4580a]725 if ( o->get_next() ) {
726 set_last( o->get_next()->clone() );
[1db21619]727 } // if
[68cd1ce]728 } // if
[b87a5ed]729 delete o;
[66406f3]730
[b87a5ed]731 return this;
[3848e0e]732}
733
[f135b50]734DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) {
735 if ( o && o -> type) {
736 type->base= o->type;
737 }
738 delete o;
739 return this;
740}
741
[ba7aa2d]742DeclarationNode * DeclarationNode::addTypedef() {
743 TypeData * newtype = new TypeData( TypeData::Symbolic );
[2298f728]744 newtype->symbolic.params = nullptr;
[8f6f47d7]745 newtype->symbolic.isTypedef = true;
[2298f728]746 newtype->symbolic.name = name ? new string( *name ) : nullptr;
[b87a5ed]747 newtype->base = type;
748 type = newtype;
749 return this;
[3848e0e]750}
751
[ba7aa2d]752DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
[bb7422a]753 if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
[702e826]754 if ( variable.assertions ) {
755 variable.assertions->appendList( assertions );
756 } else {
757 variable.assertions = assertions;
758 } // if
759 return this;
[2298f728]760 } // if
761
[b87a5ed]762 assert( type );
763 switch ( type->kind ) {
[0d0931d]764 case TypeData::Symbolic:
[8f6f47d7]765 if ( type->symbolic.assertions ) {
766 type->symbolic.assertions->appendList( assertions );
[b87a5ed]767 } else {
[8f6f47d7]768 type->symbolic.assertions = assertions;
[68cd1ce]769 } // if
[b87a5ed]770 break;
[0d0931d]771 default:
[b87a5ed]772 assert( false );
[68cd1ce]773 } // switch
[974906e2]774
[b87a5ed]775 return this;
[51b73452]776}
777
[fb114fa1]778DeclarationNode * DeclarationNode::addName( string * newname ) {
[2298f728]779 assert( ! name );
780 name = newname;
[b87a5ed]781 return this;
[51b73452]782}
783
[c0aa336]784DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
[58dd019]785 assert( ! asmName );
[c0aa336]786 asmName = newname ? newname->asmName : nullptr;
787 return this->addQualifiers( newname );
[58dd019]788}
789
[ba7aa2d]790DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
[b87a5ed]791 bitfieldWidth = size;
792 return this;
[51b73452]793}
794
[ba7aa2d]795DeclarationNode * DeclarationNode::addVarArgs() {
[b87a5ed]796 assert( type );
797 hasEllipsis = true;
798 return this;
[51b73452]799}
800
[c453ac4]801DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
[b87a5ed]802 assert( type );
803 assert( type->kind == TypeData::Function );
[2298f728]804 assert( ! type->function.body );
[8f6f47d7]805 type->function.body = body;
[c453ac4]806 type->function.withExprs = withExprs;
[b87a5ed]807 return this;
[51b73452]808}
809
[ba7aa2d]810DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
[b87a5ed]811 assert( type );
812 assert( type->kind == TypeData::Function );
[2298f728]813 assert( ! type->function.oldDeclList );
[8f6f47d7]814 type->function.oldDeclList = list;
[b87a5ed]815 return this;
[51b73452]816}
817
[c0aa336]818DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
[b87a5ed]819 if ( type ) {
[ba7aa2d]820 TypeData * prevBase = type;
821 TypeData * curBase = type->base;
[2298f728]822 while ( curBase != nullptr ) {
[b87a5ed]823 prevBase = curBase;
824 curBase = curBase->base;
[68cd1ce]825 } // while
[b87a5ed]826 prevBase->base = newType;
827 } else {
828 type = newType;
[68cd1ce]829 } // if
[c0aa336]830 return this;
[3848e0e]831}
832
[c0aa336]833DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
834 if ( a ) {
[bb7422a]835 spliceBegin( attributes, a->attributes );
[c0aa336]836 a->attributes.clear();
837 } // if
838 return this;
839} // copyAttribute
840
[ba7aa2d]841DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
[b87a5ed]842 if ( p ) {
[6926a6d]843 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[c0aa336]844 setBase( p->type );
[2298f728]845 p->type = nullptr;
[c0aa336]846 copyAttribute( p );
[b87a5ed]847 delete p;
[68cd1ce]848 } // if
[b87a5ed]849 return this;
[3848e0e]850}
851
[ba7aa2d]852DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
[b87a5ed]853 if ( a ) {
854 assert( a->type->kind == TypeData::Array );
[c0aa336]855 setBase( a->type );
[2298f728]856 a->type = nullptr;
[c0aa336]857 copyAttribute( a );
[b87a5ed]858 delete a;
[68cd1ce]859 } // if
[b87a5ed]860 return this;
[51b73452]861}
862
[ba7aa2d]863DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
[b87a5ed]864 if ( p ) {
[e6cee92]865 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[b87a5ed]866 if ( type ) {
867 switch ( type->kind ) {
[0d0931d]868 case TypeData::Aggregate:
869 case TypeData::Enum:
[b87a5ed]870 p->type->base = new TypeData( TypeData::AggregateInst );
[8f6f47d7]871 p->type->base->aggInst.aggregate = type;
[b87a5ed]872 if ( type->kind == TypeData::Aggregate ) {
[8f6f47d7]873 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
[68cd1ce]874 } // if
[6f95000]875 p->type->base->qualifiers |= type->qualifiers;
[b87a5ed]876 break;
877
[0d0931d]878 default:
[b87a5ed]879 p->type->base = type;
[68cd1ce]880 } // switch
[2298f728]881 type = nullptr;
[68cd1ce]882 } // if
[b87a5ed]883 delete this;
884 return p;
885 } else {
886 return this;
[68cd1ce]887 } // if
[51b73452]888}
889
[ba7aa2d]890static TypeData * findLast( TypeData * a ) {
[b87a5ed]891 assert( a );
[ba7aa2d]892 TypeData * cur = a;
[a32b204]893 while ( cur->base ) {
[b87a5ed]894 cur = cur->base;
[68cd1ce]895 } // while
[b87a5ed]896 return cur;
[3848e0e]897}
898
[ba7aa2d]899DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
[0d0931d]900 if ( ! a ) return this;
[738e304]901 assert( a->type->kind == TypeData::Array );
902 TypeData * lastArray = findLast( a->type );
903 if ( type ) {
904 switch ( type->kind ) {
[0d0931d]905 case TypeData::Aggregate:
906 case TypeData::Enum:
[738e304]907 lastArray->base = new TypeData( TypeData::AggregateInst );
908 lastArray->base->aggInst.aggregate = type;
909 if ( type->kind == TypeData::Aggregate ) {
910 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
911 } // if
[6f95000]912 lastArray->base->qualifiers |= type->qualifiers;
[738e304]913 break;
[0d0931d]914 default:
[738e304]915 lastArray->base = type;
916 } // switch
917 type = nullptr;
[68cd1ce]918 } // if
[738e304]919 delete this;
920 return a;
[51b73452]921}
[3848e0e]922
[ba7aa2d]923DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
924 TypeData * ftype = new TypeData( TypeData::Function );
[8f6f47d7]925 ftype->function.params = params;
[c0aa336]926 setBase( ftype );
[b87a5ed]927 return this;
[3848e0e]928}
929
[ba7aa2d]930static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
[b87a5ed]931 if ( type ) {
932 if ( type->kind != TypeData::Function ) {
933 type->base = addIdListToType( type->base, ids );
934 } else {
[8f6f47d7]935 type->function.idList = ids;
[68cd1ce]936 } // if
[b87a5ed]937 return type;
[3848e0e]938 } else {
[ba7aa2d]939 TypeData * newtype = new TypeData( TypeData::Function );
[8f6f47d7]940 newtype->function.idList = ids;
[b87a5ed]941 return newtype;
[68cd1ce]942 } // if
[2298f728]943} // addIdListToType
[974906e2]944
[ba7aa2d]945DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
[b87a5ed]946 type = addIdListToType( type, ids );
947 return this;
[3848e0e]948}
949
[ba7aa2d]950DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
[b87a5ed]951 initializer = init;
952 return this;
[3848e0e]953}
954
[67cf18c]955DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
[bb7422a]956 assertf( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS, "Called addTypeInitializer on something that isn't a type variable." );
[67cf18c]957 variable.initializer = init;
958 return this;
959}
960
[a46b69c]961DeclarationNode * DeclarationNode::cloneType( string * name ) {
962 DeclarationNode * newnode = newName( name );
[b87a5ed]963 newnode->type = maybeClone( type );
[a7c90d4]964 newnode->copySpecifiers( this );
[b87a5ed]965 return newnode;
[3848e0e]966}
967
[2298f728]968DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
969 if ( ! o ) return nullptr;
970
[a7c90d4]971 o->copySpecifiers( this );
[2298f728]972 if ( type ) {
973 TypeData * srcType = type;
974
[c0aa336]975 // search for the base type by scanning off pointers and array designators
[2298f728]976 while ( srcType->base ) {
977 srcType = srcType->base;
978 } // while
979
980 TypeData * newType = srcType->clone();
981 if ( newType->kind == TypeData::AggregateInst ) {
982 // don't duplicate members
983 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
984 delete newType->aggInst.aggregate->enumeration.constants;
985 newType->aggInst.aggregate->enumeration.constants = nullptr;
[b2da0574]986 newType->aggInst.aggregate->enumeration.body = false;
[b87a5ed]987 } else {
[2298f728]988 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
989 delete newType->aggInst.aggregate->aggregate.fields;
990 newType->aggInst.aggregate->aggregate.fields = nullptr;
[b2da0574]991 newType->aggInst.aggregate->aggregate.body = false;
[68cd1ce]992 } // if
[43c89a7]993 // don't hoist twice
994 newType->aggInst.hoistType = false;
[68cd1ce]995 } // if
[2298f728]996
997 newType->forall = maybeClone( type->forall );
998 if ( ! o->type ) {
999 o->type = newType;
1000 } else {
1001 addTypeToType( newType, o->type );
1002 delete newType;
1003 } // if
[68cd1ce]1004 } // if
[b87a5ed]1005 return o;
[51b73452]1006}
1007
[ba7aa2d]1008DeclarationNode * DeclarationNode::extractAggregate() const {
[b87a5ed]1009 if ( type ) {
[ba7aa2d]1010 TypeData * ret = typeextractAggregate( type );
[b87a5ed]1011 if ( ret ) {
[ba7aa2d]1012 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]1013 newnode->type = ret;
1014 return newnode;
[843054c2]1015 } // if
1016 } // if
[2298f728]1017 return nullptr;
[3848e0e]1018}
1019
[45e753c]1020// If a typedef wraps an anonymous declaration, name the inner declaration
1021// so it has a consistent name across translation units.
1022static void nameTypedefedDecl(
1023 DeclarationNode * innerDecl,
1024 const DeclarationNode * outerDecl ) {
1025 TypeData * outer = outerDecl->type;
1026 assert( outer );
1027 // First make sure this is a typedef:
1028 if ( outer->kind != TypeData::Symbolic || !outer->symbolic.isTypedef ) {
1029 return;
1030 }
1031 TypeData * inner = innerDecl->type;
1032 assert( inner );
1033 // Always clear any CVs associated with the aggregate:
1034 inner->qualifiers.reset();
1035 // Handle anonymous aggregates: typedef struct { int i; } foo
1036 if ( inner->kind == TypeData::Aggregate && inner->aggregate.anon ) {
1037 delete inner->aggregate.name;
1038 inner->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
1039 inner->aggregate.anon = false;
1040 assert( outer->base );
1041 delete outer->base->aggInst.aggregate->aggregate.name;
1042 outer->base->aggInst.aggregate->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
1043 outer->base->aggInst.aggregate->aggregate.anon = false;
1044 outer->base->aggInst.aggregate->qualifiers.reset();
1045 // Handle anonymous enumeration: typedef enum { A, B, C } foo
1046 } else if ( inner->kind == TypeData::Enum && inner->enumeration.anon ) {
1047 delete inner->enumeration.name;
1048 inner->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
1049 inner->enumeration.anon = false;
1050 assert( outer->base );
1051 delete outer->base->aggInst.aggregate->enumeration.name;
1052 outer->base->aggInst.aggregate->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
1053 outer->base->aggInst.aggregate->enumeration.anon = false;
1054 // No qualifiers.reset() here.
1055 }
1056}
1057
1058// This code handles a special issue with the attribute transparent_union.
1059//
1060// typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union ))
1061//
1062// Here the attribute aligned goes with the typedef_name, so variables declared of this type are
1063// aligned. However, the attribute transparent_union must be moved from the typedef_name to
1064// alias union U. Currently, this is the only know attribute that must be moved from typedef to
1065// alias.
1066static void moveUnionAttribute( ast::Decl * decl, ast::UnionDecl * unionDecl ) {
1067 if ( auto typedefDecl = dynamic_cast<ast::TypedefDecl *>( decl ) ) {
1068 // Is the typedef alias a union aggregate?
1069 if ( nullptr == unionDecl ) return;
1070
1071 // If typedef is an alias for a union, then its alias type was hoisted above and remembered.
1072 if ( auto unionInstType = typedefDecl->base.as<ast::UnionInstType>() ) {
1073 auto instType = ast::mutate( unionInstType );
1074 // Remove all transparent_union attributes from typedef and move to alias union.
1075 for ( auto attr = instType->attributes.begin() ; attr != instType->attributes.end() ; ) {
1076 assert( *attr );
1077 if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) {
1078 unionDecl->attributes.emplace_back( attr->release() );
1079 attr = instType->attributes.erase( attr );
1080 } else {
1081 attr++;
1082 }
1083 }
1084 typedefDecl->base = instType;
1085 }
1086 }
1087}
1088
1089// Get the non-anonymous name of the instance type of the declaration,
1090// if one exists.
1091static const std::string * getInstTypeOfName( ast::Decl * decl ) {
1092 if ( auto dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
1093 if ( auto aggr = dynamic_cast<ast::BaseInstType const *>( dwt->get_type() ) ) {
1094 if ( aggr->name.find("anonymous") == std::string::npos ) {
1095 return &aggr->name;
1096 }
1097 }
1098 }
1099 return nullptr;
1100}
1101
[561354f]1102std::vector<ast::ptr<ast::StructDecl>> buildDataConstructors( DeclarationNode * firstNode ) {
1103 std::vector<ast::ptr<ast::StructDecl>> outputList;
[28f8f15]1104 std::back_insert_iterator<std::vector<ast::ptr<ast::StructDecl>>> out( outputList );
1105 for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
[fa2c005]1106
[28f8f15]1107 assert( cur->type->kind == TypeData::Symbolic );
1108 const std::string * name = cur->name;
1109 auto ctor = new ast::StructDecl( cur->location,
1110 std::string(*name),
1111 ast::AggregateDecl::Aggregate::Struct
1112 );
1113 ctor->set_body(true);
1114 TypeData * td = cur->type;
1115 TypeData::Symbolic_t st = td->symbolic;
1116 DeclarationNode * params = st.params;
1117
1118 if ( params ) {
1119 buildList( params, ctor->members );
1120 }
1121
1122 for ( std::size_t i = 0; i < ctor->members.size(); ++i ) {
1123 assert(ctor->members[i]->name == "");
1124 ast::Decl * member = ctor->members[i].get_and_mutate();
1125 member->name = "field_" + std::to_string(i);
1126 }
[fa2c005]1127
[28f8f15]1128 *out++ = ctor;
1129 }
[561354f]1130 return outputList;
[28f8f15]1131}
1132
[fa2c005]1133void buildDataConstructorsAsMember( DeclarationNode * firstNode, ast::AdtDecl * adtDecl ) {
1134 std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( adtDecl->members );
1135 for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
1136
1137 assert( cur->type->kind == TypeData::Symbolic );
1138 const std::string * name = cur->name;
1139 auto ctor = new ast::StructDecl( cur->location,
1140 std::string(*name),
1141 ast::AggregateDecl::Aggregate::Struct
1142 );
1143 ctor->set_body(true);
1144 TypeData * td = cur->type;
1145 TypeData::Symbolic_t st = td->symbolic;
1146 DeclarationNode * params = st.params;
1147
1148 if ( params ) {
1149 buildList( params, ctor->members );
1150 }
1151
1152 for ( std::size_t i = 0; i < ctor->members.size(); ++i ) {
1153 assert(ctor->members[i]->name == "");
1154 ast::Decl * member = ctor->members[i].get_and_mutate();
1155 member->name = "field_" + std::to_string(i);
1156 }
1157
1158 *out++ = ctor;
1159 }
1160}
1161
1162ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::Decl>> & typeList ) {
[561354f]1163 ast::UnionDecl * out = new ast::UnionDecl( loc, "temp_data_union" );
[28f8f15]1164 // size_t index = 0;
1165 if ( typeList.size() > 0 ) out->set_body( true );
1166 size_t i = 0;
[fa2c005]1167 for (const ast::ptr<ast::Decl> structDecl : typeList ) {
1168 ast::StructInstType * inst = new ast::StructInstType( structDecl.as< ast::StructDecl >() );
[28f8f15]1169 ast::ObjectDecl * instObj = new ast::ObjectDecl(
1170 structDecl->location,
1171 "option_" + std::to_string(i),
1172 inst
1173 );
1174 i++;
1175 out->members.push_back( instObj );
1176
1177 }
1178 return out;
1179}
1180
[fa2c005]1181ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::Decl>> & typeList ) {
[561354f]1182 ast::EnumDecl * out = new ast::EnumDecl( loc, "temp_data_tag" );
[28f8f15]1183 if ( typeList.size() > 0 ) out->set_body( true );
[fa2c005]1184 for ( const ast::ptr<ast::Decl> structDecl : typeList ) {
[28f8f15]1185 ast::EnumInstType * inst = new ast::EnumInstType( out );
1186 assert( inst->base != nullptr );
1187 ast::ObjectDecl * instObj = new ast::ObjectDecl(
1188 structDecl->location,
1189 structDecl->name,
1190 inst
1191 );
1192 out->members.push_back( instObj );
1193 }
1194 return out;
1195}
1196
[561354f]1197ast::StructDecl * buildTaggedUnions( const TypeData * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union ) {
[28f8f15]1198 assert( tags->members.size() == data_union->members.size() );
[fa2c005]1199 ast::StructDecl * out = new ast::StructDecl( data->location, (*(data->adt.name)) + "_tag_union" );
[561354f]1200 out->kind = ast::AggregateDecl::Adt;
[28f8f15]1201
1202 out->set_body( true );
1203
1204 ast::EnumInstType * tag = new ast::EnumInstType( tags );
1205 ast::ObjectDecl * tag_obj = new ast::ObjectDecl(
1206 data->location,
1207 "tag",
1208 tag
1209 );
1210 ast::UnionInstType * value = new ast::UnionInstType( data_union );
1211 ast::ObjectDecl * value_obj = new ast::ObjectDecl(
1212 data->location,
1213 "value",
1214 value
1215 );
1216
1217 out->members.push_back( value_obj );
1218 out->members.push_back( tag_obj );
1219 return out;
1220}
1221
[6611177]1222void buildList( DeclarationNode * firstNode,
[bb7422a]1223 std::vector<ast::ptr<ast::Decl>> & outputList ) {
[a16764a6]1224 SemanticErrorException errors;
[bb7422a]1225 std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( outputList );
[2298f728]1226
[45e753c]1227 for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) {
[b87a5ed]1228 try {
[45e753c]1229 bool extracted_named = false;
1230 ast::UnionDecl * unionDecl = nullptr;
[692c1cc]1231
[ba7aa2d]1232 if ( DeclarationNode * extr = cur->extractAggregate() ) {
[78e2fca]1233 assert( cur->type );
[45e753c]1234 nameTypedefedDecl( extr, cur );
[692c1cc]1235
[45e753c]1236 if ( ast::Decl * decl = extr->build() ) {
[692c1cc]1237 // Remember the declaration if it is a union aggregate ?
[bb7422a]1238 unionDecl = dynamic_cast<ast::UnionDecl *>( decl );
[692c1cc]1239
1240 *out++ = decl;
[3d7e53b]1241
1242 // need to remember the cases where a declaration contains an anonymous aggregate definition
1243 assert( extr->type );
1244 if ( extr->type->kind == TypeData::Aggregate ) {
[692c1cc]1245 // typedef struct { int A } B is the only case?
[45e753c]1246 extracted_named = !extr->type->aggregate.anon;
[3d7e53b]1247 } else if ( extr->type->kind == TypeData::Enum ) {
[692c1cc]1248 // typedef enum { A } B is the only case?
[45e753c]1249 extracted_named = !extr->type->enumeration.anon;
1250 } else {
1251 extracted_named = true;
[3d7e53b]1252 }
[843054c2]1253 } // if
[f39096c]1254 delete extr;
[843054c2]1255 } // if
[2298f728]1256
[45e753c]1257 if ( ast::Decl * decl = cur->build() ) {
1258 moveUnionAttribute( decl, unionDecl );
1259
1260 if ( "" == decl->name && !cur->get_inLine() ) {
1261 // Don't include anonymous declaration for named aggregates,
1262 // but do include them for anonymous aggregates, e.g.:
1263 // struct S {
1264 // struct T { int x; }; // no anonymous member
1265 // struct { int y; }; // anonymous member
1266 // struct T; // anonymous member
1267 // };
1268 if ( extracted_named ) {
1269 continue;
1270 }
[692c1cc]1271
[45e753c]1272 if ( auto name = getInstTypeOfName( decl ) ) {
1273 // Temporary: warn about anonymous member declarations of named types, since
1274 // this conflicts with the syntax for the forward declaration of an anonymous type.
1275 SemanticWarning( cur->location, Warning::AggrForwardDecl, name->c_str() );
1276 }
[e07caa2]1277 } // if
[45e753c]1278 *out++ = decl;
[843054c2]1279 } // if
[45e753c]1280 } catch ( SemanticErrorException & e ) {
[b87a5ed]1281 errors.append( e );
[843054c2]1282 } // try
[e07caa2]1283 } // for
[2298f728]1284
[b87a5ed]1285 if ( ! errors.isEmpty() ) {
1286 throw errors;
[843054c2]1287 } // if
[2298f728]1288} // buildList
[3848e0e]1289
[3d7e53b]1290// currently only builds assertions, function parameters, and return values
[6611177]1291void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ) {
[a16764a6]1292 SemanticErrorException errors;
[bb7422a]1293 std::back_insert_iterator<std::vector<ast::ptr<ast::DeclWithType>>> out( outputList );
[43c89a7]1294
[45e753c]1295 for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
[b87a5ed]1296 try {
[bb7422a]1297 ast::Decl * decl = cur->build();
[45e753c]1298 assertf( decl, "buildList: build for ast::DeclWithType." );
[bb7422a]1299 if ( ast::DeclWithType * dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
[47498bd]1300 dwt->location = cur->location;
[3ca7ef3]1301 *out++ = dwt;
[bb7422a]1302 } else if ( ast::StructDecl * agg = dynamic_cast<ast::StructDecl *>( decl ) ) {
[3d7e53b]1303 // e.g., int foo(struct S) {}
[bb7422a]1304 auto inst = new ast::StructInstType( agg->name );
1305 auto obj = new ast::ObjectDecl( cur->location, "", inst );
1306 obj->linkage = linkage;
[3ca7ef3]1307 *out++ = obj;
[47498bd]1308 delete agg;
[bb7422a]1309 } else if ( ast::UnionDecl * agg = dynamic_cast<ast::UnionDecl *>( decl ) ) {
[3d7e53b]1310 // e.g., int foo(union U) {}
[bb7422a]1311 auto inst = new ast::UnionInstType( agg->name );
1312 auto obj = new ast::ObjectDecl( cur->location,
1313 "", inst, nullptr, ast::Storage::Classes(),
1314 linkage );
[3ca7ef3]1315 *out++ = obj;
[bb7422a]1316 } else if ( ast::EnumDecl * agg = dynamic_cast<ast::EnumDecl *>( decl ) ) {
[3d7e53b]1317 // e.g., int foo(enum E) {}
[bb7422a]1318 auto inst = new ast::EnumInstType( agg->name );
1319 auto obj = new ast::ObjectDecl( cur->location,
1320 "",
1321 inst,
1322 nullptr,
1323 ast::Storage::Classes(),
1324 linkage
1325 );
[3ca7ef3]1326 *out++ = obj;
[45e753c]1327 } else {
1328 assertf( false, "buildList: Could not convert to ast::DeclWithType." );
[843054c2]1329 } // if
[45e753c]1330 } catch ( SemanticErrorException & e ) {
[b87a5ed]1331 errors.append( e );
[843054c2]1332 } // try
[3a5131ed]1333 } // for
1334
[b87a5ed]1335 if ( ! errors.isEmpty() ) {
1336 throw errors;
[843054c2]1337 } // if
[2298f728]1338} // buildList
[3848e0e]1339
[bb7422a]1340void buildTypeList( const DeclarationNode * firstNode,
1341 std::vector<ast::ptr<ast::Type>> & outputList ) {
[a16764a6]1342 SemanticErrorException errors;
[bb7422a]1343 std::back_insert_iterator<std::vector<ast::ptr<ast::Type>>> out( outputList );
[2298f728]1344
[45e753c]1345 for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) {
[b87a5ed]1346 try {
[ba7aa2d]1347 * out++ = cur->buildType();
[45e753c]1348 } catch ( SemanticErrorException & e ) {
[b87a5ed]1349 errors.append( e );
[843054c2]1350 } // try
[45e753c]1351 } // for
[2298f728]1352
[b87a5ed]1353 if ( ! errors.isEmpty() ) {
1354 throw errors;
[843054c2]1355 } // if
[2298f728]1356} // buildTypeList
[51b73452]1357
[bb7422a]1358ast::Decl * DeclarationNode::build() const {
[a16764a6]1359 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
[2298f728]1360
[e994912]1361 if ( asmStmt ) {
[bb7422a]1362 auto stmt = strict_dynamic_cast<ast::AsmStmt *>( asmStmt->build() );
1363 return new ast::AsmDecl( stmt->location, stmt );
[e994912]1364 } // if
[2d019af]1365 if ( directiveStmt ) {
[bb7422a]1366 auto stmt = strict_dynamic_cast<ast::DirectiveStmt *>( directiveStmt->build() );
1367 return new ast::DirectiveDecl( stmt->location, stmt );
[2d019af]1368 } // if
[e994912]1369
[bb7422a]1370 if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
[f0ecf9b]1371 // otype is internally converted to dtype + otype parameters
[bb7422a]1372 static const ast::TypeDecl::Kind kindMap[] = { ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Ftype, ast::TypeDecl::Ttype, ast::TypeDecl::Dimension };
1373 static_assert( sizeof(kindMap) / sizeof(kindMap[0]) == ast::TypeDecl::NUMBER_OF_KINDS, "DeclarationNode::build: kindMap is out of sync." );
[8f60f0b]1374 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
[bb7422a]1375 ast::TypeDecl * ret = new ast::TypeDecl( location,
1376 *name,
1377 ast::Storage::Classes(),
1378 (ast::Type *)nullptr,
1379 kindMap[ variable.tyClass ],
1380 variable.tyClass == ast::TypeDecl::Otype || variable.tyClass == ast::TypeDecl::DStype,
1381 variable.initializer ? variable.initializer->buildType() : nullptr
1382 );
1383 buildList( variable.assertions, ret->assertions );
[2298f728]1384 return ret;
1385 } // if
1386
[843054c2]1387 if ( type ) {
[dd020c0]1388 // Function specifiers can only appear on a function definition/declaration.
1389 //
1390 // inline _Noreturn int f(); // allowed
1391 // inline _Noreturn int g( int i ); // allowed
1392 // inline _Noreturn int i; // disallowed
[fb04321]1393 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
[a16764a6]1394 SemanticError( this, "invalid function specifier for " );
[dd020c0]1395 } // if
[284da8c]1396 // Forall qualifier can only appear on a function/aggregate definition/declaration.
1397 //
1398 // forall int f(); // allowed
1399 // forall int g( int i ); // allowed
1400 // forall int i; // disallowed
1401 if ( type->kind != TypeData::Function && type->forall ) {
1402 SemanticError( this, "invalid type qualifier for " );
1403 } // if
[3ed994e]1404 bool isDelete = initializer && initializer->get_isDelete();
[bb7422a]1405 ast::Decl * decl = buildDecl(
1406 type,
1407 name ? *name : string( "" ),
1408 storageClasses,
1409 maybeBuild( bitfieldWidth ),
1410 funcSpecs,
1411 linkage,
1412 asmName,
1413 isDelete ? nullptr : maybeBuild( initializer ),
1414 copy( attributes )
1415 )->set_extension( extension );
[3ed994e]1416 if ( isDelete ) {
[bb7422a]1417 auto dwt = strict_dynamic_cast<ast::DeclWithType *>( decl );
[3ed994e]1418 dwt->isDeleted = true;
1419 }
1420 return decl;
[843054c2]1421 } // if
[2298f728]1422
[f6e3e34]1423 if ( assert.condition ) {
[bb7422a]1424 auto cond = maybeBuild( assert.condition );
1425 auto msg = strict_dynamic_cast<ast::ConstantExpr *>( maybeCopy( assert.message ) );
1426 return new ast::StaticAssertDecl( location, cond, msg );
[f6e3e34]1427 }
1428
[dd020c0]1429 // SUE's cannot have function specifiers, either
1430 //
[79aae15]1431 // inline _Noreturn struct S { ... }; // disallowed
1432 // inline _Noreturn enum E { ... }; // disallowed
[fb04321]1433 if ( funcSpecs.any() ) {
[a16764a6]1434 SemanticError( this, "invalid function specifier for " );
[843054c2]1435 } // if
[e874605]1436 if ( enumInLine ) {
[bb7422a]1437 return new ast::InlineMemberDecl( location,
1438 *name, (ast::Type*)nullptr, storageClasses, linkage );
[e874605]1439 } // if
[dd020c0]1440 assertf( name, "ObjectDecl must a have name\n" );
[bb7422a]1441 auto ret = new ast::ObjectDecl( location,
1442 *name,
1443 (ast::Type*)nullptr,
1444 maybeBuild( initializer ),
1445 storageClasses,
1446 linkage,
1447 maybeBuild( bitfieldWidth )
1448 );
1449 ret->asmName = asmName;
1450 ret->extension = extension;
1451 return ret;
[51b73452]1452}
1453
[bb7422a]1454ast::Type * DeclarationNode::buildType() const {
[b87a5ed]1455 assert( type );
[974906e2]1456
[b87a5ed]1457 switch ( type->kind ) {
[0d0931d]1458 case TypeData::Enum:
1459 case TypeData::Aggregate: {
[bb7422a]1460 ast::BaseInstType * ret =
1461 buildComAggInst( type, copy( attributes ), linkage );
1462 buildList( type->aggregate.actuals, ret->params );
[0d0931d]1463 return ret;
1464 }
1465 case TypeData::Symbolic: {
[bb7422a]1466 ast::TypeInstType * ret = new ast::TypeInstType(
1467 *type->symbolic.name,
1468 // This is just a default, the true value is not known yet.
1469 ast::TypeDecl::Dtype,
1470 buildQualifiers( type ),
1471 copy( attributes ) );
1472 buildList( type->symbolic.actuals, ret->params );
[0d0931d]1473 return ret;
1474 }
1475 default:
[bb7422a]1476 ast::Type * simpletypes = typebuild( type );
1477 // copy because member is const
1478 simpletypes->attributes = attributes;
[c0aa336]1479 return simpletypes;
[b87a5ed]1480 } // switch
[3848e0e]1481}
1482
[b87a5ed]1483// Local Variables: //
1484// tab-width: 4 //
1485// mode: c++ //
1486// compile-command: "make install" //
1487// End: //
Note: See TracBrowser for help on using the repository browser.