source: src/Parser/TypeData.cc@ 9f10c4b8

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 resolv-new with_gc
Last change on this file since 9f10c4b8 was 8217e8f, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 32.2 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// TypeData.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 15:12:51 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Mon Aug 14 10:41:00 2017
13// Update Count : 568
14//
15
16#include <cassert>
17#include <algorithm>
18#include <iterator>
19#include "Common/utility.h"
20#include "TypeData.h"
21#include "SynTree/Type.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Expression.h"
24#include "SynTree/Statement.h"
25#include "SynTree/Initializer.h"
26using namespace std;
27
28TypeData::TypeData( Kind k ) : kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
29 switch ( kind ) {
30 case Unknown:
31 case Pointer:
32 case Reference:
33 case EnumConstant:
34 // nothing else to initialize
35 break;
36 case Basic:
37 // basic = new Basic_t;
38 break;
39 case Array:
40 // array = new Array_t;
41 array.dimension = nullptr;
42 array.isVarLen = false;
43 array.isStatic = false;
44 break;
45 case Function:
46 // function = new Function_t;
47 function.params = nullptr;
48 function.idList = nullptr;
49 function.oldDeclList = nullptr;
50 function.body = nullptr;
51 function.newStyle = false;
52 break;
53 // Enum is an Aggregate, so both structures are initialized together.
54 case Enum:
55 // enumeration = new Enumeration_t;
56 enumeration.name = nullptr;
57 enumeration.constants = nullptr;
58 enumeration.body = false;
59 case Aggregate:
60 // aggregate = new Aggregate_t;
61 aggregate.name = nullptr;
62 aggregate.params = nullptr;
63 aggregate.actuals = nullptr;
64 aggregate.fields = nullptr;
65 aggregate.body = false;
66 break;
67 case AggregateInst:
68 // aggInst = new AggInst_t;
69 aggInst.aggregate = nullptr;
70 aggInst.params = nullptr;
71 aggInst.hoistType = false;;
72 break;
73 case Symbolic:
74 case SymbolicInst:
75 // symbolic = new Symbolic_t;
76 symbolic.name = nullptr;
77 symbolic.params = nullptr;
78 symbolic.actuals = nullptr;
79 symbolic.assertions = nullptr;
80 break;
81 case Tuple:
82 // tuple = new Tuple_t;
83 tuple = nullptr;
84 break;
85 case Typeof:
86 // typeexpr = new Typeof_t;
87 typeexpr = nullptr;
88 break;
89 case Builtin:
90 // builtin = new Builtin_t;
91 break;
92 } // switch
93} // TypeData::TypeData
94
95TypeData::~TypeData() {
96 delete base;
97 delete forall;
98
99 switch ( kind ) {
100 case Unknown:
101 case Pointer:
102 case Reference:
103 case EnumConstant:
104 // nothing to destroy
105 break;
106 case Basic:
107 // delete basic;
108 break;
109 case Array:
110 delete array.dimension;
111 // delete array;
112 break;
113 case Function:
114 delete function.params;
115 delete function.idList;
116 delete function.oldDeclList;
117 delete function.body;
118 // delete function;
119 break;
120 case Aggregate:
121 delete aggregate.name;
122 delete aggregate.params;
123 delete aggregate.actuals;
124 delete aggregate.fields;
125 // delete aggregate;
126 break;
127 case AggregateInst:
128 delete aggInst.aggregate;
129 delete aggInst.params;
130 // delete aggInst;
131 break;
132 case Enum:
133 delete enumeration.name;
134 delete enumeration.constants;
135 // delete enumeration;
136 break;
137 case Symbolic:
138 case SymbolicInst:
139 delete symbolic.name;
140 delete symbolic.params;
141 delete symbolic.actuals;
142 delete symbolic.assertions;
143 // delete symbolic;
144 break;
145 case Tuple:
146 // delete tuple->members;
147 delete tuple;
148 break;
149 case Typeof:
150 // delete typeexpr->expr;
151 delete typeexpr;
152 break;
153 case Builtin:
154 // delete builtin;
155 break;
156 } // switch
157} // TypeData::~TypeData
158
159TypeData * TypeData::clone() const {
160 TypeData * newtype = new TypeData( kind );
161 newtype->qualifiers = qualifiers;
162 newtype->base = maybeClone( base );
163 newtype->forall = maybeClone( forall );
164
165 switch ( kind ) {
166 case Unknown:
167 case EnumConstant:
168 case Pointer:
169 case Reference:
170 // nothing else to copy
171 break;
172 case Basic:
173 newtype->basictype = basictype;
174 newtype->complextype = complextype;
175 newtype->signedness = signedness;
176 newtype->length = length;
177 break;
178 case Array:
179 newtype->array.dimension = maybeClone( array.dimension );
180 newtype->array.isVarLen = array.isVarLen;
181 newtype->array.isStatic = array.isStatic;
182 break;
183 case Function:
184 newtype->function.params = maybeClone( function.params );
185 newtype->function.idList = maybeClone( function.idList );
186 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
187 newtype->function.body = maybeClone( function.body );
188 newtype->function.newStyle = function.newStyle;
189 break;
190 case Aggregate:
191 newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
192 newtype->aggregate.params = maybeClone( aggregate.params );
193 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
194 newtype->aggregate.fields = maybeClone( aggregate.fields );
195 newtype->aggregate.kind = aggregate.kind;
196 newtype->aggregate.body = aggregate.body;
197 newtype->aggregate.tagged = aggregate.tagged;
198 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
199 break;
200 case AggregateInst:
201 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
202 newtype->aggInst.params = maybeClone( aggInst.params );
203 newtype->aggInst.hoistType = aggInst.hoistType;
204 break;
205 case Enum:
206 newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
207 newtype->enumeration.constants = maybeClone( enumeration.constants );
208 newtype->enumeration.body = enumeration.body;
209 break;
210 case Symbolic:
211 case SymbolicInst:
212 newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
213 newtype->symbolic.params = maybeClone( symbolic.params );
214 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
215 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
216 newtype->symbolic.isTypedef = symbolic.isTypedef;
217 break;
218 case Tuple:
219 newtype->tuple = maybeClone( tuple );
220 break;
221 case Typeof:
222 newtype->typeexpr = maybeClone( typeexpr );
223 break;
224 case Builtin:
225 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
226 newtype->builtintype = builtintype;
227 break;
228 } // switch
229 return newtype;
230} // TypeData::clone
231
232void TypeData::print( ostream &os, int indent ) const {
233 for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
234 if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
235 } // for
236
237 if ( forall ) {
238 os << "forall " << endl;
239 forall->printList( os, indent + 4 );
240 } // if
241
242 switch ( kind ) {
243 case Unknown:
244 os << "entity of unknown type ";
245 break;
246 case Pointer:
247 os << "pointer ";
248 if ( base ) {
249 os << "to ";
250 base->print( os, indent );
251 } // if
252 break;
253 case EnumConstant:
254 os << "enumeration constant ";
255 break;
256 case Basic:
257 if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
258 if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
259 assert( basictype != DeclarationNode::NoBasicType );
260 os << DeclarationNode::basicTypeNames[ basictype ] << " ";
261 if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
262 break;
263 case Array:
264 if ( array.isStatic ) {
265 os << "static ";
266 } // if
267 if ( array.dimension ) {
268 os << "array of ";
269 array.dimension->printOneLine( os, indent );
270 } else if ( array.isVarLen ) {
271 os << "variable-length array of ";
272 } else {
273 os << "open array of ";
274 } // if
275 if ( base ) {
276 base->print( os, indent );
277 } // if
278 break;
279 case Function:
280 os << "function" << endl;
281 if ( function.params ) {
282 os << string( indent + 2, ' ' ) << "with parameters " << endl;
283 function.params->printList( os, indent + 4 );
284 } else {
285 os << string( indent + 2, ' ' ) << "with no parameters " << endl;
286 } // if
287 if ( function.idList ) {
288 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
289 function.idList->printList( os, indent + 4 );
290 } // if
291 if ( function.oldDeclList ) {
292 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
293 function.oldDeclList->printList( os, indent + 4 );
294 } // if
295 os << string( indent + 2, ' ' ) << "returning ";
296 if ( base ) {
297 base->print( os, indent + 4 );
298 } else {
299 os << "nothing ";
300 } // if
301 os << endl;
302 if ( function.body ) {
303 os << string( indent + 2, ' ' ) << "with body " << endl;
304 function.body->printList( os, indent + 2 );
305 } // if
306 break;
307 case Aggregate:
308 os << DeclarationNode::aggregateNames[ aggregate.kind ] << ' ' << *aggregate.name << endl;
309 if ( aggregate.params ) {
310 os << string( indent + 2, ' ' ) << "with type parameters " << endl;
311 aggregate.params->printList( os, indent + 4 );
312 } // if
313 if ( aggregate.actuals ) {
314 os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
315 aggregate.actuals->printList( os, indent + 4 );
316 } // if
317 if ( aggregate.fields ) {
318 os << string( indent + 2, ' ' ) << "with members " << endl;
319 aggregate.fields->printList( os, indent + 4 );
320 } // if
321 if ( aggregate.body ) {
322 os << string( indent + 2, ' ' ) << " with body " << endl;
323 } // if
324 break;
325 case AggregateInst:
326 if ( aggInst.aggregate ) {
327 os << "instance of " ;
328 aggInst.aggregate->print( os, indent );
329 } else {
330 os << "instance of an unspecified aggregate ";
331 } // if
332 if ( aggInst.params ) {
333 os << string( indent + 2, ' ' ) << "with parameters " << endl;
334 aggInst.params->printList( os, indent + 2 );
335 } // if
336 break;
337 case Enum:
338 os << "enumeration ";
339 if ( enumeration.constants ) {
340 os << "with constants" << endl;
341 enumeration.constants->printList( os, indent + 2 );
342 } // if
343 if ( enumeration.body ) {
344 os << string( indent + 2, ' ' ) << " with body " << endl;
345 } // if
346 break;
347 case SymbolicInst:
348 os << "instance of type " << *symbolic.name;
349 if ( symbolic.actuals ) {
350 os << " with parameters" << endl;
351 symbolic.actuals->printList( os, indent + 2 );
352 } // if
353 break;
354 case Symbolic:
355 if ( symbolic.isTypedef ) {
356 os << "typedef definition ";
357 } else {
358 os << "type definition ";
359 } // if
360 if ( symbolic.params ) {
361 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
362 symbolic.params->printList( os, indent + 2 );
363 } // if
364 if ( symbolic.assertions ) {
365 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
366 symbolic.assertions->printList( os, indent + 4 );
367 os << string( indent + 2, ' ' );
368 } // if
369 if ( base ) {
370 os << "for ";
371 base->print( os, indent + 2 );
372 } // if
373 break;
374 case Tuple:
375 os << "tuple ";
376 if ( tuple ) {
377 os << "with members " << endl;
378 tuple->printList( os, indent + 2 );
379 } // if
380 break;
381 case Typeof:
382 os << "type-of expression ";
383 if ( typeexpr ) {
384 typeexpr->print( os, indent + 2 );
385 } // if
386 break;
387 case Builtin:
388 os << "gcc builtin type";
389 break;
390 default:
391 os << "internal error: TypeData::print " << kind << endl;
392 assert( false );
393 } // switch
394} // TypeData::print
395
396template< typename ForallList >
397void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
398 buildList( firstNode, outputList );
399 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i ) {
400 TypeDecl * td = static_cast<TypeDecl *>(*i);
401 if ( td->get_kind() == TypeDecl::Any ) {
402 // add assertion parameters to `type' tyvars in reverse order
403 // add dtor: void ^?{}(T *)
404 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
405 dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
406 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
407
408 // add copy ctor: void ?{}(T *, T)
409 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
410 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
411 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
412 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
413
414 // add default ctor: void ?{}(T *)
415 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
416 ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
417 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
418
419 // add assignment operator: T * ?=?(T *, T)
420 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
421 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
422 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
423 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
424 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
425 } // if
426 } // for
427}
428
429Type * typebuild( const TypeData * td ) {
430 assert( td );
431 switch ( td->kind ) {
432 case TypeData::Unknown:
433 // fill in implicit int
434 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
435 case TypeData::Basic:
436 return buildBasicType( td );
437 case TypeData::Pointer:
438 return buildPointer( td );
439 case TypeData::Array:
440 return buildArray( td );
441 case TypeData::Reference:
442 return buildReference( td );
443 case TypeData::Function:
444 return buildFunction( td );
445 case TypeData::AggregateInst:
446 return buildAggInst( td );
447 case TypeData::EnumConstant:
448 // the name gets filled in later -- by SymTab::Validate
449 return new EnumInstType( buildQualifiers( td ), "" );
450 case TypeData::SymbolicInst:
451 return buildSymbolicInst( td );;
452 case TypeData::Tuple:
453 return buildTuple( td );
454 case TypeData::Typeof:
455 return buildTypeof( td );
456 case TypeData::Builtin:
457 if(td->builtintype == DeclarationNode::Zero) {
458 return new ZeroType( noQualifiers );
459 }
460 else if(td->builtintype == DeclarationNode::One) {
461 return new OneType( noQualifiers );
462 }
463 else {
464 return new VarArgsType( buildQualifiers( td ) );
465 }
466 case TypeData::Symbolic:
467 case TypeData::Enum:
468 case TypeData::Aggregate:
469 assert( false );
470 } // switch
471 return nullptr;
472} // typebuild
473
474TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
475 TypeData * ret = nullptr;
476
477 switch ( td->kind ) {
478 case TypeData::Aggregate:
479 if ( ! toplevel && td->aggregate.fields ) {
480 ret = td->clone();
481 } // if
482 break;
483 case TypeData::Enum:
484 if ( ! toplevel && td->enumeration.constants ) {
485 ret = td->clone();
486 } // if
487 break;
488 case TypeData::AggregateInst:
489 if ( td->aggInst.aggregate ) {
490 ret = typeextractAggregate( td->aggInst.aggregate, false );
491 } // if
492 break;
493 default:
494 if ( td->base ) {
495 ret = typeextractAggregate( td->base, false );
496 } // if
497 } // switch
498 return ret;
499} // typeextractAggregate
500
501Type::Qualifiers buildQualifiers( const TypeData * td ) {
502 return td->qualifiers;
503} // buildQualifiers
504
505Type * buildBasicType( const TypeData * td ) {
506 BasicType::Kind ret;
507
508 switch ( td->basictype ) {
509 case DeclarationNode::Void:
510 if ( td->signedness != DeclarationNode::NoSignedness && td->length != DeclarationNode::NoLength ) {
511 throw SemanticError( "invalid type specifier \"void\" in type: ", td );
512 } // if
513
514 return new VoidType( buildQualifiers( td ) );
515 break;
516
517 case DeclarationNode::Bool:
518 if ( td->signedness != DeclarationNode::NoSignedness ) {
519 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
520 } // if
521 if ( td->length != DeclarationNode::NoLength ) {
522 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
523 } // if
524
525 ret = BasicType::Bool;
526 break;
527
528 case DeclarationNode::Char:
529 // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
530 // character types. The implementation shall define char to have the same range, representation, and behavior as
531 // either signed char or unsigned char.
532 static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
533
534 if ( td->length != DeclarationNode::NoLength ) {
535 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
536 } // if
537
538 ret = chartype[ td->signedness ];
539 break;
540
541 case DeclarationNode::Int:
542 static BasicType::Kind inttype[2][4] = {
543 { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
544 { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
545 };
546
547 Integral: ;
548 if ( td->signedness == DeclarationNode::NoSignedness ) {
549 const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
550 } // if
551 ret = inttype[ td->signedness ][ td->length ];
552 break;
553
554 case DeclarationNode::Float:
555 case DeclarationNode::Double:
556 case DeclarationNode::LongDouble: // not set until below
557 static BasicType::Kind floattype[3][3] = {
558 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
559 { BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary },
560 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
561 };
562
563 FloatingPoint: ;
564 if ( td->signedness != DeclarationNode::NoSignedness ) {
565 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
566 } // if
567 if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
568 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
569 } // if
570 if ( td->basictype == DeclarationNode::Float && td->length == DeclarationNode::Long ) {
571 throw SemanticError( "invalid type specifier \"long\" in type: ", td );
572 } // if
573 if ( td->length == DeclarationNode::Long ) {
574 const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
575 } // if
576
577 ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
578 break;
579
580 case DeclarationNode::NoBasicType:
581 // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
582 if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
583 const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
584 goto FloatingPoint;
585 } // if
586
587 const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
588 goto Integral;
589 default:
590 assert(false);
591 return nullptr;
592 } // switch
593
594 BasicType * bt = new BasicType( buildQualifiers( td ), ret );
595 buildForall( td->forall, bt->get_forall() );
596 return bt;
597} // buildBasicType
598
599PointerType * buildPointer( const TypeData * td ) {
600 PointerType * pt;
601 if ( td->base ) {
602 pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
603 } else {
604 pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
605 } // if
606 buildForall( td->forall, pt->get_forall() );
607 return pt;
608} // buildPointer
609
610ArrayType * buildArray( const TypeData * td ) {
611 ArrayType * at;
612 if ( td->base ) {
613 at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
614 td->array.isVarLen, td->array.isStatic );
615 } else {
616 at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
617 maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
618 } // if
619 buildForall( td->forall, at->get_forall() );
620 return at;
621} // buildArray
622
623ReferenceType * buildReference( const TypeData * td ) {
624 ReferenceType * rt;
625 if ( td->base ) {
626 rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
627 } else {
628 rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
629 } // if
630 buildForall( td->forall, rt->get_forall() );
631 return rt;
632} // buildReference
633
634AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
635 assert( td->kind == TypeData::Aggregate );
636 AggregateDecl * at;
637 switch ( td->aggregate.kind ) {
638 case DeclarationNode::Struct:
639 case DeclarationNode::Coroutine:
640 case DeclarationNode::Monitor:
641 case DeclarationNode::Thread:
642 at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
643 buildForall( td->aggregate.params, at->get_parameters() );
644 break;
645 case DeclarationNode::Union:
646 at = new UnionDecl( *td->aggregate.name, attributes, linkage );
647 buildForall( td->aggregate.params, at->get_parameters() );
648 break;
649 case DeclarationNode::Trait:
650 at = new TraitDecl( *td->aggregate.name, attributes, linkage );
651 buildList( td->aggregate.params, at->get_parameters() );
652 break;
653 default:
654 assert( false );
655 } // switch
656
657 buildList( td->aggregate.fields, at->get_members() );
658 at->set_body( td->aggregate.body );
659
660 return at;
661} // buildAggregate
662
663ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
664 switch ( type->kind ) {
665 case TypeData::Enum: {
666 if ( type->enumeration.body ) {
667 EnumDecl * typedecl = buildEnum( type, attributes, linkage );
668 return new EnumInstType( buildQualifiers( type ), typedecl );
669 } else {
670 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
671 } // if
672 }
673 case TypeData::Aggregate: {
674 ReferenceToType * ret;
675 if ( type->aggregate.body ) {
676 AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
677 switch ( type->aggregate.kind ) {
678 case DeclarationNode::Struct:
679 case DeclarationNode::Coroutine:
680 case DeclarationNode::Monitor:
681 case DeclarationNode::Thread:
682 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
683 break;
684 case DeclarationNode::Union:
685 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
686 break;
687 case DeclarationNode::Trait:
688 assert( false );
689 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
690 break;
691 default:
692 assert( false );
693 } // switch
694 } else {
695 switch ( type->aggregate.kind ) {
696 case DeclarationNode::Struct:
697 case DeclarationNode::Coroutine:
698 case DeclarationNode::Monitor:
699 case DeclarationNode::Thread:
700 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
701 break;
702 case DeclarationNode::Union:
703 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
704 break;
705 case DeclarationNode::Trait:
706 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
707 break;
708 default:
709 assert( false );
710 } // switch
711 } // if
712 return ret;
713 }
714 default:
715 assert( false );
716 } // switch
717} // buildAggInst
718
719ReferenceToType * buildAggInst( const TypeData * td ) {
720 assert( td->kind == TypeData::AggregateInst );
721
722 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
723 ReferenceToType * ret = nullptr;
724 TypeData * type = td->aggInst.aggregate;
725 switch ( type->kind ) {
726 case TypeData::Enum: {
727 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
728 }
729 case TypeData::Aggregate: {
730 switch ( type->aggregate.kind ) {
731 case DeclarationNode::Struct:
732 case DeclarationNode::Coroutine:
733 case DeclarationNode::Monitor:
734 case DeclarationNode::Thread:
735 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
736 break;
737 case DeclarationNode::Union:
738 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
739 break;
740 case DeclarationNode::Trait:
741 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
742 break;
743 default:
744 assert( false );
745 } // switch
746 }
747 break;
748 default:
749 assert( false );
750 } // switch
751
752 ret->set_hoistType( td->aggInst.hoistType );
753 buildList( td->aggInst.params, ret->get_parameters() );
754 buildForall( td->forall, ret->get_forall() );
755 return ret;
756} // buildAggInst
757
758NamedTypeDecl * buildSymbolic( const TypeData * td, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
759 assert( td->kind == TypeData::Symbolic );
760 NamedTypeDecl * ret;
761 assert( td->base );
762 if ( td->symbolic.isTypedef ) {
763 ret = new TypedefDecl( name, scs, typebuild( td->base ), linkage );
764 } else {
765 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Any );
766 } // if
767 buildList( td->symbolic.params, ret->get_parameters() );
768 buildList( td->symbolic.assertions, ret->get_assertions() );
769 return ret;
770} // buildSymbolic
771
772EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
773 assert( td->kind == TypeData::Enum );
774 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
775 buildList( td->enumeration.constants, ret->get_members() );
776 list< Declaration * >::iterator members = ret->get_members().begin();
777 for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
778 if ( cur->has_enumeratorValue() ) {
779 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
780 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
781 } // if
782 } // for
783 ret->set_body( td->enumeration.body );
784 return ret;
785} // buildEnum
786
787TypeInstType * buildSymbolicInst( const TypeData * td ) {
788 assert( td->kind == TypeData::SymbolicInst );
789 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
790 buildList( td->symbolic.actuals, ret->get_parameters() );
791 buildForall( td->forall, ret->get_forall() );
792 return ret;
793} // buildSymbolicInst
794
795TupleType * buildTuple( const TypeData * td ) {
796 assert( td->kind == TypeData::Tuple );
797 std::list< Type * > types;
798 buildTypeList( td->tuple, types );
799 TupleType * ret = new TupleType( buildQualifiers( td ), types );
800 buildForall( td->forall, ret->get_forall() );
801 return ret;
802} // buildTuple
803
804TypeofType * buildTypeof( const TypeData * td ) {
805 assert( td->kind == TypeData::Typeof );
806 assert( td->typeexpr );
807 // assert( td->typeexpr->expr );
808 return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
809} // buildTypeof
810
811Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, ConstantExpr *asmName, Initializer * init, std::list< Attribute * > attributes ) {
812 if ( td->kind == TypeData::Function ) {
813 if ( td->function.idList ) { // KR function ?
814 buildKRFunction( td->function ); // transform into C11 function
815 } // if
816
817 FunctionDecl * decl;
818 Statement * stmt = maybeBuild<Statement>( td->function.body );
819 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
820 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
821 return decl->set_asmName( asmName );
822 } else if ( td->kind == TypeData::Aggregate ) {
823 return buildAggregate( td, attributes, linkage );
824 } else if ( td->kind == TypeData::Enum ) {
825 return buildEnum( td, attributes, linkage );
826 } else if ( td->kind == TypeData::Symbolic ) {
827 return buildSymbolic( td, name, scs, linkage );
828 } else {
829 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
830 } // if
831 return nullptr;
832} // buildDecl
833
834FunctionType * buildFunction( const TypeData * td ) {
835 assert( td->kind == TypeData::Function );
836 bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true;
837 if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle;
838 FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis );
839 buildList( td->function.params, ft->get_parameters() );
840 buildForall( td->forall, ft->get_forall() );
841 if ( td->base ) {
842 switch ( td->base->kind ) {
843 case TypeData::Tuple:
844 buildList( td->base->tuple, ft->get_returnVals() );
845 break;
846 default:
847 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
848 } // switch
849 } else {
850 ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
851 } // if
852 return ft;
853} // buildFunction
854
855// Transform KR routine declarations into C99 routine declarations:
856//
857// rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
858//
859// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
860// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
861// extra names are disallowed.
862//
863// Note, there is no KR routine-prototype syntax:
864//
865// rtn( a, b, c ) int a, c; double b; // invalid KR prototype
866// rtn(); // valid KR prototype
867
868void buildKRFunction( const TypeData::Function_t & function ) {
869 assert( ! function.params );
870 // loop over declaration first as it is easier to spot errors
871 for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
872 // scan ALL parameter names for each declaration name to check for duplicates
873 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
874 if ( *decl->name == *param->name ) {
875 // type set => parameter name already transformed by a declaration names so there is a duplicate
876 // declaration name attempting a second transformation
877 if ( param->type ) throw SemanticError( string( "duplicate declaration name " ) + *param->name );
878 // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
879 // parameter name attempting a second transformation
880 if ( ! decl->type ) throw SemanticError( string( "duplicate parameter name " ) + *param->name );
881 param->type = decl->type; // set copy declaration type to parameter type
882 decl->type = nullptr; // reset declaration type
883 param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
884 } // if
885 } // for
886 // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
887 if ( decl->type ) throw SemanticError( string( "missing name in parameter list " ) + *decl->name );
888 } // for
889
890 // Parameter names without a declaration default to type int:
891 //
892 // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
893
894 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
895 if ( ! param->type ) { // generate type int for empty parameter type
896 param->type = new TypeData( TypeData::Basic );
897 param->type->basictype = DeclarationNode::Int;
898 } // if
899 } // for
900
901 function.params = function.idList; // newly modified idList becomes parameters
902 function.idList = nullptr; // idList now empty
903 delete function.oldDeclList; // deletes entire list
904 function.oldDeclList = nullptr; // reset
905} // buildKRFunction
906
907// Local Variables: //
908// tab-width: 4 //
909// mode: c++ //
910// compile-command: "make install" //
911// End: //
Note: See TracBrowser for help on using the repository browser.