source: src/Parser/DeclarationNode.cc@ 75d789c

Last change on this file since 75d789c was e048ece, checked in by Andrew Beach <ajbeach@…>, 19 months ago

Moved the DeclarationNode enums over to TypeData where they are actually used.

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