source: src/Parser/DeclarationNode.cc@ 48b9b36

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

fix warning about cv-qualifiers on zero/one

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