source: src/Parser/TypeData.cc@ 936e9f4

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 936e9f4 was d180746, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Big header cleaning pass - commit 2

  • 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> // for assert
17#include <ostream> // for operator<<, ostream, basic_ostream
18
19#include "Common/SemanticError.h" // for SemanticError
20#include "Common/utility.h" // for maybeClone, maybeBuild, maybeMoveB...
21#include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
22#include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, FunctionDecl
23#include "SynTree/Expression.h" // for Expression, ConstantExpr (ptr only)
24#include "SynTree/Initializer.h" // for SingleInit, Initializer (ptr only)
25#include "SynTree/Statement.h" // for CompoundStmt, Statement
26#include "SynTree/Type.h" // for BasicType, Type, Type::ForallList
27#include "TypeData.h"
28
29class Attribute;
30
31using namespace std;
32
33TypeData::TypeData( Kind k ) : kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
34 switch ( kind ) {
35 case Unknown:
36 case Pointer:
37 case EnumConstant:
38 // nothing else to initialize
39 break;
40 case Basic:
41 // basic = new Basic_t;
42 break;
43 case Array:
44 // array = new Array_t;
45 array.dimension = nullptr;
46 array.isVarLen = false;
47 array.isStatic = false;
48 break;
49 case Function:
50 // function = new Function_t;
51 function.params = nullptr;
52 function.idList = nullptr;
53 function.oldDeclList = nullptr;
54 function.body = nullptr;
55 function.newStyle = false;
56 break;
57 // Enum is an Aggregate, so both structures are initialized together.
58 case Enum:
59 // enumeration = new Enumeration_t;
60 enumeration.name = nullptr;
61 enumeration.constants = nullptr;
62 enumeration.body = false;
63 case Aggregate:
64 // aggregate = new Aggregate_t;
65 aggregate.name = nullptr;
66 aggregate.params = nullptr;
67 aggregate.actuals = nullptr;
68 aggregate.fields = nullptr;
69 aggregate.body = false;
70 break;
71 case AggregateInst:
72 // aggInst = new AggInst_t;
73 aggInst.aggregate = nullptr;
74 aggInst.params = nullptr;
75 aggInst.hoistType = false;;
76 break;
77 case Symbolic:
78 case SymbolicInst:
79 // symbolic = new Symbolic_t;
80 symbolic.name = nullptr;
81 symbolic.params = nullptr;
82 symbolic.actuals = nullptr;
83 symbolic.assertions = nullptr;
84 break;
85 case Tuple:
86 // tuple = new Tuple_t;
87 tuple = nullptr;
88 break;
89 case Typeof:
90 // typeexpr = new Typeof_t;
91 typeexpr = nullptr;
92 break;
93 case Builtin:
94 // builtin = new Builtin_t;
95 break;
96 } // switch
97} // TypeData::TypeData
98
99TypeData::~TypeData() {
100 delete base;
101 delete forall;
102
103 switch ( kind ) {
104 case Unknown:
105 case Pointer:
106 case EnumConstant:
107 // nothing to destroy
108 break;
109 case Basic:
110 // delete basic;
111 break;
112 case Array:
113 delete array.dimension;
114 // delete array;
115 break;
116 case Function:
117 delete function.params;
118 delete function.idList;
119 delete function.oldDeclList;
120 delete function.body;
121 // delete function;
122 break;
123 case Aggregate:
124 delete aggregate.name;
125 delete aggregate.params;
126 delete aggregate.actuals;
127 delete aggregate.fields;
128 // delete aggregate;
129 break;
130 case AggregateInst:
131 delete aggInst.aggregate;
132 delete aggInst.params;
133 // delete aggInst;
134 break;
135 case Enum:
136 delete enumeration.name;
137 delete enumeration.constants;
138 // delete enumeration;
139 break;
140 case Symbolic:
141 case SymbolicInst:
142 delete symbolic.name;
143 delete symbolic.params;
144 delete symbolic.actuals;
145 delete symbolic.assertions;
146 // delete symbolic;
147 break;
148 case Tuple:
149 // delete tuple->members;
150 delete tuple;
151 break;
152 case Typeof:
153 // delete typeexpr->expr;
154 delete typeexpr;
155 break;
156 case Builtin:
157 // delete builtin;
158 break;
159 } // switch
160} // TypeData::~TypeData
161
162TypeData * TypeData::clone() const {
163 TypeData * newtype = new TypeData( kind );
164 newtype->qualifiers = qualifiers;
165 newtype->base = maybeClone( base );
166 newtype->forall = maybeClone( forall );
167
168 switch ( kind ) {
169 case Unknown:
170 case EnumConstant:
171 case Pointer:
172 // nothing else to copy
173 break;
174 case Basic:
175 newtype->basictype = basictype;
176 newtype->complextype = complextype;
177 newtype->signedness = signedness;
178 newtype->length = length;
179 break;
180 case Array:
181 newtype->array.dimension = maybeClone( array.dimension );
182 newtype->array.isVarLen = array.isVarLen;
183 newtype->array.isStatic = array.isStatic;
184 break;
185 case Function:
186 newtype->function.params = maybeClone( function.params );
187 newtype->function.idList = maybeClone( function.idList );
188 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
189 newtype->function.body = maybeClone( function.body );
190 newtype->function.newStyle = function.newStyle;
191 break;
192 case Aggregate:
193 newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
194 newtype->aggregate.params = maybeClone( aggregate.params );
195 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
196 newtype->aggregate.fields = maybeClone( aggregate.fields );
197 newtype->aggregate.kind = aggregate.kind;
198 newtype->aggregate.body = aggregate.body;
199 newtype->aggregate.tagged = aggregate.tagged;
200 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
201 break;
202 case AggregateInst:
203 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
204 newtype->aggInst.params = maybeClone( aggInst.params );
205 newtype->aggInst.hoistType = aggInst.hoistType;
206 break;
207 case Enum:
208 newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
209 newtype->enumeration.constants = maybeClone( enumeration.constants );
210 newtype->enumeration.body = enumeration.body;
211 break;
212 case Symbolic:
213 case SymbolicInst:
214 newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
215 newtype->symbolic.params = maybeClone( symbolic.params );
216 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
217 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
218 newtype->symbolic.isTypedef = symbolic.isTypedef;
219 break;
220 case Tuple:
221 newtype->tuple = maybeClone( tuple );
222 break;
223 case Typeof:
224 newtype->typeexpr = maybeClone( typeexpr );
225 break;
226 case Builtin:
227 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
228 newtype->builtintype = builtintype;
229 break;
230 } // switch
231 return newtype;
232} // TypeData::clone
233
234void TypeData::print( ostream &os, int indent ) const {
235 for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
236 if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
237 } // for
238
239 if ( forall ) {
240 os << "forall " << endl;
241 forall->printList( os, indent + 4 );
242 } // if
243
244 switch ( kind ) {
245 case Unknown:
246 os << "entity of unknown type ";
247 break;
248 case Pointer:
249 os << "pointer ";
250 if ( base ) {
251 os << "to ";
252 base->print( os, indent );
253 } // if
254 break;
255 case EnumConstant:
256 os << "enumeration constant ";
257 break;
258 case Basic:
259 if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
260 if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
261 assert( basictype != DeclarationNode::NoBasicType );
262 os << DeclarationNode::basicTypeNames[ basictype ] << " ";
263 if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
264 break;
265 case Array:
266 if ( array.isStatic ) {
267 os << "static ";
268 } // if
269 if ( array.dimension ) {
270 os << "array of ";
271 array.dimension->printOneLine( os, indent );
272 } else if ( array.isVarLen ) {
273 os << "variable-length array of ";
274 } else {
275 os << "open array of ";
276 } // if
277 if ( base ) {
278 base->print( os, indent );
279 } // if
280 break;
281 case Function:
282 os << "function" << endl;
283 if ( function.params ) {
284 os << string( indent + 2, ' ' ) << "with parameters " << endl;
285 function.params->printList( os, indent + 4 );
286 } else {
287 os << string( indent + 2, ' ' ) << "with no parameters " << endl;
288 } // if
289 if ( function.idList ) {
290 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
291 function.idList->printList( os, indent + 4 );
292 } // if
293 if ( function.oldDeclList ) {
294 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
295 function.oldDeclList->printList( os, indent + 4 );
296 } // if
297 os << string( indent + 2, ' ' ) << "returning ";
298 if ( base ) {
299 base->print( os, indent + 4 );
300 } else {
301 os << "nothing ";
302 } // if
303 os << endl;
304 if ( function.body ) {
305 os << string( indent + 2, ' ' ) << "with body " << endl;
306 function.body->printList( os, indent + 2 );
307 } // if
308 break;
309 case Aggregate:
310 os << DeclarationNode::aggregateNames[ aggregate.kind ] << ' ' << *aggregate.name << endl;
311 if ( aggregate.params ) {
312 os << string( indent + 2, ' ' ) << "with type parameters " << endl;
313 aggregate.params->printList( os, indent + 4 );
314 } // if
315 if ( aggregate.actuals ) {
316 os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
317 aggregate.actuals->printList( os, indent + 4 );
318 } // if
319 if ( aggregate.fields ) {
320 os << string( indent + 2, ' ' ) << "with members " << endl;
321 aggregate.fields->printList( os, indent + 4 );
322 } // if
323 if ( aggregate.body ) {
324 os << string( indent + 2, ' ' ) << " with body " << endl;
325 } // if
326 break;
327 case AggregateInst:
328 if ( aggInst.aggregate ) {
329 os << "instance of " ;
330 aggInst.aggregate->print( os, indent );
331 } else {
332 os << "instance of an unspecified aggregate ";
333 } // if
334 if ( aggInst.params ) {
335 os << string( indent + 2, ' ' ) << "with parameters " << endl;
336 aggInst.params->printList( os, indent + 2 );
337 } // if
338 break;
339 case Enum:
340 os << "enumeration ";
341 if ( enumeration.constants ) {
342 os << "with constants" << endl;
343 enumeration.constants->printList( os, indent + 2 );
344 } // if
345 if ( enumeration.body ) {
346 os << string( indent + 2, ' ' ) << " with body " << endl;
347 } // if
348 break;
349 case SymbolicInst:
350 os << "instance of type " << *symbolic.name;
351 if ( symbolic.actuals ) {
352 os << " with parameters" << endl;
353 symbolic.actuals->printList( os, indent + 2 );
354 } // if
355 break;
356 case Symbolic:
357 if ( symbolic.isTypedef ) {
358 os << "typedef definition ";
359 } else {
360 os << "type definition ";
361 } // if
362 if ( symbolic.params ) {
363 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
364 symbolic.params->printList( os, indent + 2 );
365 } // if
366 if ( symbolic.assertions ) {
367 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
368 symbolic.assertions->printList( os, indent + 4 );
369 os << string( indent + 2, ' ' );
370 } // if
371 if ( base ) {
372 os << "for ";
373 base->print( os, indent + 2 );
374 } // if
375 break;
376 case Tuple:
377 os << "tuple ";
378 if ( tuple ) {
379 os << "with members " << endl;
380 tuple->printList( os, indent + 2 );
381 } // if
382 break;
383 case Typeof:
384 os << "type-of expression ";
385 if ( typeexpr ) {
386 typeexpr->print( os, indent + 2 );
387 } // if
388 break;
389 case Builtin:
390 os << "gcc builtin type";
391 break;
392 default:
393 os << "internal error: TypeData::print " << kind << endl;
394 assert( false );
395 } // switch
396} // TypeData::print
397
398template< typename ForallList >
399void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
400 buildList( firstNode, outputList );
401 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i ) {
402 TypeDecl * td = static_cast<TypeDecl *>(*i);
403 if ( td->get_kind() == TypeDecl::Any ) {
404 // add assertion parameters to `type' tyvars in reverse order
405 // add dtor: void ^?{}(T *)
406 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
407 dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
408 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
409
410 // add copy ctor: void ?{}(T *, T)
411 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
412 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
413 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
414 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
415
416 // add default ctor: void ?{}(T *)
417 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
418 ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
419 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
420
421 // add assignment operator: T * ?=?(T *, T)
422 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
423 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
424 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
425 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
426 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
427 } // if
428 } // for
429}
430
431Type * typebuild( const TypeData * td ) {
432 assert( td );
433 switch ( td->kind ) {
434 case TypeData::Unknown:
435 // fill in implicit int
436 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
437 case TypeData::Basic:
438 return buildBasicType( td );
439 case TypeData::Pointer:
440 return buildPointer( td );
441 case TypeData::Array:
442 return buildArray( 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} // buildPointer
622
623AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
624 assert( td->kind == TypeData::Aggregate );
625 AggregateDecl * at;
626 switch ( td->aggregate.kind ) {
627 case DeclarationNode::Struct:
628 case DeclarationNode::Coroutine:
629 case DeclarationNode::Monitor:
630 case DeclarationNode::Thread:
631 at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
632 buildForall( td->aggregate.params, at->get_parameters() );
633 break;
634 case DeclarationNode::Union:
635 at = new UnionDecl( *td->aggregate.name, attributes, linkage );
636 buildForall( td->aggregate.params, at->get_parameters() );
637 break;
638 case DeclarationNode::Trait:
639 at = new TraitDecl( *td->aggregate.name, attributes, linkage );
640 buildList( td->aggregate.params, at->get_parameters() );
641 break;
642 default:
643 assert( false );
644 } // switch
645
646 buildList( td->aggregate.fields, at->get_members() );
647 at->set_body( td->aggregate.body );
648
649 return at;
650} // buildAggregate
651
652ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
653 switch ( type->kind ) {
654 case TypeData::Enum: {
655 if ( type->enumeration.body ) {
656 EnumDecl * typedecl = buildEnum( type, attributes, linkage );
657 return new EnumInstType( buildQualifiers( type ), typedecl );
658 } else {
659 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
660 } // if
661 }
662 case TypeData::Aggregate: {
663 ReferenceToType * ret;
664 if ( type->aggregate.body ) {
665 AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
666 switch ( type->aggregate.kind ) {
667 case DeclarationNode::Struct:
668 case DeclarationNode::Coroutine:
669 case DeclarationNode::Monitor:
670 case DeclarationNode::Thread:
671 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
672 break;
673 case DeclarationNode::Union:
674 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
675 break;
676 case DeclarationNode::Trait:
677 assert( false );
678 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
679 break;
680 default:
681 assert( false );
682 } // switch
683 } else {
684 switch ( type->aggregate.kind ) {
685 case DeclarationNode::Struct:
686 case DeclarationNode::Coroutine:
687 case DeclarationNode::Monitor:
688 case DeclarationNode::Thread:
689 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
690 break;
691 case DeclarationNode::Union:
692 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
693 break;
694 case DeclarationNode::Trait:
695 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
696 break;
697 default:
698 assert( false );
699 } // switch
700 } // if
701 return ret;
702 }
703 default:
704 assert( false );
705 } // switch
706} // buildAggInst
707
708ReferenceToType * buildAggInst( const TypeData * td ) {
709 assert( td->kind == TypeData::AggregateInst );
710
711 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
712 ReferenceToType * ret = nullptr;
713 TypeData * type = td->aggInst.aggregate;
714 switch ( type->kind ) {
715 case TypeData::Enum: {
716 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
717 }
718 case TypeData::Aggregate: {
719 switch ( type->aggregate.kind ) {
720 case DeclarationNode::Struct:
721 case DeclarationNode::Coroutine:
722 case DeclarationNode::Monitor:
723 case DeclarationNode::Thread:
724 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
725 break;
726 case DeclarationNode::Union:
727 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
728 break;
729 case DeclarationNode::Trait:
730 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
731 break;
732 default:
733 assert( false );
734 } // switch
735 }
736 break;
737 default:
738 assert( false );
739 } // switch
740
741 ret->set_hoistType( td->aggInst.hoistType );
742 buildList( td->aggInst.params, ret->get_parameters() );
743 buildForall( td->forall, ret->get_forall() );
744 return ret;
745} // buildAggInst
746
747NamedTypeDecl * buildSymbolic( const TypeData * td, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
748 assert( td->kind == TypeData::Symbolic );
749 NamedTypeDecl * ret;
750 assert( td->base );
751 if ( td->symbolic.isTypedef ) {
752 ret = new TypedefDecl( name, scs, typebuild( td->base ), linkage );
753 } else {
754 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Any );
755 } // if
756 buildList( td->symbolic.params, ret->get_parameters() );
757 buildList( td->symbolic.assertions, ret->get_assertions() );
758 return ret;
759} // buildSymbolic
760
761EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
762 assert( td->kind == TypeData::Enum );
763 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage );
764 buildList( td->enumeration.constants, ret->get_members() );
765 list< Declaration * >::iterator members = ret->get_members().begin();
766 for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
767 if ( cur->has_enumeratorValue() ) {
768 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
769 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
770 } // if
771 } // for
772 ret->set_body( td->enumeration.body );
773 return ret;
774} // buildEnum
775
776TypeInstType * buildSymbolicInst( const TypeData * td ) {
777 assert( td->kind == TypeData::SymbolicInst );
778 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
779 buildList( td->symbolic.actuals, ret->get_parameters() );
780 buildForall( td->forall, ret->get_forall() );
781 return ret;
782} // buildSymbolicInst
783
784TupleType * buildTuple( const TypeData * td ) {
785 assert( td->kind == TypeData::Tuple );
786 std::list< Type * > types;
787 buildTypeList( td->tuple, types );
788 TupleType * ret = new TupleType( buildQualifiers( td ), types );
789 buildForall( td->forall, ret->get_forall() );
790 return ret;
791} // buildTuple
792
793TypeofType * buildTypeof( const TypeData * td ) {
794 assert( td->kind == TypeData::Typeof );
795 assert( td->typeexpr );
796 // assert( td->typeexpr->expr );
797 return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
798} // buildTypeof
799
800Declaration * 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 ) {
801 if ( td->kind == TypeData::Function ) {
802 if ( td->function.idList ) { // KR function ?
803 buildKRFunction( td->function ); // transform into C11 function
804 } // if
805
806 FunctionDecl * decl;
807 Statement * stmt = maybeBuild<Statement>( td->function.body );
808 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
809 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
810 return decl->set_asmName( asmName );
811 } else if ( td->kind == TypeData::Aggregate ) {
812 return buildAggregate( td, attributes, linkage );
813 } else if ( td->kind == TypeData::Enum ) {
814 return buildEnum( td, attributes, linkage );
815 } else if ( td->kind == TypeData::Symbolic ) {
816 return buildSymbolic( td, name, scs, linkage );
817 } else {
818 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
819 } // if
820 return nullptr;
821} // buildDecl
822
823FunctionType * buildFunction( const TypeData * td ) {
824 assert( td->kind == TypeData::Function );
825 bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true;
826 if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle;
827 FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis );
828 buildList( td->function.params, ft->get_parameters() );
829 buildForall( td->forall, ft->get_forall() );
830 if ( td->base ) {
831 switch ( td->base->kind ) {
832 case TypeData::Tuple:
833 buildList( td->base->tuple, ft->get_returnVals() );
834 break;
835 default:
836 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
837 } // switch
838 } else {
839 ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
840 } // if
841 return ft;
842} // buildFunction
843
844// Transform KR routine declarations into C99 routine declarations:
845//
846// rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
847//
848// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
849// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
850// extra names are disallowed.
851//
852// Note, there is no KR routine-prototype syntax:
853//
854// rtn( a, b, c ) int a, c; double b; // invalid KR prototype
855// rtn(); // valid KR prototype
856
857void buildKRFunction( const TypeData::Function_t & function ) {
858 assert( ! function.params );
859 // loop over declaration first as it is easier to spot errors
860 for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
861 // scan ALL parameter names for each declaration name to check for duplicates
862 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
863 if ( *decl->name == *param->name ) {
864 // type set => parameter name already transformed by a declaration names so there is a duplicate
865 // declaration name attempting a second transformation
866 if ( param->type ) throw SemanticError( string( "duplicate declaration name " ) + *param->name );
867 // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
868 // parameter name attempting a second transformation
869 if ( ! decl->type ) throw SemanticError( string( "duplicate parameter name " ) + *param->name );
870 param->type = decl->type; // set copy declaration type to parameter type
871 decl->type = nullptr; // reset declaration type
872 param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
873 } // if
874 } // for
875 // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
876 if ( decl->type ) throw SemanticError( string( "missing name in parameter list " ) + *decl->name );
877 } // for
878
879 // Parameter names without a declaration default to type int:
880 //
881 // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
882
883 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
884 if ( ! param->type ) { // generate type int for empty parameter type
885 param->type = new TypeData( TypeData::Basic );
886 param->type->basictype = DeclarationNode::Int;
887 } // if
888 } // for
889
890 function.params = function.idList; // newly modified idList becomes parameters
891 function.idList = nullptr; // idList now empty
892 delete function.oldDeclList; // deletes entire list
893 function.oldDeclList = nullptr; // reset
894} // buildKRFunction
895
896// Local Variables: //
897// tab-width: 4 //
898// mode: c++ //
899// compile-command: "make install" //
900// End: //
Note: See TracBrowser for help on using the repository browser.