source: src/Parser/DeclarationNode.cc@ 7fdb94e1

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since 7fdb94e1 was 7fdb94e1, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

rewrite TypedefTable

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