source: src/Parser/DeclarationNode.cc@ 679e644

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 679e644 was 679e644, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

extend plan 9, anonymous declarations, change token for default argument

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