source: src/Parser/DeclarationNode.cc@ 5546f50b

Last change on this file since 5546f50b was 6cef439, checked in by Andrew Beach <ajbeach@…>, 20 months ago

Return 'TypeData *' from some parse rules. Moved TypeData construction over to that file.

  • Property mode set to 100644
File size: 35.0 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[974906e2]7// DeclarationNode.cc --
[b87a5ed]8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
[b38f6da]11// Last Modified By : Peter A. Buhr
[4eb3a7c5]12// Last Modified On : Fri Feb 23 18:25:57 2024
13// Update Count : 1533
[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
[5bf685f]37#include "Common/utility.h" // for copy, spliceBegin
[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
[de62360d]44extern TypedefTable typedefTable;
45
[51b73452]46using namespace std;
47
[201aeb9]48// These must harmonize with the corresponding DeclarationNode enumerations.
[702e826]49const char * DeclarationNode::basicTypeNames[] = {
50 "void", "_Bool", "char", "int", "int128",
51 "float", "double", "long double", "float80", "float128",
52 "_float16", "_float32", "_float32x", "_float64", "_float64x", "_float128", "_float128x", "NoBasicTypeNames"
53};
54const char * DeclarationNode::complexTypeNames[] = {
55 "_Complex", "NoComplexTypeNames", "_Imaginary"
56}; // Imaginary unsupported => parse, but make invisible and print error message
57const char * DeclarationNode::signednessNames[] = {
58 "signed", "unsigned", "NoSignednessNames"
59};
60const char * DeclarationNode::lengthNames[] = {
61 "short", "long", "long long", "NoLengthNames"
62};
63const char * DeclarationNode::builtinTypeNames[] = {
64 "__builtin_va_list", "__auto_type", "zero_t", "one_t", "NoBuiltinTypeNames"
65};
[51b73452]66
67UniqueName DeclarationNode::anonymous( "__anonymous" );
68
[bb7422a]69extern ast::Linkage::Spec linkage; // defined in parser.yy
[51b73452]70
[7d05e7e]71DeclarationNode::DeclarationNode() :
[e07caa2]72 linkage( ::linkage ) {
[2298f728]73
[faddbd8]74// variable.name = nullptr;
[bb7422a]75 variable.tyClass = ast::TypeDecl::NUMBER_OF_KINDS;
[28307be]76 variable.assertions = nullptr;
[67cf18c]77 variable.initializer = nullptr;
[7d05e7e]78
[f6e3e34]79 assert.condition = nullptr;
80 assert.message = nullptr;
[28307be]81}
82
83DeclarationNode::~DeclarationNode() {
[4c0b674]84 delete name;
85
[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;
[44adf1b]103 newnode->next = maybeCopy( next );
[2298f728]104 newnode->name = name ? new string( *name ) : nullptr;
[c0aa336]105
[5bf685f]106 newnode->type = maybeCopy( type );
[679e644]107 newnode->inLine = inLine;
[a7c90d4]108 newnode->storageClasses = storageClasses;
109 newnode->funcSpecs = funcSpecs;
[5bf685f]110 newnode->bitfieldWidth = maybeCopy( bitfieldWidth );
111 newnode->enumeratorValue.reset( maybeCopy( enumeratorValue.get() ) );
[b87a5ed]112 newnode->hasEllipsis = hasEllipsis;
113 newnode->linkage = linkage;
[bb7422a]114 newnode->asmName = maybeCopy( asmName );
115 newnode->attributes = attributes;
[5bf685f]116 newnode->initializer = maybeCopy( initializer );
[c0aa336]117 newnode->extension = extension;
[5bf685f]118 newnode->asmStmt = maybeCopy( asmStmt );
[c0aa336]119 newnode->error = error;
[3848e0e]120
[faddbd8]121// newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
[28307be]122 newnode->variable.tyClass = variable.tyClass;
[5bf685f]123 newnode->variable.assertions = maybeCopy( variable.assertions );
124 newnode->variable.initializer = maybeCopy( variable.initializer );
[3848e0e]125
[5bf685f]126 newnode->assert.condition = maybeCopy( assert.condition );
[bb7422a]127 newnode->assert.message = maybeCopy( assert.message );
[28307be]128 return newnode;
129} // DeclarationNode::clone
[3848e0e]130
[f2f512ba]131void DeclarationNode::print( std::ostream & os, int indent ) const {
[59db689]132 os << string( indent, ' ' );
[2298f728]133 if ( name ) {
134 os << *name << ": ";
[68cd1ce]135 } // if
[51b73452]136
[bb7422a]137 if ( linkage != ast::Linkage::Cforall ) {
138 os << ast::Linkage::name( linkage ) << " ";
[68cd1ce]139 } // if
[3848e0e]140
[bb7422a]141 ast::print( os, storageClasses );
142 ast::print( os, funcSpecs );
[dd020c0]143
[b87a5ed]144 if ( type ) {
145 type->print( os, indent );
146 } else {
147 os << "untyped entity ";
[68cd1ce]148 } // if
[3848e0e]149
[b87a5ed]150 if ( bitfieldWidth ) {
[59db689]151 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
[b87a5ed]152 bitfieldWidth->printOneLine( os );
[68cd1ce]153 } // if
[3848e0e]154
[2298f728]155 if ( initializer ) {
[59db689]156 os << endl << string( indent + 2, ' ' ) << "with initializer ";
[b87a5ed]157 initializer->printOneLine( os );
[974906e2]158 os << " maybe constructed? " << initializer->get_maybeConstructed();
[68cd1ce]159 } // if
[3848e0e]160
[692c1cc]161 if ( ! attributes.empty() ) {
[4eb3a7c5]162 os << string( indent + 2, ' ' ) << "with attributes" << endl;
[bb7422a]163 for ( ast::ptr<ast::Attribute> const & attr : reverseIterate( attributes ) ) {
[4eb3a7c5]164 os << string( indent + 4, ' ' );
165 ast::print( os, attr, indent + 2 );
[692c1cc]166 } // for
167 } // if
[66406f3]168
[b87a5ed]169 os << endl;
[51b73452]170}
171
[f2f512ba]172void DeclarationNode::printList( std::ostream & os, int indent ) const {
[dc3fbe5]173 ParseList::printList( os, indent );
[b87a5ed]174 if ( hasEllipsis ) {
175 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
[68cd1ce]176 } // if
[51b73452]177}
178
[6cef439]179DeclarationNode * DeclarationNode::newFromTypeData( TypeData * type ) {
180 DeclarationNode * newnode = new DeclarationNode;
181 newnode->type = type;
182 return newnode;
183} // DeclarationNode::newFromTypeData
184
[bb7422a]185DeclarationNode * DeclarationNode::newStorageClass( ast::Storage::Classes sc ) {
[ba7aa2d]186 DeclarationNode * newnode = new DeclarationNode;
[08d5507b]187 newnode->storageClasses = sc;
[b87a5ed]188 return newnode;
[dd020c0]189} // DeclarationNode::newStorageClass
[3848e0e]190
[bb7422a]191DeclarationNode * DeclarationNode::newFuncSpecifier( ast::Function::Specs fs ) {
[ba7aa2d]192 DeclarationNode * newnode = new DeclarationNode;
[08d5507b]193 newnode->funcSpecs = fs;
[c1c1112]194 return newnode;
[dd020c0]195} // DeclarationNode::newFuncSpecifier
[c1c1112]196
[bb7422a]197DeclarationNode * DeclarationNode::newAggregate( ast::AggregateDecl::Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
[ba7aa2d]198 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]199 newnode->type = new TypeData( TypeData::Aggregate );
[8f6f47d7]200 newnode->type->aggregate.kind = kind;
[692c1cc]201 newnode->type->aggregate.anon = name == nullptr;
202 newnode->type->aggregate.name = newnode->type->aggregate.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
[8f6f47d7]203 newnode->type->aggregate.actuals = actuals;
204 newnode->type->aggregate.fields = fields;
205 newnode->type->aggregate.body = body;
[6ea87486]206 newnode->type->aggregate.tagged = false;
207 newnode->type->aggregate.parent = nullptr;
[b87a5ed]208 return newnode;
[984dce6]209} // DeclarationNode::newAggregate
[3848e0e]210
[e4d7c1c]211DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base, EnumHiding hiding ) {
[ba7aa2d]212 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]213 newnode->type = new TypeData( TypeData::Enum );
[692c1cc]214 newnode->type->enumeration.anon = name == nullptr;
215 newnode->type->enumeration.name = newnode->type->enumeration.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
[8f6f47d7]216 newnode->type->enumeration.constants = constants;
[ca1a547]217 newnode->type->enumeration.body = body;
[b0d9ff7]218 newnode->type->enumeration.typed = typed;
[e4d7c1c]219 newnode->type->enumeration.hiding = hiding;
[78e2fca]220 if ( base && base->type ) {
[ed9a1ae]221 newnode->type->base = base->type;
[9e7236f4]222 } // if
223
[b87a5ed]224 return newnode;
[984dce6]225} // DeclarationNode::newEnum
[3848e0e]226
[a46b69c]227DeclarationNode * DeclarationNode::newName( const string * name ) {
[ba7aa2d]228 DeclarationNode * newnode = new DeclarationNode;
[a46b69c]229 assert( ! newnode->name );
[2298f728]230 newnode->name = name;
[a46b69c]231 return newnode;
232} // DeclarationNode::newName
233
[374cb117]234DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
[a46b69c]235 DeclarationNode * newnode = newName( name );
[4f147cc]236 newnode->enumeratorValue.reset( constant );
[b87a5ed]237 return newnode;
[984dce6]238} // DeclarationNode::newEnumConstant
[3848e0e]239
[374cb117]240DeclarationNode * DeclarationNode::newEnumValueGeneric( const string * name, InitializerNode * init ) {
[b0d9ff7]241 if ( init ) {
242 if ( init->get_expression() ) {
[374cb117]243 return newEnumConstant( name, init->get_expression() );
[b0d9ff7]244 } else {
[374cb117]245 DeclarationNode * newnode = newName( name );
246 newnode->initializer = init;
247 return newnode;
248 } // if
249 } else {
[b0d9ff7]250 return newName( name );
[374cb117]251 } // if
[9e7236f4]252} // DeclarationNode::newEnumValueGeneric
[374cb117]253
[1e30df7]254DeclarationNode * DeclarationNode::newEnumInLine( const string name ) {
255 DeclarationNode * newnode = newName( new std::string(name) );
256 newnode->enumInLine = true;
257 return newnode;
258}
259
[bb7422a]260DeclarationNode * DeclarationNode::newTypeParam( ast::TypeDecl::Kind tc, const string * name ) {
[a46b69c]261 DeclarationNode * newnode = newName( name );
[2298f728]262 newnode->type = nullptr;
[28307be]263 newnode->variable.tyClass = tc;
[faddbd8]264 newnode->variable.assertions = nullptr;
[b87a5ed]265 return newnode;
[984dce6]266} // DeclarationNode::newTypeParam
[3848e0e]267
[fb114fa1]268DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
[ba7aa2d]269 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]270 newnode->type = new TypeData( TypeData::Aggregate );
[2298f728]271 newnode->type->aggregate.name = name;
[bb7422a]272 newnode->type->aggregate.kind = ast::AggregateDecl::Trait;
[8f6f47d7]273 newnode->type->aggregate.params = params;
274 newnode->type->aggregate.fields = asserts;
[b87a5ed]275 return newnode;
[984dce6]276} // DeclarationNode::newTrait
[3848e0e]277
[fb114fa1]278DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
[ba7aa2d]279 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]280 newnode->type = new TypeData( TypeData::AggregateInst );
[8f6f47d7]281 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
[bb7422a]282 newnode->type->aggInst.aggregate->aggregate.kind = ast::AggregateDecl::Trait;
[2298f728]283 newnode->type->aggInst.aggregate->aggregate.name = name;
[8f6f47d7]284 newnode->type->aggInst.params = params;
[b87a5ed]285 return newnode;
[984dce6]286} // DeclarationNode::newTraitUse
[3848e0e]287
[25bca42]288DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) {
[a46b69c]289 DeclarationNode * newnode = newName( name );
[b87a5ed]290 newnode->type = new TypeData( TypeData::Symbolic );
[8f6f47d7]291 newnode->type->symbolic.isTypedef = false;
292 newnode->type->symbolic.params = typeParams;
[b87a5ed]293 return newnode;
[984dce6]294} // DeclarationNode::newTypeDecl
[3848e0e]295
[ce8c12f]296DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
[ba7aa2d]297 DeclarationNode * newnode = new DeclarationNode;
[ce8c12f]298 newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
[71d0eab]299 if ( kind == OperKinds::And ) {
300 // T && is parsed as 'And' operator rather than two references => add a second reference type
301 TypeData * td = new TypeData( TypeData::Reference );
302 td->base = newnode->type;
303 newnode->type = td;
304 }
[c3396e0]305 if ( qualifiers ) {
306 return newnode->addQualifiers( qualifiers );
307 } else {
308 return newnode;
309 } // if
[984dce6]310} // DeclarationNode::newPointer
[3848e0e]311
[ba7aa2d]312DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
313 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]314 newnode->type = new TypeData( TypeData::Array );
[8f6f47d7]315 newnode->type->array.dimension = size;
316 newnode->type->array.isStatic = isStatic;
[bb7422a]317 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ast::ConstantExpr *>() ) {
[8f6f47d7]318 newnode->type->array.isVarLen = false;
[71bd8c6]319 } else {
[8f6f47d7]320 newnode->type->array.isVarLen = true;
[71bd8c6]321 } // if
[b87a5ed]322 return newnode->addQualifiers( qualifiers );
[984dce6]323} // DeclarationNode::newArray
[3848e0e]324
[ba7aa2d]325DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
326 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]327 newnode->type = new TypeData( TypeData::Array );
[2298f728]328 newnode->type->array.dimension = nullptr;
[8f6f47d7]329 newnode->type->array.isStatic = false;
330 newnode->type->array.isVarLen = true;
[b87a5ed]331 return newnode->addQualifiers( qualifiers );
[3848e0e]332}
333
[ba7aa2d]334DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
335 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]336 newnode->bitfieldWidth = size;
337 return newnode;
[3848e0e]338}
339
[ba7aa2d]340DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
341 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]342 newnode->type = new TypeData( TypeData::Tuple );
[8f6f47d7]343 newnode->type->tuple = members;
[b87a5ed]344 return newnode;
[3848e0e]345}
346
[f855545]347DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) {
[ba7aa2d]348 DeclarationNode * newnode = new DeclarationNode;
[f855545]349 newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof );
[8f6f47d7]350 newnode->type->typeexpr = expr;
[b87a5ed]351 return newnode;
[3848e0e]352}
353
[a46b69c]354DeclarationNode * DeclarationNode::newFunction( const string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) {
355 DeclarationNode * newnode = newName( name );
356 newnode->type = new TypeData( TypeData::Function );
357 newnode->type->function.params = param;
358 newnode->type->function.body = body;
359
360 if ( ret ) {
361 newnode->type->base = ret->type;
362 ret->type = nullptr;
363 delete ret;
364 } // if
365
366 return newnode;
367} // DeclarationNode::newFunction
368
[25bca42]369DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
[44a81853]370 DeclarationNode * newnode = new DeclarationNode;
371 newnode->type = nullptr;
[bb7422a]372 std::vector<ast::ptr<ast::Expr>> exprs;
[44a81853]373 buildList( expr, exprs );
[b38f6da]374 newnode->attributes.push_back( new ast::Attribute( *name, std::move( exprs ) ) );
[44a81853]375 delete name;
376 return newnode;
377}
378
[2d019af]379DeclarationNode * DeclarationNode::newDirectiveStmt( StatementNode * stmt ) {
380 DeclarationNode * newnode = new DeclarationNode;
381 newnode->directiveStmt = stmt;
382 return newnode;
383}
384
[e994912]385DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
386 DeclarationNode * newnode = new DeclarationNode;
387 newnode->asmStmt = stmt;
388 return newnode;
389}
390
[bb7422a]391DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, ast::Expr * message ) {
[f6e3e34]392 DeclarationNode * newnode = new DeclarationNode;
393 newnode->assert.condition = condition;
394 newnode->assert.message = message;
395 return newnode;
396}
397
[bb7422a]398static void appendError( string & dst, const string & src ) {
[5b639ee]399 if ( src.empty() ) return;
400 if ( dst.empty() ) { dst = src; return; }
401 dst += ", " + src;
402} // appendError
403
[ba7aa2d]404void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
[bb7422a]405 const ast::CV::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
406 const ast::CV::Qualifiers duplicates = qsrc & qdst;
407
408 if ( duplicates.any() ) {
409 std::stringstream str;
410 str << "duplicate ";
411 ast::print( str, duplicates );
412 str << "qualifier(s)";
413 appendError( error, str.str() );
[a7c90d4]414 } // for
[c1c1112]415} // DeclarationNode::checkQualifiers
416
[a7c90d4]417void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
[bb7422a]418 ast::Function::Specs fsDups = funcSpecs & src->funcSpecs;
419 if ( fsDups.any() ) {
420 std::stringstream str;
421 str << "duplicate ";
422 ast::print( str, fsDups );
423 str << "function specifier(s)";
424 appendError( error, str.str() );
[dd020c0]425 } // if
426
[bb7422a]427 // Skip if everything is unset.
428 if ( storageClasses.any() && src->storageClasses.any() ) {
429 ast::Storage::Classes dups = storageClasses & src->storageClasses;
430 // Check for duplicates.
431 if ( dups.any() ) {
432 std::stringstream str;
433 str << "duplicate ";
434 ast::print( str, dups );
435 str << "storage class(es)";
436 appendError( error, str.str() );
437 // Check for conflicts.
438 } else if ( !src->storageClasses.is_threadlocal_any() ) {
439 std::stringstream str;
440 str << "conflicting ";
441 ast::print( str, ast::Storage::Classes( 1 << storageClasses.ffs() ) );
442 str << "& ";
443 ast::print( str, ast::Storage::Classes( 1 << src->storageClasses.ffs() ) );
444 str << "storage classes";
445 appendError( error, str.str() );
446 // FIX to preserve invariant of one basic storage specifier
447 src->storageClasses.reset();
448 }
[b6424d9]449 } // if
[dd020c0]450
[a7c90d4]451 appendError( error, src->error );
452} // DeclarationNode::checkSpecifiers
[b6424d9]453
[4eb3a7c5]454DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q, bool copyattr ) {
[6f95000]455 funcSpecs |= q->funcSpecs;
456 storageClasses |= q->storageClasses;
[c0aa336]457
[4eb3a7c5]458 if ( copyattr ) {
459 std::vector<ast::ptr<ast::Attribute>> tmp;
460 tmp.reserve( q->attributes.size() );
461 for ( auto const & attr : q->attributes ) {
462 tmp.emplace_back( ast::shallowCopy( attr.get() ) );
463 } // for
464 spliceBegin( attributes, tmp );
465 } // if
[bb7422a]466
[b6424d9]467 return this;
[a7c90d4]468} // DeclarationNode::copySpecifiers
[b6424d9]469
[ba7aa2d]470DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
[af9da5f]471 if ( ! q ) { return this; } // empty qualifier
[101e0bd]472
[a7c90d4]473 checkSpecifiers( q );
474 copySpecifiers( q );
[101e0bd]475
[c38ae92]476 if ( ! q->type ) { delete q; return this; }
[101e0bd]477
478 if ( ! type ) {
[c38ae92]479 type = q->type; // reuse structure
[1b772749]480 q->type = nullptr;
481 delete q;
[101e0bd]482 return this;
483 } // if
484
[a7c90d4]485 checkQualifiers( type, q->type );
[af60383]486 BuiltinType const builtin = type->builtintype;
[78e2fca]487 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.any() && error.length() == 0 ) {
[be00a2d]488 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, builtinTypeNames[builtin] );
[9dc31c10]489 } // if
[af60383]490 type = ::addQualifiers( q->type, type );
491 q->type = nullptr;
[6ef2d81]492
[b87a5ed]493 delete q;
494 return this;
[101e0bd]495} // addQualifiers
[3848e0e]496
[af60383]497DeclarationNode * DeclarationNode::addType( DeclarationNode * o, bool copyattr ) {
498 if ( !o ) return this;
499
500 checkSpecifiers( o );
501 copySpecifiers( o, copyattr );
502 if ( o->type ) {
503 type = ::addType( o->type, type, o->attributes );
504 o->type = nullptr;
[101e0bd]505 } // if
[af60383]506 if ( o->bitfieldWidth ) {
507 bitfieldWidth = o->bitfieldWidth;
[68cd1ce]508 } // if
[3848e0e]509
[af60383]510 // there may be typedefs chained onto the type
511 if ( o->next ) {
512 set_last( o->next->clone() );
[68cd1ce]513 } // if
[66406f3]514
[af60383]515 delete o;
[b87a5ed]516 return this;
[3848e0e]517}
518
[f135b50]519DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) {
[af60383]520 if ( o && o->type ) {
521 type->base = o->type;
[b38f6da]522 } // if
[f135b50]523 delete o;
524 return this;
525}
526
[ba7aa2d]527DeclarationNode * DeclarationNode::addTypedef() {
528 TypeData * newtype = new TypeData( TypeData::Symbolic );
[2298f728]529 newtype->symbolic.params = nullptr;
[8f6f47d7]530 newtype->symbolic.isTypedef = true;
[2298f728]531 newtype->symbolic.name = name ? new string( *name ) : nullptr;
[b87a5ed]532 newtype->base = type;
533 type = newtype;
534 return this;
[3848e0e]535}
536
[ba7aa2d]537DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
[bb7422a]538 if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
[702e826]539 if ( variable.assertions ) {
[dc3fbe5]540 variable.assertions->set_last( assertions );
[702e826]541 } else {
542 variable.assertions = assertions;
543 } // if
544 return this;
[2298f728]545 } // if
546
[b87a5ed]547 assert( type );
548 switch ( type->kind ) {
[0d0931d]549 case TypeData::Symbolic:
[8f6f47d7]550 if ( type->symbolic.assertions ) {
[dc3fbe5]551 type->symbolic.assertions->set_last( assertions );
[b87a5ed]552 } else {
[8f6f47d7]553 type->symbolic.assertions = assertions;
[68cd1ce]554 } // if
[b87a5ed]555 break;
[0d0931d]556 default:
[b87a5ed]557 assert( false );
[68cd1ce]558 } // switch
[974906e2]559
[b87a5ed]560 return this;
[51b73452]561}
562
[fb114fa1]563DeclarationNode * DeclarationNode::addName( string * newname ) {
[2298f728]564 assert( ! name );
565 name = newname;
[b87a5ed]566 return this;
[51b73452]567}
568
[c0aa336]569DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
[58dd019]570 assert( ! asmName );
[c0aa336]571 asmName = newname ? newname->asmName : nullptr;
572 return this->addQualifiers( newname );
[58dd019]573}
574
[ba7aa2d]575DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
[b87a5ed]576 bitfieldWidth = size;
577 return this;
[51b73452]578}
579
[ba7aa2d]580DeclarationNode * DeclarationNode::addVarArgs() {
[b87a5ed]581 assert( type );
582 hasEllipsis = true;
583 return this;
[51b73452]584}
585
[c453ac4]586DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
[b87a5ed]587 assert( type );
588 assert( type->kind == TypeData::Function );
[2298f728]589 assert( ! type->function.body );
[8f6f47d7]590 type->function.body = body;
[c453ac4]591 type->function.withExprs = withExprs;
[b87a5ed]592 return this;
[51b73452]593}
594
[ba7aa2d]595DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
[b87a5ed]596 assert( type );
597 assert( type->kind == TypeData::Function );
[2298f728]598 assert( ! type->function.oldDeclList );
[8f6f47d7]599 type->function.oldDeclList = list;
[b87a5ed]600 return this;
[51b73452]601}
602
[c0aa336]603DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
[b87a5ed]604 if ( type ) {
[af60383]605 type->setLastBase( newType );
[b87a5ed]606 } else {
607 type = newType;
[68cd1ce]608 } // if
[c0aa336]609 return this;
[3848e0e]610}
611
[c0aa336]612DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
613 if ( a ) {
[bb7422a]614 spliceBegin( attributes, a->attributes );
[c0aa336]615 a->attributes.clear();
616 } // if
617 return this;
618} // copyAttribute
619
[ba7aa2d]620DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
[b87a5ed]621 if ( p ) {
[6926a6d]622 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[c0aa336]623 setBase( p->type );
[2298f728]624 p->type = nullptr;
[c0aa336]625 copyAttribute( p );
[b87a5ed]626 delete p;
[68cd1ce]627 } // if
[b87a5ed]628 return this;
[3848e0e]629}
630
[ba7aa2d]631DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
[b87a5ed]632 if ( a ) {
633 assert( a->type->kind == TypeData::Array );
[c0aa336]634 setBase( a->type );
[2298f728]635 a->type = nullptr;
[c0aa336]636 copyAttribute( a );
[b87a5ed]637 delete a;
[68cd1ce]638 } // if
[b87a5ed]639 return this;
[51b73452]640}
641
[ba7aa2d]642DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
[b87a5ed]643 if ( p ) {
[e6cee92]644 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[b87a5ed]645 if ( type ) {
[af60383]646 p->type->base = makeNewBase( type );
[2298f728]647 type = nullptr;
[68cd1ce]648 } // if
[b87a5ed]649 delete this;
650 return p;
651 } else {
652 return this;
[68cd1ce]653 } // if
[51b73452]654}
655
[ba7aa2d]656DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
[0d0931d]657 if ( ! a ) return this;
[738e304]658 assert( a->type->kind == TypeData::Array );
659 if ( type ) {
[af60383]660 a->type->setLastBase( makeNewBase( type ) );
[738e304]661 type = nullptr;
[68cd1ce]662 } // if
[738e304]663 delete this;
664 return a;
[51b73452]665}
[3848e0e]666
[ba7aa2d]667DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
668 TypeData * ftype = new TypeData( TypeData::Function );
[8f6f47d7]669 ftype->function.params = params;
[c0aa336]670 setBase( ftype );
[b87a5ed]671 return this;
[3848e0e]672}
673
[ba7aa2d]674static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
[b87a5ed]675 if ( type ) {
676 if ( type->kind != TypeData::Function ) {
677 type->base = addIdListToType( type->base, ids );
678 } else {
[8f6f47d7]679 type->function.idList = ids;
[68cd1ce]680 } // if
[b87a5ed]681 return type;
[3848e0e]682 } else {
[ba7aa2d]683 TypeData * newtype = new TypeData( TypeData::Function );
[8f6f47d7]684 newtype->function.idList = ids;
[b87a5ed]685 return newtype;
[68cd1ce]686 } // if
[2298f728]687} // addIdListToType
[974906e2]688
[ba7aa2d]689DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
[b87a5ed]690 type = addIdListToType( type, ids );
691 return this;
[3848e0e]692}
693
[ba7aa2d]694DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
[b87a5ed]695 initializer = init;
696 return this;
[3848e0e]697}
698
[67cf18c]699DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
[bb7422a]700 assertf( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS, "Called addTypeInitializer on something that isn't a type variable." );
[67cf18c]701 variable.initializer = init;
702 return this;
703}
704
[a46b69c]705DeclarationNode * DeclarationNode::cloneType( string * name ) {
706 DeclarationNode * newnode = newName( name );
[5bf685f]707 newnode->type = maybeCopy( type );
[a7c90d4]708 newnode->copySpecifiers( this );
[b87a5ed]709 return newnode;
[3848e0e]710}
711
[4eb3a7c5]712DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o, bool copyattr ) {
[2298f728]713 if ( ! o ) return nullptr;
[4eb3a7c5]714 o->copySpecifiers( this, copyattr );
[2298f728]715 if ( type ) {
[af60383]716 o->type = ::cloneBaseType( type, o->type );
[68cd1ce]717 } // if
[b87a5ed]718 return o;
[51b73452]719}
720
[ba7aa2d]721DeclarationNode * DeclarationNode::extractAggregate() const {
[b87a5ed]722 if ( type ) {
[ba7aa2d]723 TypeData * ret = typeextractAggregate( type );
[b87a5ed]724 if ( ret ) {
[ba7aa2d]725 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]726 newnode->type = ret;
[4eb3a7c5]727 if ( ret->kind == TypeData::Aggregate ) {
728 newnode->attributes.swap( ret->aggregate.attributes );
729 } // if
[b87a5ed]730 return newnode;
[843054c2]731 } // if
732 } // if
[2298f728]733 return nullptr;
[3848e0e]734}
735
[b38f6da]736// If a typedef wraps an anonymous declaration, name the inner declaration so it has a consistent name across
737// translation units.
[45e753c]738static void nameTypedefedDecl(
739 DeclarationNode * innerDecl,
740 const DeclarationNode * outerDecl ) {
741 TypeData * outer = outerDecl->type;
742 assert( outer );
743 // First make sure this is a typedef:
744 if ( outer->kind != TypeData::Symbolic || !outer->symbolic.isTypedef ) {
745 return;
746 }
747 TypeData * inner = innerDecl->type;
748 assert( inner );
749 // Always clear any CVs associated with the aggregate:
750 inner->qualifiers.reset();
751 // Handle anonymous aggregates: typedef struct { int i; } foo
752 if ( inner->kind == TypeData::Aggregate && inner->aggregate.anon ) {
753 delete inner->aggregate.name;
754 inner->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
755 inner->aggregate.anon = false;
756 assert( outer->base );
757 delete outer->base->aggInst.aggregate->aggregate.name;
758 outer->base->aggInst.aggregate->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
759 outer->base->aggInst.aggregate->aggregate.anon = false;
760 outer->base->aggInst.aggregate->qualifiers.reset();
761 // Handle anonymous enumeration: typedef enum { A, B, C } foo
762 } else if ( inner->kind == TypeData::Enum && inner->enumeration.anon ) {
763 delete inner->enumeration.name;
764 inner->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
765 inner->enumeration.anon = false;
766 assert( outer->base );
767 delete outer->base->aggInst.aggregate->enumeration.name;
768 outer->base->aggInst.aggregate->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
769 outer->base->aggInst.aggregate->enumeration.anon = false;
770 // No qualifiers.reset() here.
771 }
772}
773
774// This code handles a special issue with the attribute transparent_union.
775//
776// typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union ))
777//
778// Here the attribute aligned goes with the typedef_name, so variables declared of this type are
779// aligned. However, the attribute transparent_union must be moved from the typedef_name to
780// alias union U. Currently, this is the only know attribute that must be moved from typedef to
781// alias.
782static void moveUnionAttribute( ast::Decl * decl, ast::UnionDecl * unionDecl ) {
783 if ( auto typedefDecl = dynamic_cast<ast::TypedefDecl *>( decl ) ) {
784 // Is the typedef alias a union aggregate?
785 if ( nullptr == unionDecl ) return;
786
787 // If typedef is an alias for a union, then its alias type was hoisted above and remembered.
788 if ( auto unionInstType = typedefDecl->base.as<ast::UnionInstType>() ) {
789 auto instType = ast::mutate( unionInstType );
790 // Remove all transparent_union attributes from typedef and move to alias union.
791 for ( auto attr = instType->attributes.begin() ; attr != instType->attributes.end() ; ) {
792 assert( *attr );
793 if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) {
794 unionDecl->attributes.emplace_back( attr->release() );
795 attr = instType->attributes.erase( attr );
796 } else {
797 attr++;
798 }
799 }
800 typedefDecl->base = instType;
801 }
802 }
803}
804
805// Get the non-anonymous name of the instance type of the declaration,
806// if one exists.
807static const std::string * getInstTypeOfName( ast::Decl * decl ) {
808 if ( auto dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
809 if ( auto aggr = dynamic_cast<ast::BaseInstType const *>( dwt->get_type() ) ) {
810 if ( aggr->name.find("anonymous") == std::string::npos ) {
811 return &aggr->name;
812 }
813 }
814 }
815 return nullptr;
816}
817
[b38f6da]818void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList ) {
[a16764a6]819 SemanticErrorException errors;
[bb7422a]820 std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( outputList );
[2298f728]821
[dc3fbe5]822 for ( const DeclarationNode * cur = firstNode ; cur ; cur = cur->next ) {
[b87a5ed]823 try {
[45e753c]824 bool extracted_named = false;
825 ast::UnionDecl * unionDecl = nullptr;
[692c1cc]826
[ba7aa2d]827 if ( DeclarationNode * extr = cur->extractAggregate() ) {
[78e2fca]828 assert( cur->type );
[45e753c]829 nameTypedefedDecl( extr, cur );
[692c1cc]830
[45e753c]831 if ( ast::Decl * decl = extr->build() ) {
[692c1cc]832 // Remember the declaration if it is a union aggregate ?
[bb7422a]833 unionDecl = dynamic_cast<ast::UnionDecl *>( decl );
[692c1cc]834
835 *out++ = decl;
[3d7e53b]836
837 // need to remember the cases where a declaration contains an anonymous aggregate definition
838 assert( extr->type );
839 if ( extr->type->kind == TypeData::Aggregate ) {
[692c1cc]840 // typedef struct { int A } B is the only case?
[4eb3a7c5]841 extracted_named = ! extr->type->aggregate.anon;
[3d7e53b]842 } else if ( extr->type->kind == TypeData::Enum ) {
[692c1cc]843 // typedef enum { A } B is the only case?
[4eb3a7c5]844 extracted_named = ! extr->type->enumeration.anon;
[45e753c]845 } else {
846 extracted_named = true;
[3d7e53b]847 }
[843054c2]848 } // if
[f39096c]849 delete extr;
[843054c2]850 } // if
[2298f728]851
[45e753c]852 if ( ast::Decl * decl = cur->build() ) {
853 moveUnionAttribute( decl, unionDecl );
854
855 if ( "" == decl->name && !cur->get_inLine() ) {
856 // Don't include anonymous declaration for named aggregates,
857 // but do include them for anonymous aggregates, e.g.:
858 // struct S {
859 // struct T { int x; }; // no anonymous member
860 // struct { int y; }; // anonymous member
861 // struct T; // anonymous member
862 // };
863 if ( extracted_named ) {
864 continue;
865 }
[692c1cc]866
[45e753c]867 if ( auto name = getInstTypeOfName( decl ) ) {
868 // Temporary: warn about anonymous member declarations of named types, since
869 // this conflicts with the syntax for the forward declaration of an anonymous type.
870 SemanticWarning( cur->location, Warning::AggrForwardDecl, name->c_str() );
871 }
[e07caa2]872 } // if
[45e753c]873 *out++ = decl;
[843054c2]874 } // if
[45e753c]875 } catch ( SemanticErrorException & e ) {
[b87a5ed]876 errors.append( e );
[843054c2]877 } // try
[e07caa2]878 } // for
[2298f728]879
[b87a5ed]880 if ( ! errors.isEmpty() ) {
881 throw errors;
[843054c2]882 } // if
[2298f728]883} // buildList
[3848e0e]884
[3d7e53b]885// currently only builds assertions, function parameters, and return values
[6611177]886void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ) {
[a16764a6]887 SemanticErrorException errors;
[bb7422a]888 std::back_insert_iterator<std::vector<ast::ptr<ast::DeclWithType>>> out( outputList );
[43c89a7]889
[dc3fbe5]890 for ( const DeclarationNode * cur = firstNode; cur; cur = cur->next ) {
[b87a5ed]891 try {
[bb7422a]892 ast::Decl * decl = cur->build();
[45e753c]893 assertf( decl, "buildList: build for ast::DeclWithType." );
[bb7422a]894 if ( ast::DeclWithType * dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
[47498bd]895 dwt->location = cur->location;
[3ca7ef3]896 *out++ = dwt;
[bb7422a]897 } else if ( ast::StructDecl * agg = dynamic_cast<ast::StructDecl *>( decl ) ) {
[3d7e53b]898 // e.g., int foo(struct S) {}
[bb7422a]899 auto inst = new ast::StructInstType( agg->name );
900 auto obj = new ast::ObjectDecl( cur->location, "", inst );
901 obj->linkage = linkage;
[3ca7ef3]902 *out++ = obj;
[47498bd]903 delete agg;
[bb7422a]904 } else if ( ast::UnionDecl * agg = dynamic_cast<ast::UnionDecl *>( decl ) ) {
[3d7e53b]905 // e.g., int foo(union U) {}
[bb7422a]906 auto inst = new ast::UnionInstType( agg->name );
907 auto obj = new ast::ObjectDecl( cur->location,
908 "", inst, nullptr, ast::Storage::Classes(),
909 linkage );
[3ca7ef3]910 *out++ = obj;
[bb7422a]911 } else if ( ast::EnumDecl * agg = dynamic_cast<ast::EnumDecl *>( decl ) ) {
[3d7e53b]912 // e.g., int foo(enum E) {}
[bb7422a]913 auto inst = new ast::EnumInstType( agg->name );
914 auto obj = new ast::ObjectDecl( cur->location,
915 "",
916 inst,
917 nullptr,
918 ast::Storage::Classes(),
919 linkage
920 );
[3ca7ef3]921 *out++ = obj;
[45e753c]922 } else {
923 assertf( false, "buildList: Could not convert to ast::DeclWithType." );
[843054c2]924 } // if
[45e753c]925 } catch ( SemanticErrorException & e ) {
[b87a5ed]926 errors.append( e );
[843054c2]927 } // try
[3a5131ed]928 } // for
929
[b87a5ed]930 if ( ! errors.isEmpty() ) {
931 throw errors;
[843054c2]932 } // if
[2298f728]933} // buildList
[3848e0e]934
[bb7422a]935void buildTypeList( const DeclarationNode * firstNode,
936 std::vector<ast::ptr<ast::Type>> & outputList ) {
[a16764a6]937 SemanticErrorException errors;
[bb7422a]938 std::back_insert_iterator<std::vector<ast::ptr<ast::Type>>> out( outputList );
[2298f728]939
[dc3fbe5]940 for ( const DeclarationNode * cur = firstNode ; cur ; cur = cur->next ) {
[b87a5ed]941 try {
[ba7aa2d]942 * out++ = cur->buildType();
[45e753c]943 } catch ( SemanticErrorException & e ) {
[b87a5ed]944 errors.append( e );
[843054c2]945 } // try
[45e753c]946 } // for
[2298f728]947
[b87a5ed]948 if ( ! errors.isEmpty() ) {
949 throw errors;
[843054c2]950 } // if
[2298f728]951} // buildTypeList
[51b73452]952
[bb7422a]953ast::Decl * DeclarationNode::build() const {
[a16764a6]954 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
[2298f728]955
[e994912]956 if ( asmStmt ) {
[bb7422a]957 auto stmt = strict_dynamic_cast<ast::AsmStmt *>( asmStmt->build() );
958 return new ast::AsmDecl( stmt->location, stmt );
[e994912]959 } // if
[2d019af]960 if ( directiveStmt ) {
[bb7422a]961 auto stmt = strict_dynamic_cast<ast::DirectiveStmt *>( directiveStmt->build() );
962 return new ast::DirectiveDecl( stmt->location, stmt );
[2d019af]963 } // if
[e994912]964
[bb7422a]965 if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
[f0ecf9b]966 // otype is internally converted to dtype + otype parameters
[bb7422a]967 static const ast::TypeDecl::Kind kindMap[] = { ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Ftype, ast::TypeDecl::Ttype, ast::TypeDecl::Dimension };
968 static_assert( sizeof(kindMap) / sizeof(kindMap[0]) == ast::TypeDecl::NUMBER_OF_KINDS, "DeclarationNode::build: kindMap is out of sync." );
[8f60f0b]969 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
[bb7422a]970 ast::TypeDecl * ret = new ast::TypeDecl( location,
971 *name,
972 ast::Storage::Classes(),
973 (ast::Type *)nullptr,
974 kindMap[ variable.tyClass ],
975 variable.tyClass == ast::TypeDecl::Otype || variable.tyClass == ast::TypeDecl::DStype,
976 variable.initializer ? variable.initializer->buildType() : nullptr
977 );
978 buildList( variable.assertions, ret->assertions );
[2298f728]979 return ret;
980 } // if
981
[843054c2]982 if ( type ) {
[dd020c0]983 // Function specifiers can only appear on a function definition/declaration.
984 //
985 // inline _Noreturn int f(); // allowed
986 // inline _Noreturn int g( int i ); // allowed
987 // inline _Noreturn int i; // disallowed
[fb04321]988 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
[a16764a6]989 SemanticError( this, "invalid function specifier for " );
[dd020c0]990 } // if
[284da8c]991 // Forall qualifier can only appear on a function/aggregate definition/declaration.
992 //
993 // forall int f(); // allowed
994 // forall int g( int i ); // allowed
995 // forall int i; // disallowed
996 if ( type->kind != TypeData::Function && type->forall ) {
997 SemanticError( this, "invalid type qualifier for " );
998 } // if
[3ed994e]999 bool isDelete = initializer && initializer->get_isDelete();
[bb7422a]1000 ast::Decl * decl = buildDecl(
1001 type,
1002 name ? *name : string( "" ),
1003 storageClasses,
1004 maybeBuild( bitfieldWidth ),
1005 funcSpecs,
1006 linkage,
1007 asmName,
1008 isDelete ? nullptr : maybeBuild( initializer ),
1009 copy( attributes )
1010 )->set_extension( extension );
[3ed994e]1011 if ( isDelete ) {
[bb7422a]1012 auto dwt = strict_dynamic_cast<ast::DeclWithType *>( decl );
[3ed994e]1013 dwt->isDeleted = true;
1014 }
1015 return decl;
[843054c2]1016 } // if
[2298f728]1017
[f6e3e34]1018 if ( assert.condition ) {
[bb7422a]1019 auto cond = maybeBuild( assert.condition );
1020 auto msg = strict_dynamic_cast<ast::ConstantExpr *>( maybeCopy( assert.message ) );
1021 return new ast::StaticAssertDecl( location, cond, msg );
[f6e3e34]1022 }
1023
[dd020c0]1024 // SUE's cannot have function specifiers, either
1025 //
[79aae15]1026 // inline _Noreturn struct S { ... }; // disallowed
1027 // inline _Noreturn enum E { ... }; // disallowed
[fb04321]1028 if ( funcSpecs.any() ) {
[a16764a6]1029 SemanticError( this, "invalid function specifier for " );
[843054c2]1030 } // if
[e874605]1031 if ( enumInLine ) {
[bb7422a]1032 return new ast::InlineMemberDecl( location,
1033 *name, (ast::Type*)nullptr, storageClasses, linkage );
[e874605]1034 } // if
[dd020c0]1035 assertf( name, "ObjectDecl must a have name\n" );
[bb7422a]1036 auto ret = new ast::ObjectDecl( location,
1037 *name,
1038 (ast::Type*)nullptr,
1039 maybeBuild( initializer ),
1040 storageClasses,
1041 linkage,
1042 maybeBuild( bitfieldWidth )
1043 );
1044 ret->asmName = asmName;
1045 ret->extension = extension;
1046 return ret;
[51b73452]1047}
1048
[bb7422a]1049ast::Type * DeclarationNode::buildType() const {
[b87a5ed]1050 assert( type );
[974906e2]1051
[b87a5ed]1052 switch ( type->kind ) {
[0d0931d]1053 case TypeData::Enum:
1054 case TypeData::Aggregate: {
[bb7422a]1055 ast::BaseInstType * ret =
1056 buildComAggInst( type, copy( attributes ), linkage );
1057 buildList( type->aggregate.actuals, ret->params );
[0d0931d]1058 return ret;
1059 }
1060 case TypeData::Symbolic: {
[bb7422a]1061 ast::TypeInstType * ret = new ast::TypeInstType(
1062 *type->symbolic.name,
1063 // This is just a default, the true value is not known yet.
1064 ast::TypeDecl::Dtype,
1065 buildQualifiers( type ),
1066 copy( attributes ) );
1067 buildList( type->symbolic.actuals, ret->params );
[0d0931d]1068 return ret;
1069 }
1070 default:
[bb7422a]1071 ast::Type * simpletypes = typebuild( type );
1072 // copy because member is const
1073 simpletypes->attributes = attributes;
[c0aa336]1074 return simpletypes;
[b87a5ed]1075 } // switch
[3848e0e]1076}
1077
[b87a5ed]1078// Local Variables: //
1079// tab-width: 4 //
1080// mode: c++ //
1081// compile-command: "make install" //
1082// End: //
Note: See TracBrowser for help on using the repository browser.