source: src/Parser/DeclarationNode.cc@ 3a513d89

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

Save progress

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