source: src/Parser/DeclarationNode.cc@ 777ed2b

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 no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 777ed2b was 3d7e53b, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add anon flag to TypeData and remove anonymous members for named aggregates

  • Property mode set to 100644
File size: 39.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// DeclarationNode.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 6 06:56:08 2018
13// Update Count : 1088
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
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( const 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 if ( ret ) {
184 newnode->type->base = ret->type;
185 ret->type = nullptr;
186 delete ret;
187 } // if
188
189 return newnode;
190} // DeclarationNode::newFunction
191
192
193DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) {
194 DeclarationNode * newnode = new DeclarationNode;
195 newnode->storageClasses = sc;
196 return newnode;
197} // DeclarationNode::newStorageClass
198
199DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) {
200 DeclarationNode * newnode = new DeclarationNode;
201 newnode->funcSpecs = fs;
202 return newnode;
203} // DeclarationNode::newFuncSpecifier
204
205DeclarationNode * DeclarationNode::newTypeQualifier( Type::Qualifiers tq ) {
206 DeclarationNode * newnode = new DeclarationNode;
207 newnode->type = new TypeData();
208 newnode->type->qualifiers = tq;
209 return newnode;
210} // DeclarationNode::newQualifier
211
212DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
213 DeclarationNode * newnode = new DeclarationNode;
214 newnode->type = new TypeData( TypeData::Basic );
215 newnode->type->basictype = bt;
216 return newnode;
217} // DeclarationNode::newBasicType
218
219DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
220 DeclarationNode * newnode = new DeclarationNode;
221 newnode->type = new TypeData( TypeData::Basic );
222 newnode->type->complextype = ct;
223 return newnode;
224} // DeclarationNode::newComplexType
225
226DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
227 DeclarationNode * newnode = new DeclarationNode;
228 newnode->type = new TypeData( TypeData::Basic );
229 newnode->type->signedness = sn;
230 return newnode;
231} // DeclarationNode::newSignedNess
232
233DeclarationNode * DeclarationNode::newLength( Length lnth ) {
234 DeclarationNode * newnode = new DeclarationNode;
235 newnode->type = new TypeData( TypeData::Basic );
236 newnode->type->length = lnth;
237 return newnode;
238} // DeclarationNode::newLength
239
240DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
241 DeclarationNode * newnode = new DeclarationNode;
242 newnode->type = new TypeData( TypeData::Unknown );
243 newnode->type->forall = forall;
244 return newnode;
245} // DeclarationNode::newForall
246
247DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
248 DeclarationNode * newnode = new DeclarationNode;
249 newnode->type = new TypeData( TypeData::SymbolicInst );
250 newnode->type->symbolic.name = name;
251 newnode->type->symbolic.isTypedef = true;
252 newnode->type->symbolic.params = nullptr;
253 return newnode;
254} // DeclarationNode::newFromTypedef
255
256DeclarationNode * DeclarationNode::newFromGlobalScope() {
257 DeclarationNode * newnode = new DeclarationNode;
258 newnode->type = new TypeData( TypeData::GlobalScope );
259 return newnode;
260}
261
262DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
263 DeclarationNode * newnode = new DeclarationNode;
264 newnode->type = new TypeData( TypeData::Qualified );
265 newnode->type->qualified.parent = parent->type;
266 newnode->type->qualified.child = child->type;
267 parent->type = nullptr;
268 child->type = nullptr;
269 delete parent;
270 delete child;
271 return newnode;
272}
273
274DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
275 DeclarationNode * newnode = new DeclarationNode;
276 newnode->type = new TypeData( TypeData::Aggregate );
277 newnode->type->aggregate.kind = kind;
278 newnode->type->aggregate.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
279 newnode->type->aggregate.actuals = actuals;
280 newnode->type->aggregate.fields = fields;
281 newnode->type->aggregate.body = body;
282 newnode->type->aggregate.tagged = false;
283 newnode->type->aggregate.parent = nullptr;
284 newnode->type->aggregate.anon = name == nullptr;
285 return newnode;
286} // DeclarationNode::newAggregate
287
288DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) {
289 DeclarationNode * newnode = new DeclarationNode;
290 newnode->type = new TypeData( TypeData::Enum );
291 newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
292 newnode->type->enumeration.constants = constants;
293 newnode->type->enumeration.body = body;
294 newnode->type->enumeration.anon = name == nullptr;
295 return newnode;
296} // DeclarationNode::newEnum
297
298DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
299 DeclarationNode * newnode = new DeclarationNode;
300 newnode->name = name;
301 newnode->enumeratorValue.reset( constant );
302 return newnode;
303} // DeclarationNode::newEnumConstant
304
305DeclarationNode * DeclarationNode::newName( const string * name ) {
306 DeclarationNode * newnode = new DeclarationNode;
307 newnode->name = name;
308 return newnode;
309} // DeclarationNode::newName
310
311DeclarationNode * DeclarationNode::newFromTypeGen( const string * name, ExpressionNode * params ) {
312 DeclarationNode * newnode = new DeclarationNode;
313 newnode->type = new TypeData( TypeData::SymbolicInst );
314 newnode->type->symbolic.name = name;
315 newnode->type->symbolic.isTypedef = false;
316 newnode->type->symbolic.actuals = params;
317 return newnode;
318} // DeclarationNode::newFromTypeGen
319
320DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, const string * name ) {
321 DeclarationNode * newnode = new DeclarationNode;
322 newnode->type = nullptr;
323 assert( ! newnode->name );
324// newnode->variable.name = name;
325 newnode->name = name;
326 newnode->variable.tyClass = tc;
327 newnode->variable.assertions = nullptr;
328 return newnode;
329} // DeclarationNode::newTypeParam
330
331DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
332 DeclarationNode * newnode = new DeclarationNode;
333 newnode->type = new TypeData( TypeData::Aggregate );
334 newnode->type->aggregate.name = name;
335 newnode->type->aggregate.kind = Trait;
336 newnode->type->aggregate.params = params;
337 newnode->type->aggregate.fields = asserts;
338 return newnode;
339} // DeclarationNode::newTrait
340
341DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
342 DeclarationNode * newnode = new DeclarationNode;
343 newnode->type = new TypeData( TypeData::AggregateInst );
344 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
345 newnode->type->aggInst.aggregate->aggregate.kind = Trait;
346 newnode->type->aggInst.aggregate->aggregate.name = name;
347 newnode->type->aggInst.params = params;
348 return newnode;
349} // DeclarationNode::newTraitUse
350
351DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) {
352 DeclarationNode * newnode = new DeclarationNode;
353 newnode->name = name;
354 newnode->type = new TypeData( TypeData::Symbolic );
355 newnode->type->symbolic.isTypedef = false;
356 newnode->type->symbolic.params = typeParams;
357 return newnode;
358} // DeclarationNode::newTypeDecl
359
360DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
361 DeclarationNode * newnode = new DeclarationNode;
362 newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
363 if ( kind == OperKinds::And ) {
364 // T && is parsed as 'And' operator rather than two references => add a second reference type
365 TypeData * td = new TypeData( TypeData::Reference );
366 td->base = newnode->type;
367 newnode->type = td;
368 }
369 if ( qualifiers ) {
370 return newnode->addQualifiers( qualifiers );
371 } else {
372 return newnode;
373 } // if
374} // DeclarationNode::newPointer
375
376DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
377 DeclarationNode * newnode = new DeclarationNode;
378 newnode->type = new TypeData( TypeData::Array );
379 newnode->type->array.dimension = size;
380 newnode->type->array.isStatic = isStatic;
381 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
382 newnode->type->array.isVarLen = false;
383 } else {
384 newnode->type->array.isVarLen = true;
385 } // if
386 return newnode->addQualifiers( qualifiers );
387} // DeclarationNode::newArray
388
389DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
390 DeclarationNode * newnode = new DeclarationNode;
391 newnode->type = new TypeData( TypeData::Array );
392 newnode->type->array.dimension = nullptr;
393 newnode->type->array.isStatic = false;
394 newnode->type->array.isVarLen = true;
395 return newnode->addQualifiers( qualifiers );
396}
397
398DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
399 DeclarationNode * newnode = new DeclarationNode;
400 newnode->bitfieldWidth = size;
401 return newnode;
402}
403
404DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
405 DeclarationNode * newnode = new DeclarationNode;
406 newnode->type = new TypeData( TypeData::Tuple );
407 newnode->type->tuple = members;
408 return newnode;
409}
410
411DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
412 DeclarationNode * newnode = new DeclarationNode;
413 newnode->type = new TypeData( TypeData::Typeof );
414 newnode->type->typeexpr = expr;
415 return newnode;
416}
417
418DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
419 DeclarationNode * newnode = new DeclarationNode;
420 newnode->type = new TypeData( TypeData::Builtin );
421 newnode->builtin = bt;
422 newnode->type->builtintype = newnode->builtin;
423 return newnode;
424} // DeclarationNode::newBuiltinType
425
426DeclarationNode * DeclarationNode::newAttr( const string * name, ExpressionNode * expr ) {
427 DeclarationNode * newnode = new DeclarationNode;
428 newnode->type = nullptr;
429// newnode->attr.name = name;
430 newnode->name = name;
431 newnode->attr.expr = expr;
432 return newnode;
433}
434
435DeclarationNode * DeclarationNode::newAttr( const string * name, DeclarationNode * type ) {
436 DeclarationNode * newnode = new DeclarationNode;
437 newnode->type = nullptr;
438// newnode->attr.name = name;
439 newnode->name = name;
440 newnode->attr.type = type;
441 return newnode;
442}
443
444DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
445 DeclarationNode * newnode = new DeclarationNode;
446 newnode->type = nullptr;
447 std::list< Expression * > exprs;
448 buildList( expr, exprs );
449 newnode->attributes.push_back( new Attribute( *name, exprs ) );
450 delete name;
451 return newnode;
452}
453
454DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
455 DeclarationNode * newnode = new DeclarationNode;
456 newnode->asmStmt = stmt;
457 return newnode;
458}
459
460DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, Expression * message ) {
461 DeclarationNode * newnode = new DeclarationNode;
462 newnode->assert.condition = condition;
463 newnode->assert.message = message;
464 return newnode;
465}
466
467
468void appendError( string & dst, const string & src ) {
469 if ( src.empty() ) return;
470 if ( dst.empty() ) { dst = src; return; }
471 dst += ", " + src;
472} // appendError
473
474void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
475 const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
476
477 if ( (qsrc & qdst).any() ) { // duplicates ?
478 for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates
479 if ( qsrc[i] && qdst[i] ) {
480 appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] );
481 } // if
482 } // for
483 } // for
484} // DeclarationNode::checkQualifiers
485
486void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
487 if ( (funcSpecs & src->funcSpecs).any() ) { // duplicates ?
488 for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates
489 if ( funcSpecs[i] && src->funcSpecs[i] ) {
490 appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] );
491 } // if
492 } // for
493 } // if
494
495 if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ?
496 if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ?
497 for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates
498 if ( storageClasses[i] && src->storageClasses[i] ) {
499 appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] );
500 } // if
501 } // for
502 // src is the new item being added and has a single bit
503 } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ?
504 appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] +
505 " & " + Type::StorageClassesNames[src->storageClasses.ffs()] );
506 src->storageClasses.reset(); // FIX to preserve invariant of one basic storage specifier
507 } // if
508 } // if
509
510 appendError( error, src->error );
511} // DeclarationNode::checkSpecifiers
512
513DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
514 funcSpecs |= q->funcSpecs;
515 storageClasses |= q->storageClasses;
516
517 for ( Attribute *attr: reverseIterate( q->attributes ) ) {
518 attributes.push_front( attr->clone() );
519 } // for
520 return this;
521} // DeclarationNode::copySpecifiers
522
523static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
524 if ( dst->base ) {
525 addQualifiersToType( src, dst->base );
526 } else if ( dst->kind == TypeData::Function ) {
527 dst->base = src;
528 src = nullptr;
529 } else {
530 dst->qualifiers |= src->qualifiers;
531 } // if
532} // addQualifiersToType
533
534DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
535 if ( ! q ) { return this; } // empty qualifier
536
537 checkSpecifiers( q );
538 copySpecifiers( q );
539
540 if ( ! q->type ) { delete q; return this; }
541
542 if ( ! type ) {
543 type = q->type; // reuse structure
544 q->type = nullptr;
545 delete q;
546 return this;
547 } // if
548
549 if ( q->type->forall ) { // forall qualifier ?
550 if ( type->forall ) { // polymorphic routine ?
551 type->forall->appendList( q->type->forall ); // augment forall qualifier
552 } else {
553 if ( type->kind == TypeData::Aggregate ) { // struct/union ?
554 if ( type->aggregate.params ) { // polymorphic ?
555 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
556 } else { // not polymorphic
557 type->aggregate.params = q->type->forall; // set forall qualifier
558 } // if
559 } else { // not polymorphic
560 type->forall = q->type->forall; // make polymorphic routine
561 } // if
562 } // if
563 q->type->forall = nullptr; // forall qualifier moved
564 } // if
565
566 checkQualifiers( type, q->type );
567 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
568 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
569 } // if
570 addQualifiersToType( q->type, type );
571
572 delete q;
573 return this;
574} // addQualifiers
575
576static void addTypeToType( TypeData *&src, TypeData *&dst ) {
577 if ( src->forall && dst->kind == TypeData::Function ) {
578 if ( dst->forall ) {
579 dst->forall->appendList( src->forall );
580 } else {
581 dst->forall = src->forall;
582 } // if
583 src->forall = nullptr;
584 } // if
585 if ( dst->base ) {
586 addTypeToType( src, dst->base );
587 } else {
588 switch ( dst->kind ) {
589 case TypeData::Unknown:
590 src->qualifiers |= dst->qualifiers;
591 dst = src;
592 src = nullptr;
593 break;
594 case TypeData::Basic:
595 dst->qualifiers |= src->qualifiers;
596 if ( src->kind != TypeData::Unknown ) {
597 assert( src->kind == TypeData::Basic );
598
599 if ( dst->basictype == DeclarationNode::NoBasicType ) {
600 dst->basictype = src->basictype;
601 } else if ( src->basictype != DeclarationNode::NoBasicType )
602 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
603
604 if ( dst->complextype == DeclarationNode::NoComplexType ) {
605 dst->complextype = src->complextype;
606 } else if ( src->complextype != DeclarationNode::NoComplexType )
607 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
608
609 if ( dst->signedness == DeclarationNode::NoSignedness ) {
610 dst->signedness = src->signedness;
611 } else if ( src->signedness != DeclarationNode::NoSignedness )
612 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
613
614 if ( dst->length == DeclarationNode::NoLength ) {
615 dst->length = src->length;
616 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
617 dst->length = DeclarationNode::LongLong;
618 } else if ( src->length != DeclarationNode::NoLength )
619 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
620 } // if
621 break;
622 default:
623 switch ( src->kind ) {
624 case TypeData::Aggregate:
625 case TypeData::Enum:
626 dst->base = new TypeData( TypeData::AggregateInst );
627 dst->base->aggInst.aggregate = src;
628 if ( src->kind == TypeData::Aggregate ) {
629 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
630 } // if
631 dst->base->qualifiers |= src->qualifiers;
632 src = nullptr;
633 break;
634 default:
635 if ( dst->forall ) {
636 dst->forall->appendList( src->forall );
637 } else {
638 dst->forall = src->forall;
639 } // if
640 src->forall = nullptr;
641 dst->base = src;
642 src = nullptr;
643 } // switch
644 } // switch
645 } // if
646}
647
648DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
649 if ( o ) {
650 checkSpecifiers( o );
651 copySpecifiers( o );
652 if ( o->type ) {
653 if ( ! type ) {
654 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
655 type = new TypeData( TypeData::AggregateInst );
656 type->aggInst.aggregate = o->type;
657 if ( o->type->kind == TypeData::Aggregate ) {
658 type->aggInst.hoistType = o->type->aggregate.body;
659 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
660 } else {
661 type->aggInst.hoistType = o->type->enumeration.body;
662 } // if
663 type->qualifiers |= o->type->qualifiers;
664 } else {
665 type = o->type;
666 } // if
667 o->type = nullptr;
668 } else {
669 addTypeToType( o->type, type );
670 } // if
671 } // if
672 if ( o->bitfieldWidth ) {
673 bitfieldWidth = o->bitfieldWidth;
674 } // if
675
676 // there may be typedefs chained onto the type
677 if ( o->get_next() ) {
678 set_last( o->get_next()->clone() );
679 } // if
680 } // if
681 delete o;
682 return this;
683}
684
685DeclarationNode * DeclarationNode::addTypedef() {
686 TypeData * newtype = new TypeData( TypeData::Symbolic );
687 newtype->symbolic.params = nullptr;
688 newtype->symbolic.isTypedef = true;
689 newtype->symbolic.name = name ? new string( *name ) : nullptr;
690 newtype->base = type;
691 type = newtype;
692 return this;
693}
694
695DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
696 if ( variable.tyClass != NoTypeClass ) {
697 if ( variable.assertions ) {
698 variable.assertions->appendList( assertions );
699 } else {
700 variable.assertions = assertions;
701 } // if
702 return this;
703 } // if
704
705 assert( type );
706 switch ( type->kind ) {
707 case TypeData::Symbolic:
708 if ( type->symbolic.assertions ) {
709 type->symbolic.assertions->appendList( assertions );
710 } else {
711 type->symbolic.assertions = assertions;
712 } // if
713 break;
714 default:
715 assert( false );
716 } // switch
717
718 return this;
719}
720
721DeclarationNode * DeclarationNode::addName( string * newname ) {
722 assert( ! name );
723 name = newname;
724 return this;
725}
726
727DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
728 assert( ! asmName );
729 asmName = newname ? newname->asmName : nullptr;
730 return this->addQualifiers( newname );
731}
732
733DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
734 bitfieldWidth = size;
735 return this;
736}
737
738DeclarationNode * DeclarationNode::addVarArgs() {
739 assert( type );
740 hasEllipsis = true;
741 return this;
742}
743
744DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
745 assert( type );
746 assert( type->kind == TypeData::Function );
747 assert( ! type->function.body );
748 type->function.body = body;
749 type->function.withExprs = withExprs;
750 return this;
751}
752
753DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
754 assert( type );
755 assert( type->kind == TypeData::Function );
756 assert( ! type->function.oldDeclList );
757 type->function.oldDeclList = list;
758 return this;
759}
760
761DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
762 if ( type ) {
763 TypeData * prevBase = type;
764 TypeData * curBase = type->base;
765 while ( curBase != nullptr ) {
766 prevBase = curBase;
767 curBase = curBase->base;
768 } // while
769 prevBase->base = newType;
770 } else {
771 type = newType;
772 } // if
773 return this;
774}
775
776DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
777 if ( a ) {
778 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
779 attributes.push_front( attr );
780 } // for
781 a->attributes.clear();
782 } // if
783 return this;
784} // copyAttribute
785
786DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
787 if ( p ) {
788 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
789 setBase( p->type );
790 p->type = nullptr;
791 copyAttribute( p );
792 delete p;
793 } // if
794 return this;
795}
796
797DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
798 if ( a ) {
799 assert( a->type->kind == TypeData::Array );
800 setBase( a->type );
801 a->type = nullptr;
802 copyAttribute( a );
803 delete a;
804 } // if
805 return this;
806}
807
808DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
809 if ( p ) {
810 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
811 if ( type ) {
812 switch ( type->kind ) {
813 case TypeData::Aggregate:
814 case TypeData::Enum:
815 p->type->base = new TypeData( TypeData::AggregateInst );
816 p->type->base->aggInst.aggregate = type;
817 if ( type->kind == TypeData::Aggregate ) {
818 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
819 } // if
820 p->type->base->qualifiers |= type->qualifiers;
821 break;
822
823 default:
824 p->type->base = type;
825 } // switch
826 type = nullptr;
827 } // if
828 delete this;
829 return p;
830 } else {
831 return this;
832 } // if
833}
834
835static TypeData * findLast( TypeData * a ) {
836 assert( a );
837 TypeData * cur = a;
838 while ( cur->base ) {
839 cur = cur->base;
840 } // while
841 return cur;
842}
843
844DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
845 if ( ! a ) return this;
846 assert( a->type->kind == TypeData::Array );
847 TypeData * lastArray = findLast( a->type );
848 if ( type ) {
849 switch ( type->kind ) {
850 case TypeData::Aggregate:
851 case TypeData::Enum:
852 lastArray->base = new TypeData( TypeData::AggregateInst );
853 lastArray->base->aggInst.aggregate = type;
854 if ( type->kind == TypeData::Aggregate ) {
855 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
856 } // if
857 lastArray->base->qualifiers |= type->qualifiers;
858 break;
859 default:
860 lastArray->base = type;
861 } // switch
862 type = nullptr;
863 } // if
864 delete this;
865 return a;
866}
867
868DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
869 TypeData * ftype = new TypeData( TypeData::Function );
870 ftype->function.params = params;
871 setBase( ftype );
872 return this;
873}
874
875static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
876 if ( type ) {
877 if ( type->kind != TypeData::Function ) {
878 type->base = addIdListToType( type->base, ids );
879 } else {
880 type->function.idList = ids;
881 } // if
882 return type;
883 } else {
884 TypeData * newtype = new TypeData( TypeData::Function );
885 newtype->function.idList = ids;
886 return newtype;
887 } // if
888} // addIdListToType
889
890DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
891 type = addIdListToType( type, ids );
892 return this;
893}
894
895DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
896 initializer = init;
897 return this;
898}
899
900DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
901 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
902 variable.initializer = init;
903 return this;
904}
905
906DeclarationNode * DeclarationNode::cloneType( string * newName ) {
907 DeclarationNode * newnode = new DeclarationNode;
908 newnode->type = maybeClone( type );
909 newnode->copySpecifiers( this );
910 assert( newName );
911 newnode->name = newName;
912 return newnode;
913}
914
915DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
916 if ( ! o ) return nullptr;
917
918 o->copySpecifiers( this );
919 if ( type ) {
920 TypeData * srcType = type;
921
922 // search for the base type by scanning off pointers and array designators
923 while ( srcType->base ) {
924 srcType = srcType->base;
925 } // while
926
927 TypeData * newType = srcType->clone();
928 if ( newType->kind == TypeData::AggregateInst ) {
929 // don't duplicate members
930 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
931 delete newType->aggInst.aggregate->enumeration.constants;
932 newType->aggInst.aggregate->enumeration.constants = nullptr;
933 newType->aggInst.aggregate->enumeration.body = false;
934 } else {
935 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
936 delete newType->aggInst.aggregate->aggregate.fields;
937 newType->aggInst.aggregate->aggregate.fields = nullptr;
938 newType->aggInst.aggregate->aggregate.body = false;
939 } // if
940 // don't hoist twice
941 newType->aggInst.hoistType = false;
942 } // if
943
944 newType->forall = maybeClone( type->forall );
945 if ( ! o->type ) {
946 o->type = newType;
947 } else {
948 addTypeToType( newType, o->type );
949 delete newType;
950 } // if
951 } // if
952 return o;
953}
954
955DeclarationNode * DeclarationNode::extractAggregate() const {
956 if ( type ) {
957 TypeData * ret = typeextractAggregate( type );
958 if ( ret ) {
959 DeclarationNode * newnode = new DeclarationNode;
960 newnode->type = ret;
961 return newnode;
962 } // if
963 } // if
964 return nullptr;
965}
966
967void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
968 SemanticErrorException errors;
969 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
970
971 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
972 try {
973 bool extracted = false;
974 bool anon = false;
975 if ( DeclarationNode * extr = cur->extractAggregate() ) {
976 // handle the case where a structure declaration is contained within an object or type declaration
977 Declaration * decl = extr->build();
978 if ( decl ) {
979 // hoist the structure declaration
980 decl->location = cur->location;
981 * out++ = decl;
982
983 // need to remember the cases where a declaration contains an anonymous aggregate definition
984 extracted = true;
985 assert( extr->type );
986 if ( extr->type->kind == TypeData::Aggregate ) {
987 anon = extr->type->aggregate.anon;
988 } else if ( extr->type->kind == TypeData::Enum ) {
989 // xxx - is it useful to have an implicit anonymous enum member?
990 anon = extr->type->enumeration.anon;
991 }
992 } // if
993 delete extr;
994 } // if
995
996 Declaration * decl = cur->build();
997 if ( decl ) {
998 // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.:
999 // struct S {
1000 // struct T { int x; }; // no anonymous member
1001 // struct { int y; }; // anonymous member
1002 // struct T; // anonymous member
1003 // };
1004 if ( ! (extracted && decl->name == "" && ! anon) ) {
1005 decl->location = cur->location;
1006 * out++ = decl;
1007 }
1008 } // if
1009 } catch( SemanticErrorException &e ) {
1010 errors.append( e );
1011 } // try
1012 } // while
1013
1014 if ( ! errors.isEmpty() ) {
1015 throw errors;
1016 } // if
1017} // buildList
1018
1019// currently only builds assertions, function parameters, and return values
1020void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
1021 SemanticErrorException errors;
1022 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
1023
1024 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
1025 try {
1026 Declaration * decl = cur->build();
1027 assert( decl );
1028 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
1029 dwt->location = cur->location;
1030 * out++ = dwt;
1031 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
1032 // e.g., int foo(struct S) {}
1033 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name );
1034 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1035 obj->location = cur->location;
1036 * out++ = obj;
1037 delete agg;
1038 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
1039 // e.g., int foo(union U) {}
1040 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name );
1041 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1042 obj->location = cur->location;
1043 * out++ = obj;
1044 } else if ( EnumDecl * agg = dynamic_cast< EnumDecl * >( decl ) ) {
1045 // e.g., int foo(enum E) {}
1046 EnumInstType * inst = new EnumInstType( Type::Qualifiers(), agg->name );
1047 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1048 obj->location = cur->location;
1049 * out++ = obj;
1050 } // if
1051 } catch( SemanticErrorException &e ) {
1052 errors.append( e );
1053 } // try
1054 } // for
1055
1056 if ( ! errors.isEmpty() ) {
1057 throw errors;
1058 } // if
1059} // buildList
1060
1061void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1062 SemanticErrorException errors;
1063 std::back_insert_iterator< std::list< Type * > > out( outputList );
1064 const DeclarationNode * cur = firstNode;
1065
1066 while ( cur ) {
1067 try {
1068 * out++ = cur->buildType();
1069 } catch( SemanticErrorException &e ) {
1070 errors.append( e );
1071 } // try
1072 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
1073 } // while
1074
1075 if ( ! errors.isEmpty() ) {
1076 throw errors;
1077 } // if
1078} // buildTypeList
1079
1080Declaration * DeclarationNode::build() const {
1081 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
1082
1083 if ( asmStmt ) {
1084 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1085 } // if
1086
1087 if ( variable.tyClass != NoTypeClass ) {
1088 // otype is internally converted to dtype + otype parameters
1089 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1090 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
1091 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1092 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
1093 buildList( variable.assertions, ret->get_assertions() );
1094 return ret;
1095 } // if
1096
1097 if ( type ) {
1098 // Function specifiers can only appear on a function definition/declaration.
1099 //
1100 // inline _Noreturn int f(); // allowed
1101 // inline _Noreturn int g( int i ); // allowed
1102 // inline _Noreturn int i; // disallowed
1103 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1104 SemanticError( this, "invalid function specifier for " );
1105 } // if
1106 // Forall qualifier can only appear on a function/aggregate definition/declaration.
1107 //
1108 // forall int f(); // allowed
1109 // forall int g( int i ); // allowed
1110 // forall int i; // disallowed
1111 if ( type->kind != TypeData::Function && type->forall ) {
1112 SemanticError( this, "invalid type qualifier for " );
1113 } // if
1114 bool isDelete = initializer && initializer->get_isDelete();
1115 Declaration * decl = buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, isDelete ? nullptr : maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1116 if ( isDelete ) {
1117 DeclarationWithType * dwt = strict_dynamic_cast<DeclarationWithType *>( decl );
1118 dwt->isDeleted = true;
1119 }
1120 return decl;
1121 } // if
1122
1123 if ( assert.condition ) {
1124 return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) );
1125 }
1126
1127 // SUE's cannot have function specifiers, either
1128 //
1129 // inlne _Noreturn struct S { ... }; // disallowed
1130 // inlne _Noreturn enum E { ... }; // disallowed
1131 if ( funcSpecs.any() ) {
1132 SemanticError( this, "invalid function specifier for " );
1133 } // if
1134 assertf( name, "ObjectDecl must a have name\n" );
1135 return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1136}
1137
1138Type * DeclarationNode::buildType() const {
1139 assert( type );
1140
1141 if ( attr.expr ) {
1142 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1143 } else if ( attr.type ) {
1144 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1145 } // if
1146
1147 switch ( type->kind ) {
1148 case TypeData::Enum:
1149 case TypeData::Aggregate: {
1150 ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
1151 buildList( type->aggregate.actuals, ret->get_parameters() );
1152 return ret;
1153 }
1154 case TypeData::Symbolic: {
1155 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1156 buildList( type->symbolic.actuals, ret->get_parameters() );
1157 return ret;
1158 }
1159 default:
1160 Type * simpletypes = typebuild( type );
1161 simpletypes->get_attributes() = attributes; // copy because member is const
1162 return simpletypes;
1163 } // switch
1164}
1165
1166// Local Variables: //
1167// tab-width: 4 //
1168// mode: c++ //
1169// compile-command: "make install" //
1170// End: //
Note: See TracBrowser for help on using the repository browser.