source: src/Parser/DeclarationNode.cc@ 4d6d62e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since 4d6d62e was 099e202, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

  • Property mode set to 100644
File size: 37.0 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 : Wed May 16 09:37:17 2018
13// Update Count : 1070
14//
15
16#include <cassert> // for assert, assertf, strict_dynamic_cast
17#include <iterator> // for back_insert_iterator
18#include <list> // for list
19#include <memory> // for unique_ptr
20#include <ostream> // for operator<<, ostream, basic_ostream
21#include <string> // for string, operator+, allocator, char...
22
23#include "Common/SemanticError.h" // for SemanticError
24#include "Common/UniqueName.h" // for UniqueName
25#include "Common/utility.h" // for maybeClone, maybeBuild, CodeLocation
26#include "Parser/LinkageSpec.h" // for Spec, linkageName, Cforall
27#include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
28#include "SynTree/Attribute.h" // for Attribute
29#include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, Declaration
30#include "SynTree/Expression.h" // for Expression, ConstantExpr
31#include "SynTree/Statement.h" // for AsmStmt
32#include "SynTree/Type.h" // for Type, Type::StorageClasses, Type::...
33#include "TypeData.h" // for TypeData, TypeData::Aggregate_t
34#include "TypedefTable.h" // for TypedefTable, TypedefTable::kind_t...
35
36class Initializer;
37
38extern TypedefTable typedefTable;
39
40using namespace std;
41
42// These must harmonize with the corresponding DeclarationNode enumerations.
43const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "int128", "float80", "float128", "NoBasicTypeNames" };
44const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
45const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
46const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
47const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" };
48const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
49const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "zero_t", "one_t", "NoBuiltinTypeNames" };
50
51UniqueName DeclarationNode::anonymous( "__anonymous" );
52
53extern LinkageSpec::Spec linkage; // defined in parser.yy
54
55DeclarationNode::DeclarationNode() :
56 builtin( NoBuiltinType ),
57 type( nullptr ),
58 bitfieldWidth( nullptr ),
59 hasEllipsis( false ),
60 linkage( ::linkage ),
61 asmName( nullptr ),
62 initializer( nullptr ),
63 extension( false ),
64 asmStmt( nullptr ) {
65
66// variable.name = nullptr;
67 variable.tyClass = NoTypeClass;
68 variable.assertions = nullptr;
69 variable.initializer = nullptr;
70
71// attr.name = nullptr;
72 attr.expr = nullptr;
73 attr.type = nullptr;
74
75 assert.condition = nullptr;
76 assert.message = nullptr;
77}
78
79DeclarationNode::~DeclarationNode() {
80// delete attr.name;
81 delete attr.expr;
82 delete attr.type;
83
84// delete variable.name;
85 delete variable.assertions;
86 delete variable.initializer;
87
88 delete type;
89 delete bitfieldWidth;
90
91 delete asmStmt;
92 // asmName, no delete, passed to next stage
93 delete initializer;
94
95 delete assert.condition;
96 delete assert.message;
97}
98
99DeclarationNode * DeclarationNode::clone() const {
100 DeclarationNode * newnode = new DeclarationNode;
101 newnode->set_next( maybeClone( get_next() ) );
102 newnode->name = name ? new string( *name ) : nullptr;
103
104 newnode->builtin = NoBuiltinType;
105 newnode->type = maybeClone( type );
106 newnode->storageClasses = storageClasses;
107 newnode->funcSpecs = funcSpecs;
108 newnode->bitfieldWidth = maybeClone( bitfieldWidth );
109 newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
110 newnode->hasEllipsis = hasEllipsis;
111 newnode->linkage = linkage;
112 newnode->asmName = maybeClone( asmName );
113 cloneAll( attributes, newnode->attributes );
114 newnode->initializer = maybeClone( initializer );
115 newnode->extension = extension;
116 newnode->asmStmt = maybeClone( asmStmt );
117 newnode->error = error;
118
119// newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
120 newnode->variable.tyClass = variable.tyClass;
121 newnode->variable.assertions = maybeClone( variable.assertions );
122 newnode->variable.initializer = maybeClone( variable.initializer );
123
124// newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
125 newnode->attr.expr = maybeClone( attr.expr );
126 newnode->attr.type = maybeClone( attr.type );
127
128 newnode->assert.condition = maybeClone( assert.condition );
129 newnode->assert.message = maybeClone( assert.message );
130 return newnode;
131} // DeclarationNode::clone
132
133void DeclarationNode::print( std::ostream &os, int indent ) const {
134 os << string( indent, ' ' );
135 if ( name ) {
136 os << *name << ": ";
137 } else {
138 os << "unnamed: ";
139 } // if
140
141 if ( linkage != LinkageSpec::Cforall ) {
142 os << LinkageSpec::linkageName( linkage ) << " ";
143 } // if
144
145 storageClasses.print( os );
146 funcSpecs.print( os );
147
148 if ( type ) {
149 type->print( os, indent );
150 } else {
151 os << "untyped entity ";
152 } // if
153
154 if ( bitfieldWidth ) {
155 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
156 bitfieldWidth->printOneLine( os );
157 } // if
158
159 if ( initializer ) {
160 os << endl << string( indent + 2, ' ' ) << "with initializer ";
161 initializer->printOneLine( os );
162 os << " maybe constructed? " << initializer->get_maybeConstructed();
163
164 } // if
165
166 os << endl;
167}
168
169void DeclarationNode::printList( std::ostream &os, int indent ) const {
170 ParseNode::printList( os, indent );
171 if ( hasEllipsis ) {
172 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
173 } // if
174}
175
176DeclarationNode * DeclarationNode::newFunction( string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) {
177 DeclarationNode * newnode = new DeclarationNode;
178 newnode->name = name;
179 newnode->type = new TypeData( TypeData::Function );
180 newnode->type->function.params = param;
181 newnode->type->function.body = body;
182
183 // ignore unnamed routine declarations: void p( int (*)(int) );
184 if ( newnode->name ) {
185 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
186 } // if
187
188 if ( ret ) {
189 newnode->type->base = ret->type;
190 ret->type = nullptr;
191 delete ret;
192 } // if
193
194 return newnode;
195} // DeclarationNode::newFunction
196
197
198DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) {
199 DeclarationNode * newnode = new DeclarationNode;
200 newnode->storageClasses = sc;
201 return newnode;
202} // DeclarationNode::newStorageClass
203
204DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) {
205 DeclarationNode * newnode = new DeclarationNode;
206 newnode->funcSpecs = fs;
207 return newnode;
208} // DeclarationNode::newFuncSpecifier
209
210DeclarationNode * DeclarationNode::newTypeQualifier( Type::Qualifiers tq ) {
211 DeclarationNode * newnode = new DeclarationNode;
212 newnode->type = new TypeData();
213 newnode->type->qualifiers = tq;
214 return newnode;
215} // DeclarationNode::newQualifier
216
217DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
218 DeclarationNode * newnode = new DeclarationNode;
219 newnode->type = new TypeData( TypeData::Basic );
220 newnode->type->basictype = bt;
221 return newnode;
222} // DeclarationNode::newBasicType
223
224DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
225 DeclarationNode * newnode = new DeclarationNode;
226 newnode->type = new TypeData( TypeData::Basic );
227 newnode->type->complextype = ct;
228 return newnode;
229} // DeclarationNode::newComplexType
230
231DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
232 DeclarationNode * newnode = new DeclarationNode;
233 newnode->type = new TypeData( TypeData::Basic );
234 newnode->type->signedness = sn;
235 return newnode;
236} // DeclarationNode::newSignedNess
237
238DeclarationNode * DeclarationNode::newLength( Length lnth ) {
239 DeclarationNode * newnode = new DeclarationNode;
240 newnode->type = new TypeData( TypeData::Basic );
241 newnode->type->length = lnth;
242 return newnode;
243} // DeclarationNode::newLength
244
245DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
246 DeclarationNode * newnode = new DeclarationNode;
247 newnode->type = new TypeData( TypeData::Unknown );
248 newnode->type->forall = forall;
249 return newnode;
250} // DeclarationNode::newForall
251
252DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
253 DeclarationNode * newnode = new DeclarationNode;
254 newnode->type = new TypeData( TypeData::SymbolicInst );
255 newnode->type->symbolic.name = name;
256 newnode->type->symbolic.isTypedef = true;
257 newnode->type->symbolic.params = nullptr;
258 return newnode;
259} // DeclarationNode::newFromTypedef
260
261DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
262 assert( name );
263 DeclarationNode * newnode = new DeclarationNode;
264 newnode->type = new TypeData( TypeData::Aggregate );
265 newnode->type->aggregate.kind = kind;
266 newnode->type->aggregate.name = name;
267 newnode->type->aggregate.actuals = actuals;
268 newnode->type->aggregate.fields = fields;
269 newnode->type->aggregate.body = body;
270 newnode->type->aggregate.tagged = false;
271 newnode->type->aggregate.parent = nullptr;
272 return newnode;
273} // DeclarationNode::newAggregate
274
275DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants, bool body ) {
276 assert( name );
277 DeclarationNode * newnode = new DeclarationNode;
278 newnode->type = new TypeData( TypeData::Enum );
279 newnode->type->enumeration.name = name;
280 newnode->type->enumeration.constants = constants;
281 newnode->type->enumeration.body = body;
282 return newnode;
283} // DeclarationNode::newEnum
284
285DeclarationNode * DeclarationNode::newEnumConstant( string * name, ExpressionNode * constant ) {
286 DeclarationNode * newnode = new DeclarationNode;
287 newnode->name = name;
288 newnode->enumeratorValue.reset( constant );
289 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
290 return newnode;
291} // DeclarationNode::newEnumConstant
292
293DeclarationNode * DeclarationNode::newName( string * name ) {
294 DeclarationNode * newnode = new DeclarationNode;
295 newnode->name = name;
296 return newnode;
297} // DeclarationNode::newName
298
299DeclarationNode * DeclarationNode::newFromTypeGen( string * name, ExpressionNode * params ) {
300 DeclarationNode * newnode = new DeclarationNode;
301 newnode->type = new TypeData( TypeData::SymbolicInst );
302 newnode->type->symbolic.name = name;
303 newnode->type->symbolic.isTypedef = false;
304 newnode->type->symbolic.actuals = params;
305 return newnode;
306} // DeclarationNode::newFromTypeGen
307
308DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, string * name ) {
309 DeclarationNode * newnode = new DeclarationNode;
310 newnode->type = nullptr;
311 assert( ! newnode->name );
312// newnode->variable.name = name;
313 newnode->name = name;
314 newnode->variable.tyClass = tc;
315 newnode->variable.assertions = nullptr;
316 return newnode;
317} // DeclarationNode::newTypeParam
318
319DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
320 DeclarationNode * newnode = new DeclarationNode;
321 newnode->type = new TypeData( TypeData::Aggregate );
322 newnode->type->aggregate.name = name;
323 newnode->type->aggregate.kind = Trait;
324 newnode->type->aggregate.params = params;
325 newnode->type->aggregate.fields = asserts;
326 return newnode;
327} // DeclarationNode::newTrait
328
329DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
330 DeclarationNode * newnode = new DeclarationNode;
331 newnode->type = new TypeData( TypeData::AggregateInst );
332 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
333 newnode->type->aggInst.aggregate->aggregate.kind = Trait;
334 newnode->type->aggInst.aggregate->aggregate.name = name;
335 newnode->type->aggInst.params = params;
336 return newnode;
337} // DeclarationNode::newTraitUse
338
339DeclarationNode * DeclarationNode::newTypeDecl( string * name, DeclarationNode * typeParams ) {
340 DeclarationNode * newnode = new DeclarationNode;
341 newnode->name = name;
342 newnode->type = new TypeData( TypeData::Symbolic );
343 newnode->type->symbolic.isTypedef = false;
344 newnode->type->symbolic.params = typeParams;
345 return newnode;
346} // DeclarationNode::newTypeDecl
347
348DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
349 DeclarationNode * newnode = new DeclarationNode;
350 newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
351 if ( kind == OperKinds::And ) {
352 // T && is parsed as 'And' operator rather than two references => add a second reference type
353 TypeData * td = new TypeData( TypeData::Reference );
354 td->base = newnode->type;
355 newnode->type = td;
356 }
357 if ( qualifiers ) {
358 return newnode->addQualifiers( qualifiers );
359 } else {
360 return newnode;
361 } // if
362} // DeclarationNode::newPointer
363
364DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
365 DeclarationNode * newnode = new DeclarationNode;
366 newnode->type = new TypeData( TypeData::Array );
367 newnode->type->array.dimension = size;
368 newnode->type->array.isStatic = isStatic;
369 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
370 newnode->type->array.isVarLen = false;
371 } else {
372 newnode->type->array.isVarLen = true;
373 } // if
374 return newnode->addQualifiers( qualifiers );
375} // DeclarationNode::newArray
376
377DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
378 DeclarationNode * newnode = new DeclarationNode;
379 newnode->type = new TypeData( TypeData::Array );
380 newnode->type->array.dimension = nullptr;
381 newnode->type->array.isStatic = false;
382 newnode->type->array.isVarLen = true;
383 return newnode->addQualifiers( qualifiers );
384}
385
386DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
387 DeclarationNode * newnode = new DeclarationNode;
388 newnode->bitfieldWidth = size;
389 return newnode;
390}
391
392DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
393 DeclarationNode * newnode = new DeclarationNode;
394 newnode->type = new TypeData( TypeData::Tuple );
395 newnode->type->tuple = members;
396 return newnode;
397}
398
399DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
400 DeclarationNode * newnode = new DeclarationNode;
401 newnode->type = new TypeData( TypeData::Typeof );
402 newnode->type->typeexpr = expr;
403 return newnode;
404}
405
406DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
407 DeclarationNode * newnode = new DeclarationNode;
408 newnode->type = new TypeData( TypeData::Builtin );
409 newnode->builtin = bt;
410 newnode->type->builtintype = newnode->builtin;
411 return newnode;
412} // DeclarationNode::newBuiltinType
413
414DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) {
415 DeclarationNode * newnode = new DeclarationNode;
416 newnode->type = nullptr;
417// newnode->attr.name = name;
418 newnode->name = name;
419 newnode->attr.expr = expr;
420 return newnode;
421}
422
423DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) {
424 DeclarationNode * newnode = new DeclarationNode;
425 newnode->type = nullptr;
426// newnode->attr.name = name;
427 newnode->name = name;
428 newnode->attr.type = type;
429 return newnode;
430}
431
432DeclarationNode * DeclarationNode::newAttribute( string * name, ExpressionNode * expr ) {
433 DeclarationNode * newnode = new DeclarationNode;
434 newnode->type = nullptr;
435 std::list< Expression * > exprs;
436 buildList( expr, exprs );
437 newnode->attributes.push_back( new Attribute( *name, exprs ) );
438 delete name;
439 return newnode;
440}
441
442DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
443 DeclarationNode * newnode = new DeclarationNode;
444 newnode->asmStmt = stmt;
445 return newnode;
446}
447
448DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, Expression * message ) {
449 DeclarationNode * newnode = new DeclarationNode;
450 newnode->assert.condition = condition;
451 newnode->assert.message = message;
452 return newnode;
453}
454
455
456void appendError( string & dst, const string & src ) {
457 if ( src.empty() ) return;
458 if ( dst.empty() ) { dst = src; return; }
459 dst += ", " + src;
460} // appendError
461
462void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
463 const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
464
465 if ( (qsrc & qdst).any() ) { // duplicates ?
466 for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates
467 if ( qsrc[i] && qdst[i] ) {
468 appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] );
469 } // if
470 } // for
471 } // for
472} // DeclarationNode::checkQualifiers
473
474void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
475 if ( (funcSpecs & src->funcSpecs).any() ) { // duplicates ?
476 for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates
477 if ( funcSpecs[i] && src->funcSpecs[i] ) {
478 appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] );
479 } // if
480 } // for
481 } // if
482
483 if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ?
484 if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ?
485 for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates
486 if ( storageClasses[i] && src->storageClasses[i] ) {
487 appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] );
488 } // if
489 } // for
490 // src is the new item being added and has a single bit
491 } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ?
492 appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] +
493 " & " + Type::StorageClassesNames[src->storageClasses.ffs()] );
494 src->storageClasses.reset(); // FIX to preserve invariant of one basic storage specifier
495 } // if
496 } // if
497
498 appendError( error, src->error );
499} // DeclarationNode::checkSpecifiers
500
501DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
502 funcSpecs |= q->funcSpecs;
503 storageClasses |= q->storageClasses;
504
505 for ( Attribute *attr: reverseIterate( q->attributes ) ) {
506 attributes.push_front( attr->clone() );
507 } // for
508 return this;
509} // DeclarationNode::copySpecifiers
510
511static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
512 if ( src->forall && dst->kind == TypeData::Function ) {
513 if ( dst->forall ) {
514 dst->forall->appendList( src->forall );
515 } else {
516 dst->forall = src->forall;
517 } // if
518 src->forall = nullptr;
519 } // if
520 if ( dst->base ) {
521 addQualifiersToType( src, dst->base );
522 } else if ( dst->kind == TypeData::Function ) {
523 dst->base = src;
524 src = nullptr;
525 } else {
526 dst->qualifiers |= src->qualifiers;
527 } // if
528} // addQualifiersToType
529
530DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
531 if ( ! q ) { return this; } // empty qualifier
532
533 checkSpecifiers( q );
534 copySpecifiers( q );
535
536 if ( ! q->type ) { delete q; return this; }
537
538 if ( ! type ) {
539 type = q->type; // reuse structure
540 q->type = nullptr;
541 delete q;
542 return this;
543 } // if
544
545 if ( q->type->forall ) { // forall qualifier ?
546 if ( type->forall ) { // polymorphic routine ?
547 type->forall->appendList( q->type->forall ); // augment forall qualifier
548 } else {
549 if ( type->kind == TypeData::Aggregate ) { // struct/union ?
550 if ( type->aggregate.params ) { // polymorphic ?
551 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
552 } else { // not polymorphic
553 type->aggregate.params = q->type->forall; // make polymorphic type
554 // change implicit typedef from TYPEDEFname to TYPEGENname
555 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG );
556 } // if
557 } else { // not polymorphic
558 type->forall = q->type->forall; // make polymorphic routine
559 } // if
560 } // if
561 q->type->forall = nullptr; // forall qualifier moved
562 } // if
563
564 checkQualifiers( type, q->type );
565 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
566 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
567 } // if
568 addQualifiersToType( q->type, type );
569
570 delete q;
571 return this;
572} // addQualifiers
573
574static void addTypeToType( TypeData *&src, TypeData *&dst ) {
575 if ( src->forall && dst->kind == TypeData::Function ) {
576 if ( dst->forall ) {
577 dst->forall->appendList( src->forall );
578 } else {
579 dst->forall = src->forall;
580 } // if
581 src->forall = nullptr;
582 } // if
583 if ( dst->base ) {
584 addTypeToType( src, dst->base );
585 } else {
586 switch ( dst->kind ) {
587 case TypeData::Unknown:
588 src->qualifiers |= dst->qualifiers;
589 dst = src;
590 src = nullptr;
591 break;
592 case TypeData::Basic:
593 dst->qualifiers |= src->qualifiers;
594 if ( src->kind != TypeData::Unknown ) {
595 assert( src->kind == TypeData::Basic );
596
597 if ( dst->basictype == DeclarationNode::NoBasicType ) {
598 dst->basictype = src->basictype;
599 } else if ( src->basictype != DeclarationNode::NoBasicType )
600 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
601
602 if ( dst->complextype == DeclarationNode::NoComplexType ) {
603 dst->complextype = src->complextype;
604 } else if ( src->complextype != DeclarationNode::NoComplexType )
605 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
606
607 if ( dst->signedness == DeclarationNode::NoSignedness ) {
608 dst->signedness = src->signedness;
609 } else if ( src->signedness != DeclarationNode::NoSignedness )
610 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
611
612 if ( dst->length == DeclarationNode::NoLength ) {
613 dst->length = src->length;
614 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
615 dst->length = DeclarationNode::LongLong;
616 } else if ( src->length != DeclarationNode::NoLength )
617 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
618 } // if
619 break;
620 default:
621 switch ( src->kind ) {
622 case TypeData::Aggregate:
623 case TypeData::Enum:
624 dst->base = new TypeData( TypeData::AggregateInst );
625 dst->base->aggInst.aggregate = src;
626 if ( src->kind == TypeData::Aggregate ) {
627 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
628 } // if
629 dst->base->qualifiers |= src->qualifiers;
630 src = nullptr;
631 break;
632 default:
633 if ( dst->forall ) {
634 dst->forall->appendList( src->forall );
635 } else {
636 dst->forall = src->forall;
637 } // if
638 src->forall = nullptr;
639 dst->base = src;
640 src = nullptr;
641 } // switch
642 } // switch
643 } // if
644}
645
646DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
647 if ( o ) {
648 checkSpecifiers( o );
649 copySpecifiers( o );
650 if ( o->type ) {
651 if ( ! type ) {
652 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
653 type = new TypeData( TypeData::AggregateInst );
654 type->aggInst.aggregate = o->type;
655 if ( o->type->kind == TypeData::Aggregate ) {
656 type->aggInst.hoistType = o->type->aggregate.body;
657 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
658 } else {
659 type->aggInst.hoistType = o->type->enumeration.body;
660 } // if
661 type->qualifiers |= o->type->qualifiers;
662 } else {
663 type = o->type;
664 } // if
665 o->type = nullptr;
666 } else {
667 addTypeToType( o->type, type );
668 } // if
669 } // if
670 if ( o->bitfieldWidth ) {
671 bitfieldWidth = o->bitfieldWidth;
672 } // if
673
674 // there may be typedefs chained onto the type
675 if ( o->get_next() ) {
676 set_last( o->get_next()->clone() );
677 } // if
678 } // if
679 delete o;
680 return this;
681}
682
683DeclarationNode * DeclarationNode::addTypedef() {
684 TypeData * newtype = new TypeData( TypeData::Symbolic );
685 newtype->symbolic.params = nullptr;
686 newtype->symbolic.isTypedef = true;
687 newtype->symbolic.name = name ? new string( *name ) : nullptr;
688 newtype->base = type;
689 type = newtype;
690 return this;
691}
692
693DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
694 if ( variable.tyClass != NoTypeClass ) {
695 if ( variable.assertions ) {
696 variable.assertions->appendList( assertions );
697 } else {
698 variable.assertions = assertions;
699 } // if
700 return this;
701 } // if
702
703 assert( type );
704 switch ( type->kind ) {
705 case TypeData::Symbolic:
706 if ( type->symbolic.assertions ) {
707 type->symbolic.assertions->appendList( assertions );
708 } else {
709 type->symbolic.assertions = assertions;
710 } // if
711 break;
712 default:
713 assert( false );
714 } // switch
715
716 return this;
717}
718
719DeclarationNode * DeclarationNode::addName( string * newname ) {
720 assert( ! name );
721 name = newname;
722 return this;
723}
724
725DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
726 assert( ! asmName );
727 asmName = newname ? newname->asmName : nullptr;
728 return this->addQualifiers( newname );
729}
730
731DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
732 bitfieldWidth = size;
733 return this;
734}
735
736DeclarationNode * DeclarationNode::addVarArgs() {
737 assert( type );
738 hasEllipsis = true;
739 return this;
740}
741
742DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
743 assert( type );
744 assert( type->kind == TypeData::Function );
745 assert( ! type->function.body );
746 type->function.body = body;
747 type->function.withExprs = withExprs;
748 return this;
749}
750
751DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
752 assert( type );
753 assert( type->kind == TypeData::Function );
754 assert( ! type->function.oldDeclList );
755 type->function.oldDeclList = list;
756 return this;
757}
758
759DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
760 if ( type ) {
761 TypeData * prevBase = type;
762 TypeData * curBase = type->base;
763 while ( curBase != nullptr ) {
764 prevBase = curBase;
765 curBase = curBase->base;
766 } // while
767 prevBase->base = newType;
768 } else {
769 type = newType;
770 } // if
771 return this;
772}
773
774DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
775 if ( a ) {
776 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
777 attributes.push_front( attr );
778 } // for
779 a->attributes.clear();
780 } // if
781 return this;
782} // copyAttribute
783
784DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
785 if ( p ) {
786 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
787 setBase( p->type );
788 p->type = nullptr;
789 copyAttribute( p );
790 delete p;
791 } // if
792 return this;
793}
794
795DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
796 if ( a ) {
797 assert( a->type->kind == TypeData::Array );
798 setBase( a->type );
799 a->type = nullptr;
800 copyAttribute( a );
801 delete a;
802 } // if
803 return this;
804}
805
806DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
807 if ( p ) {
808 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
809 if ( type ) {
810 switch ( type->kind ) {
811 case TypeData::Aggregate:
812 case TypeData::Enum:
813 p->type->base = new TypeData( TypeData::AggregateInst );
814 p->type->base->aggInst.aggregate = type;
815 if ( type->kind == TypeData::Aggregate ) {
816 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
817 } // if
818 p->type->base->qualifiers |= type->qualifiers;
819 break;
820
821 default:
822 p->type->base = type;
823 } // switch
824 type = nullptr;
825 } // if
826 delete this;
827 return p;
828 } else {
829 return this;
830 } // if
831}
832
833static TypeData * findLast( TypeData * a ) {
834 assert( a );
835 TypeData * cur = a;
836 while ( cur->base ) {
837 cur = cur->base;
838 } // while
839 return cur;
840}
841
842DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
843 if ( ! a ) return this;
844 assert( a->type->kind == TypeData::Array );
845 TypeData * lastArray = findLast( a->type );
846 if ( type ) {
847 switch ( type->kind ) {
848 case TypeData::Aggregate:
849 case TypeData::Enum:
850 lastArray->base = new TypeData( TypeData::AggregateInst );
851 lastArray->base->aggInst.aggregate = type;
852 if ( type->kind == TypeData::Aggregate ) {
853 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
854 } // if
855 lastArray->base->qualifiers |= type->qualifiers;
856 break;
857 default:
858 lastArray->base = type;
859 } // switch
860 type = nullptr;
861 } // if
862 delete this;
863 return a;
864}
865
866DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
867 TypeData * ftype = new TypeData( TypeData::Function );
868 ftype->function.params = params;
869 setBase( ftype );
870 return this;
871}
872
873static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
874 if ( type ) {
875 if ( type->kind != TypeData::Function ) {
876 type->base = addIdListToType( type->base, ids );
877 } else {
878 type->function.idList = ids;
879 } // if
880 return type;
881 } else {
882 TypeData * newtype = new TypeData( TypeData::Function );
883 newtype->function.idList = ids;
884 return newtype;
885 } // if
886} // addIdListToType
887
888DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
889 type = addIdListToType( type, ids );
890 return this;
891}
892
893DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
894 initializer = init;
895 return this;
896}
897
898DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
899 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
900 variable.initializer = init;
901 return this;
902}
903
904DeclarationNode * DeclarationNode::cloneType( string * newName ) {
905 DeclarationNode * newnode = new DeclarationNode;
906 newnode->type = maybeClone( type );
907 newnode->copySpecifiers( this );
908 assert( newName );
909 newnode->name = newName;
910 return newnode;
911}
912
913DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
914 if ( ! o ) return nullptr;
915
916 o->copySpecifiers( this );
917 if ( type ) {
918 TypeData * srcType = type;
919
920 // search for the base type by scanning off pointers and array designators
921 while ( srcType->base ) {
922 srcType = srcType->base;
923 } // while
924
925 TypeData * newType = srcType->clone();
926 if ( newType->kind == TypeData::AggregateInst ) {
927 // don't duplicate members
928 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
929 delete newType->aggInst.aggregate->enumeration.constants;
930 newType->aggInst.aggregate->enumeration.constants = nullptr;
931 newType->aggInst.aggregate->enumeration.body = false;
932 } else {
933 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
934 delete newType->aggInst.aggregate->aggregate.fields;
935 newType->aggInst.aggregate->aggregate.fields = nullptr;
936 newType->aggInst.aggregate->aggregate.body = false;
937 } // if
938 // don't hoist twice
939 newType->aggInst.hoistType = false;
940 } // if
941
942 newType->forall = maybeClone( type->forall );
943 if ( ! o->type ) {
944 o->type = newType;
945 } else {
946 addTypeToType( newType, o->type );
947 delete newType;
948 } // if
949 } // if
950 return o;
951}
952
953DeclarationNode * DeclarationNode::extractAggregate() const {
954 if ( type ) {
955 TypeData * ret = typeextractAggregate( type );
956 if ( ret ) {
957 DeclarationNode * newnode = new DeclarationNode;
958 newnode->type = ret;
959 return newnode;
960 } // if
961 } // if
962 return nullptr;
963}
964
965void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
966 SemanticErrorException errors;
967 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
968
969 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
970 try {
971 if ( DeclarationNode * extr = cur->extractAggregate() ) {
972 // handle the case where a structure declaration is contained within an object or type declaration
973 Declaration * decl = extr->build();
974 if ( decl ) {
975 decl->location = cur->location;
976 * out++ = decl;
977 } // if
978 delete extr;
979 } // if
980
981 Declaration * decl = cur->build();
982 if ( decl ) {
983 decl->location = cur->location;
984 * out++ = decl;
985 } // if
986 } catch( SemanticErrorException &e ) {
987 errors.append( e );
988 } // try
989 } // while
990
991 if ( ! errors.isEmpty() ) {
992 throw errors;
993 } // if
994} // buildList
995
996void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
997 SemanticErrorException errors;
998 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
999
1000 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
1001 try {
1002 Declaration * decl = cur->build();
1003 if ( decl ) {
1004 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
1005 dwt->location = cur->location;
1006 * out++ = dwt;
1007 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
1008 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
1009 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1010 obj->location = cur->location;
1011 * out++ = obj;
1012 delete agg;
1013 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
1014 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
1015 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1016 obj->location = cur->location;
1017 * out++ = obj;
1018 } // if
1019 } // if
1020 } catch( SemanticErrorException &e ) {
1021 errors.append( e );
1022 } // try
1023 } // for
1024
1025 if ( ! errors.isEmpty() ) {
1026 throw errors;
1027 } // if
1028} // buildList
1029
1030void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1031 SemanticErrorException errors;
1032 std::back_insert_iterator< std::list< Type * > > out( outputList );
1033 const DeclarationNode * cur = firstNode;
1034
1035 while ( cur ) {
1036 try {
1037 * out++ = cur->buildType();
1038 } catch( SemanticErrorException &e ) {
1039 errors.append( e );
1040 } // try
1041 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
1042 } // while
1043
1044 if ( ! errors.isEmpty() ) {
1045 throw errors;
1046 } // if
1047} // buildTypeList
1048
1049Declaration * DeclarationNode::build() const {
1050 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
1051
1052 if ( asmStmt ) {
1053 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1054 } // if
1055
1056 if ( variable.tyClass != NoTypeClass ) {
1057 // otype is internally converted to dtype + otype parameters
1058 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1059 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
1060 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1061 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
1062 buildList( variable.assertions, ret->get_assertions() );
1063 return ret;
1064 } // if
1065
1066 if ( type ) {
1067 // Function specifiers can only appear on a function definition/declaration.
1068 //
1069 // inline _Noreturn int f(); // allowed
1070 // inline _Noreturn int g( int i ); // allowed
1071 // inline _Noreturn int i; // disallowed
1072 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1073 SemanticError( this, "invalid function specifier for " );
1074 } // if
1075 return buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1076 } // if
1077
1078 if ( assert.condition ) {
1079 return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) );
1080 }
1081
1082 // SUE's cannot have function specifiers, either
1083 //
1084 // inlne _Noreturn struct S { ... }; // disallowed
1085 // inlne _Noreturn enum E { ... }; // disallowed
1086 if ( funcSpecs.any() ) {
1087 SemanticError( this, "invalid function specifier for " );
1088 } // if
1089 assertf( name, "ObjectDecl must a have name\n" );
1090 return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1091}
1092
1093Type * DeclarationNode::buildType() const {
1094 assert( type );
1095
1096 if ( attr.expr ) {
1097 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1098 } else if ( attr.type ) {
1099 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1100 } // if
1101
1102 switch ( type->kind ) {
1103 case TypeData::Enum:
1104 case TypeData::Aggregate: {
1105 ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
1106 buildList( type->aggregate.actuals, ret->get_parameters() );
1107 return ret;
1108 }
1109 case TypeData::Symbolic: {
1110 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1111 buildList( type->symbolic.actuals, ret->get_parameters() );
1112 return ret;
1113 }
1114 default:
1115 Type * simpletypes = typebuild( type );
1116 simpletypes->get_attributes() = attributes; // copy because member is const
1117 return simpletypes;
1118 } // switch
1119}
1120
1121// Local Variables: //
1122// tab-width: 4 //
1123// mode: c++ //
1124// compile-command: "make install" //
1125// End: //
Note: See TracBrowser for help on using the repository browser.