source: src/Parser/TypeData.cc @ 409433da

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

support coroutine, monitor, thread as kind of structure

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