source: src/Parser/DeclarationNode.cc@ 56b47b9

Last change on this file since 56b47b9 was af60383, checked in by Andrew Beach <ajbeach@…>, 19 months ago

Moved a field and functions from DeclarationNode to TypeData. Trying to make the line between them cleaner.

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