source: src/Parser/DeclarationNode.cc@ 2472a19

new-env with_gc
Last change on this file since 2472a19 was ff29f08, checked in by Aaron Moss <a3moss@…>, 8 years ago

Merge remote-tracking branch 'origin/master' into with_gc

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