source: src/Parser/TypeData.cc @ a2e0687

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since a2e0687 was ac10576, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Replaced emptyQualifiers with noQualifiers for consistancy with noLabels and noAttributes.

  • Property mode set to 100644
File size: 31.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// 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 : Tus Jul 18 10:10:00 2017
13// Update Count     : 566
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 EnumConstant:
33                // nothing else to initialize
34                break;
35          case Basic:
36                // basic = new Basic_t;
37                break;
38          case Array:
39                // array = new Array_t;
40                array.dimension = nullptr;
41                array.isVarLen = false;
42                array.isStatic = false;
43                break;
44          case Function:
45                // function = new Function_t;
46                function.params = nullptr;
47                function.idList = nullptr;
48                function.oldDeclList = nullptr;
49                function.body = nullptr;
50                function.newStyle = false;
51                break;
52                // Enum is an Aggregate, so both structures are initialized together.
53          case Enum:
54                // enumeration = new Enumeration_t;
55                enumeration.name = nullptr;
56                enumeration.constants = nullptr;
57                enumeration.body = false;
58          case Aggregate:
59                // aggregate = new Aggregate_t;
60                aggregate.name = nullptr;
61                aggregate.params = nullptr;
62                aggregate.actuals = nullptr;
63                aggregate.fields = nullptr;
64                aggregate.body = false;
65                aggregate.tagged = false;
66                aggregate.parent = nullptr;
67                break;
68          case AggregateInst:
69                // aggInst = new AggInst_t;
70                aggInst.aggregate = nullptr;
71                aggInst.params = nullptr;
72                aggInst.hoistType = false;;
73                break;
74          case Symbolic:
75          case SymbolicInst:
76                // symbolic = new Symbolic_t;
77                symbolic.name = nullptr;
78                symbolic.params = nullptr;
79                symbolic.actuals = nullptr;
80                symbolic.assertions = nullptr;
81                break;
82          case Tuple:
83                // tuple = new Tuple_t;
84                tuple = nullptr;
85                break;
86          case Typeof:
87                // typeexpr = new Typeof_t;
88                typeexpr = nullptr;
89                break;
90          case Builtin:
91                // builtin = new Builtin_t;
92                break;
93        } // switch
94} // TypeData::TypeData
95
96TypeData::~TypeData() {
97        delete base;
98        delete forall;
99
100        switch ( kind ) {
101          case Unknown:
102          case Pointer:
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.parent;
126                // delete aggregate;
127                break;
128          case AggregateInst:
129                delete aggInst.aggregate;
130                delete aggInst.params;
131                // delete aggInst;
132                break;
133          case Enum:
134                delete enumeration.name;
135                delete enumeration.constants;
136                // delete enumeration;
137                break;
138          case Symbolic:
139          case SymbolicInst:
140                delete symbolic.name;
141                delete symbolic.params;
142                delete symbolic.actuals;
143                delete symbolic.assertions;
144                // delete symbolic;
145                break;
146          case Tuple:
147                // delete tuple->members;
148                delete tuple;
149                break;
150          case Typeof:
151                // delete typeexpr->expr;
152                delete typeexpr;
153                break;
154          case Builtin:
155                // delete builtin;
156                break;
157        } // switch
158} // TypeData::~TypeData
159
160TypeData * TypeData::clone() const {
161        TypeData * newtype = new TypeData( kind );
162        newtype->qualifiers = qualifiers;
163        newtype->base = maybeClone( base );
164        newtype->forall = maybeClone( forall );
165
166        switch ( kind ) {
167          case Unknown:
168          case EnumConstant:
169          case Pointer:
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 PointerType( 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 PointerType( 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 PointerType( 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 PointerType( 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::Function:
442                return buildFunction( td );
443          case TypeData::AggregateInst:
444                return buildAggInst( td );
445          case TypeData::EnumConstant:
446                // the name gets filled in later -- by SymTab::Validate
447                return new EnumInstType( buildQualifiers( td ), "" );
448          case TypeData::SymbolicInst:
449                return buildSymbolicInst( td );;
450          case TypeData::Tuple:
451                return buildTuple( td );
452          case TypeData::Typeof:
453                return buildTypeof( td );
454          case TypeData::Builtin:
455                if(td->builtintype == DeclarationNode::Zero) {
456                        return new ZeroType( noQualifiers );
457                }
458                else if(td->builtintype == DeclarationNode::One) {
459                        return new OneType( noQualifiers );
460                }
461                else {
462                        return new VarArgsType( buildQualifiers( td ) );
463                }
464          case TypeData::Symbolic:
465          case TypeData::Enum:
466          case TypeData::Aggregate:
467                assert( false );
468        } // switch
469        return nullptr;
470} // typebuild
471
472TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
473        TypeData * ret = nullptr;
474
475        switch ( td->kind ) {
476          case TypeData::Aggregate:
477                if ( ! toplevel && td->aggregate.fields ) {
478                        ret = td->clone();
479                } // if
480                break;
481          case TypeData::Enum:
482                if ( ! toplevel && td->enumeration.constants ) {
483                        ret = td->clone();
484                } // if
485                break;
486          case TypeData::AggregateInst:
487                if ( td->aggInst.aggregate ) {
488                        ret = typeextractAggregate( td->aggInst.aggregate, false );
489                } // if
490                break;
491          default:
492                if ( td->base ) {
493                        ret = typeextractAggregate( td->base, false );
494                } // if
495        } // switch
496        return ret;
497} // typeextractAggregate
498
499Type::Qualifiers buildQualifiers( const TypeData * td ) {
500        return td->qualifiers;
501} // buildQualifiers
502
503Type * buildBasicType( const TypeData * td ) {
504        BasicType::Kind ret;
505
506        switch ( td->basictype ) {
507          case DeclarationNode::Void:
508                if ( td->signedness != DeclarationNode::NoSignedness && td->length != DeclarationNode::NoLength ) {
509                        throw SemanticError( "invalid type specifier \"void\" in type: ", td );
510                } // if
511
512                return new VoidType( buildQualifiers( td ) );
513                break;
514
515          case DeclarationNode::Bool:
516                if ( td->signedness != DeclarationNode::NoSignedness ) {
517                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
518                } // if
519                if ( td->length != DeclarationNode::NoLength ) {
520                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
521                } // if
522
523                ret = BasicType::Bool;
524                break;
525
526          case DeclarationNode::Char:
527                // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
528                // character types. The implementation shall define char to have the same range, representation, and behavior as
529                // either signed char or unsigned char.
530                static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
531
532                if ( td->length != DeclarationNode::NoLength ) {
533                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
534                } // if
535
536                ret = chartype[ td->signedness ];
537                break;
538
539          case DeclarationNode::Int:
540                static BasicType::Kind inttype[2][4] = {
541                        { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
542                        { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
543                };
544
545          Integral: ;
546                if ( td->signedness == DeclarationNode::NoSignedness ) {
547                        const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
548                } // if
549                ret = inttype[ td->signedness ][ td->length ];
550                break;
551
552          case DeclarationNode::Float:
553          case DeclarationNode::Double:
554          case DeclarationNode::LongDouble:                                     // not set until below
555                static BasicType::Kind floattype[3][3] = {
556                        { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
557                        { BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary },
558                        { BasicType::Float, BasicType::Double, BasicType::LongDouble },
559                };
560
561          FloatingPoint: ;
562                if ( td->signedness != DeclarationNode::NoSignedness ) {
563                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
564                } // if
565                if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
566                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
567                } // if
568                if ( td->basictype == DeclarationNode::Float && td->length == DeclarationNode::Long ) {
569                        throw SemanticError( "invalid type specifier \"long\" in type: ", td );
570                } // if
571                if ( td->length == DeclarationNode::Long ) {
572                        const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
573                } // if
574
575                ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
576                break;
577
578          case DeclarationNode::NoBasicType:
579                // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
580                if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
581                        const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
582                        goto FloatingPoint;
583                } // if
584
585                const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
586                goto Integral;
587          default:
588                assert(false);
589                return nullptr;
590        } // switch
591
592        BasicType * bt = new BasicType( buildQualifiers( td ), ret );
593        buildForall( td->forall, bt->get_forall() );
594        return bt;
595} // buildBasicType
596
597PointerType * buildPointer( const TypeData * td ) {
598        PointerType * pt;
599        if ( td->base ) {
600                pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
601        } else {
602                pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
603        } // if
604        buildForall( td->forall, pt->get_forall() );
605        return pt;
606} // buildPointer
607
608ArrayType * buildArray( const TypeData * td ) {
609        ArrayType * at;
610        if ( td->base ) {
611                at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
612                                                        td->array.isVarLen, td->array.isStatic );
613        } else {
614                at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
615                                                        maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
616        } // if
617        buildForall( td->forall, at->get_forall() );
618        return at;
619} // buildPointer
620
621AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
622        assert( td->kind == TypeData::Aggregate );
623        AggregateDecl * at;
624        switch ( td->aggregate.kind ) {
625          case DeclarationNode::Struct:
626                if ( td->aggregate.tagged ) {
627                        at = new StructDecl( *td->aggregate.name, td->aggregate.parent, attributes, linkage );
628                        buildForall( td->aggregate.params, at->get_parameters() );
629                        break;
630                }
631          case DeclarationNode::Coroutine:
632          case DeclarationNode::Monitor:
633          case DeclarationNode::Thread:
634                at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
635                buildForall( td->aggregate.params, at->get_parameters() );
636                break;
637          case DeclarationNode::Union:
638                at = new UnionDecl( *td->aggregate.name, attributes );
639                buildForall( td->aggregate.params, at->get_parameters() );
640                break;
641          case DeclarationNode::Trait:
642                at = new TraitDecl( *td->aggregate.name, attributes );
643                buildList( td->aggregate.params, at->get_parameters() );
644                break;
645          default:
646                assert( false );
647        } // switch
648
649        buildList( td->aggregate.fields, at->get_members() );
650        at->set_body( td->aggregate.body );
651
652        return at;
653} // buildAggregate
654
655ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
656        switch ( type->kind ) {
657          case TypeData::Enum: {
658                  if ( type->enumeration.body ) {
659                          EnumDecl * typedecl = buildEnum( type, attributes );
660                          return new EnumInstType( buildQualifiers( type ), typedecl );
661                  } else {
662                          return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
663                  } // if
664          }
665          case TypeData::Aggregate: {
666                  ReferenceToType * ret;
667                  if ( type->aggregate.body ) {
668                          AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
669                          switch ( type->aggregate.kind ) {
670                                case DeclarationNode::Struct:
671                                case DeclarationNode::Coroutine:
672                                case DeclarationNode::Monitor:
673                                case DeclarationNode::Thread:
674                                  ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
675                                  break;
676                                case DeclarationNode::Union:
677                                  ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
678                                  break;
679                                case DeclarationNode::Trait:
680                                  assert( false );
681                                  //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
682                                  break;
683                                default:
684                                  assert( false );
685                          } // switch
686                  } else {
687                          switch ( type->aggregate.kind ) {
688                                case DeclarationNode::Struct:
689                                case DeclarationNode::Coroutine:
690                                case DeclarationNode::Monitor:
691                                case DeclarationNode::Thread:
692                                  ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
693                                  break;
694                                case DeclarationNode::Union:
695                                  ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
696                                  break;
697                                case DeclarationNode::Trait:
698                                  ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
699                                  break;
700                                default:
701                                  assert( false );
702                          } // switch
703                  } // if
704                  return ret;
705          }
706          default:
707                assert( false );
708        } // switch
709} // buildAggInst
710
711ReferenceToType * buildAggInst( const TypeData * td ) {
712        assert( td->kind == TypeData::AggregateInst );
713
714        // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
715        ReferenceToType * ret = nullptr;
716        TypeData * type = td->aggInst.aggregate;
717        switch ( type->kind ) {
718          case TypeData::Enum: {
719                  return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
720          }
721          case TypeData::Aggregate: {
722                  switch ( type->aggregate.kind ) {
723                        case DeclarationNode::Struct:
724                        case DeclarationNode::Coroutine:
725                        case DeclarationNode::Monitor:
726                        case DeclarationNode::Thread:
727                          ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
728                          break;
729                        case DeclarationNode::Union:
730                          ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
731                          break;
732                        case DeclarationNode::Trait:
733                          ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
734                          break;
735                        default:
736                          assert( false );
737                  } // switch
738          }
739          break;
740          default:
741                assert( false );
742        } // switch
743
744        ret->set_hoistType( td->aggInst.hoistType );
745        buildList( td->aggInst.params, ret->get_parameters() );
746        buildForall( td->forall, ret->get_forall() );
747        return ret;
748} // buildAggInst
749
750NamedTypeDecl * buildSymbolic( const TypeData * td, const string & name, Type::StorageClasses scs ) {
751        assert( td->kind == TypeData::Symbolic );
752        NamedTypeDecl * ret;
753        assert( td->base );
754        if ( td->symbolic.isTypedef ) {
755                ret = new TypedefDecl( name, scs, typebuild( td->base ) );
756        } else {
757                ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Any );
758        } // if
759        buildList( td->symbolic.params, ret->get_parameters() );
760        buildList( td->symbolic.assertions, ret->get_assertions() );
761        return ret;
762} // buildSymbolic
763
764EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes ) {
765        assert( td->kind == TypeData::Enum );
766        EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes );
767        buildList( td->enumeration.constants, ret->get_members() );
768        list< Declaration * >::iterator members = ret->get_members().begin();
769        for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
770                if ( cur->has_enumeratorValue() ) {
771                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
772                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
773                } // if
774        } // for
775        ret->set_body( td->enumeration.body );
776        return ret;
777} // buildEnum
778
779TypeInstType * buildSymbolicInst( const TypeData * td ) {
780        assert( td->kind == TypeData::SymbolicInst );
781        TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
782        buildList( td->symbolic.actuals, ret->get_parameters() );
783        buildForall( td->forall, ret->get_forall() );
784        return ret;
785} // buildSymbolicInst
786
787TupleType * buildTuple( const TypeData * td ) {
788        assert( td->kind == TypeData::Tuple );
789        std::list< Type * > types;
790        buildTypeList( td->tuple, types );
791        TupleType * ret = new TupleType( buildQualifiers( td ), types );
792        buildForall( td->forall, ret->get_forall() );
793        return ret;
794} // buildTuple
795
796TypeofType * buildTypeof( const TypeData * td ) {
797        assert( td->kind == TypeData::Typeof );
798        assert( td->typeexpr );
799        // assert( td->typeexpr->expr );
800        return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
801} // buildTypeof
802
803Declaration * 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 ) {
804        if ( td->kind == TypeData::Function ) {
805                if ( td->function.idList ) {                                    // KR function ?
806                        buildKRFunction( td->function );                        // transform into C11 function
807                } // if
808
809                FunctionDecl * decl;
810                Statement * stmt = maybeBuild<Statement>( td->function.body );
811                CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
812                decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
813                return decl->set_asmName( asmName );
814        } else if ( td->kind == TypeData::Aggregate ) {
815                return buildAggregate( td, attributes, linkage );
816        } else if ( td->kind == TypeData::Enum ) {
817                return buildEnum( td, attributes );
818        } else if ( td->kind == TypeData::Symbolic ) {
819                return buildSymbolic( td, name, scs );
820        } else {
821                return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
822        } // if
823        return nullptr;
824} // buildDecl
825
826FunctionType * buildFunction( const TypeData * td ) {
827        assert( td->kind == TypeData::Function );
828        bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true;
829        if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle;
830        FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis );
831        buildList( td->function.params, ft->get_parameters() );
832        buildForall( td->forall, ft->get_forall() );
833        if ( td->base ) {
834                switch ( td->base->kind ) {
835                  case TypeData::Tuple:
836                        buildList( td->base->tuple, ft->get_returnVals() );
837                        break;
838                  default:
839                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
840                } // switch
841        } else {
842                ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
843        } // if
844        return ft;
845} // buildFunction
846
847// Transform KR routine declarations into C99 routine declarations:
848//
849//    rtn( a, b, c ) int a, c; double b {}  =>  int rtn( int a, double c, int b ) {}
850//
851// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
852// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
853// extra names are disallowed.
854//
855// Note, there is no KR routine-prototype syntax:
856//
857//    rtn( a, b, c ) int a, c; double b; // invalid KR prototype
858//    rtn(); // valid KR prototype
859
860void buildKRFunction( const TypeData::Function_t & function ) {
861        assert( ! function.params );
862        // loop over declaration first as it is easier to spot errors
863        for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
864                // scan ALL parameter names for each declaration name to check for duplicates
865                for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
866                        if ( *decl->name == *param->name ) {
867                                // type set => parameter name already transformed by a declaration names so there is a duplicate
868                                // declaration name attempting a second transformation
869                                if ( param->type ) throw SemanticError( string( "duplicate declaration name " ) + *param->name );
870                                // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
871                                // parameter name attempting a second transformation
872                                if ( ! decl->type ) throw SemanticError( string( "duplicate parameter name " ) + *param->name );
873                                param->type = decl->type;                               // set copy declaration type to parameter type
874                                decl->type = nullptr;                                   // reset declaration type
875                                param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
876                        } // if
877                } // for
878                // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
879                if ( decl->type ) throw SemanticError( string( "missing name in parameter list " ) + *decl->name );
880        } // for
881
882        // Parameter names without a declaration default to type int:
883        //
884        //    rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
885
886        for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
887                if ( ! param->type ) {                                                  // generate type int for empty parameter type
888                        param->type = new TypeData( TypeData::Basic );
889                        param->type->basictype = DeclarationNode::Int;
890                } // if
891        } // for
892
893        function.params = function.idList;                                      // newly modified idList becomes parameters
894        function.idList = nullptr;                                                      // idList now empty
895        delete function.oldDeclList;                                            // deletes entire list
896        function.oldDeclList = nullptr;                                         // reset
897} // buildKRFunction
898
899// Local Variables: //
900// tab-width: 4 //
901// mode: c++ //
902// compile-command: "make install" //
903// End: //
Note: See TracBrowser for help on using the repository browser.