source: src/Parser/TypeData.cc @ 764db43

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 764db43 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
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
[fa4805f]11// Last Modified By : Andrew Beach
[ac10576]12// Last Modified On : Tus Jul 18 10:10:00 2017
13// Update Count     : 566
[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;
[6ea87486]65                aggregate.tagged = false;
66                aggregate.parent = nullptr;
[b87a5ed]67                break;
68          case AggregateInst:
[8f6f47d7]69                // aggInst = new AggInst_t;
[2298f728]70                aggInst.aggregate = nullptr;
71                aggInst.params = nullptr;
[43c89a7]72                aggInst.hoistType = false;;
[b87a5ed]73                break;
74          case Symbolic:
75          case SymbolicInst:
[8f6f47d7]76                // symbolic = new Symbolic_t;
[2298f728]77                symbolic.name = nullptr;
78                symbolic.params = nullptr;
79                symbolic.actuals = nullptr;
80                symbolic.assertions = nullptr;
[b87a5ed]81                break;
82          case Tuple:
[8f6f47d7]83                // tuple = new Tuple_t;
84                tuple = nullptr;
[b87a5ed]85                break;
86          case Typeof:
[8f6f47d7]87                // typeexpr = new Typeof_t;
88                typeexpr = nullptr;
[b87a5ed]89                break;
[28307be]90          case Builtin:
91                // builtin = new Builtin_t;
[b87a5ed]92                break;
[68cd1ce]93        } // switch
[413ad05]94} // TypeData::TypeData
[51b7345]95
[c8ffe20b]96TypeData::~TypeData() {
[b87a5ed]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:
[8f6f47d7]107                // delete basic;
[b87a5ed]108                break;
109          case Array:
[8f6f47d7]110                delete array.dimension;
111                // delete array;
[b87a5ed]112                break;
113          case Function:
[8f6f47d7]114                delete function.params;
115                delete function.idList;
116                delete function.oldDeclList;
117                delete function.body;
118                // delete function;
[b87a5ed]119                break;
120          case Aggregate:
[2298f728]121                delete aggregate.name;
[8f6f47d7]122                delete aggregate.params;
123                delete aggregate.actuals;
124                delete aggregate.fields;
[6ea87486]125                delete aggregate.parent;
[8f6f47d7]126                // delete aggregate;
[b87a5ed]127                break;
128          case AggregateInst:
[8f6f47d7]129                delete aggInst.aggregate;
130                delete aggInst.params;
131                // delete aggInst;
[b87a5ed]132                break;
133          case Enum:
[2298f728]134                delete enumeration.name;
[8f6f47d7]135                delete enumeration.constants;
136                // delete enumeration;
[b87a5ed]137                break;
138          case Symbolic:
139          case SymbolicInst:
[2298f728]140                delete symbolic.name;
[8f6f47d7]141                delete symbolic.params;
142                delete symbolic.actuals;
143                delete symbolic.assertions;
144                // delete symbolic;
[b87a5ed]145                break;
146          case Tuple:
[8f6f47d7]147                // delete tuple->members;
[b87a5ed]148                delete tuple;
149                break;
150          case Typeof:
[8f6f47d7]151                // delete typeexpr->expr;
[b87a5ed]152                delete typeexpr;
153                break;
[28307be]154          case Builtin:
155                // delete builtin;
156                break;
[68cd1ce]157        } // switch
[413ad05]158} // TypeData::~TypeData
[51b7345]159
[413ad05]160TypeData * TypeData::clone() const {
161        TypeData * newtype = new TypeData( kind );
[738e304]162        newtype->qualifiers = qualifiers;
[b87a5ed]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:
[5b639ee]173                newtype->basictype = basictype;
174                newtype->complextype = complextype;
175                newtype->signedness = signedness;
176                newtype->length = length;
[b87a5ed]177                break;
178          case Array:
[8f6f47d7]179                newtype->array.dimension = maybeClone( array.dimension );
180                newtype->array.isVarLen = array.isVarLen;
181                newtype->array.isStatic = array.isStatic;
[b87a5ed]182                break;
183          case Function:
[8f6f47d7]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;
[b87a5ed]189                break;
190          case Aggregate:
[2298f728]191                newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
[8f6f47d7]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;
[6ea87486]197                newtype->aggregate.tagged = aggregate.tagged;
198                newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
[b87a5ed]199                break;
200          case AggregateInst:
[8f6f47d7]201                newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
202                newtype->aggInst.params = maybeClone( aggInst.params );
[43c89a7]203                newtype->aggInst.hoistType = aggInst.hoistType;
[b87a5ed]204                break;
205          case Enum:
[2298f728]206                newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
[8f6f47d7]207                newtype->enumeration.constants = maybeClone( enumeration.constants );
[ca1a547]208                newtype->enumeration.body = enumeration.body;
[b87a5ed]209                break;
210          case Symbolic:
211          case SymbolicInst:
[2298f728]212                newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
[8f6f47d7]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;
[b87a5ed]217                break;
218          case Tuple:
[8f6f47d7]219                newtype->tuple = maybeClone( tuple );
[b87a5ed]220                break;
221          case Typeof:
[8f6f47d7]222                newtype->typeexpr = maybeClone( typeexpr );
[b87a5ed]223                break;
[90c3b1c]224          case Builtin:
[4cb935e]225                assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
226                newtype->builtintype = builtintype;
[90c3b1c]227                break;
[68cd1ce]228        } // switch
[b87a5ed]229        return newtype;
[413ad05]230} // TypeData::clone
[51b7345]231
[2298f728]232void TypeData::print( ostream &os, int indent ) const {
[738e304]233        for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
[615a096]234                if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
[c1c1112]235        } // for
[b87a5ed]236
237        if ( forall ) {
238                os << "forall " << endl;
[721f17a]239                forall->printList( os, indent + 4 );
[68cd1ce]240        } // if
[b87a5ed]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 );
[68cd1ce]251                } // if
[b87a5ed]252                break;
253          case EnumConstant:
254                os << "enumeration constant ";
255                break;
256          case Basic:
[dd020c0]257                if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
258                if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
[5b639ee]259                assert( basictype != DeclarationNode::NoBasicType );
[dd020c0]260                os << DeclarationNode::basicTypeNames[ basictype ] << " ";
261                if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
[b87a5ed]262                break;
263          case Array:
[8f6f47d7]264                if ( array.isStatic ) {
[b87a5ed]265                        os << "static ";
[68cd1ce]266                } // if
[8f6f47d7]267                if ( array.dimension ) {
[b87a5ed]268                        os << "array of ";
[8f6f47d7]269                        array.dimension->printOneLine( os, indent );
270                } else if ( array.isVarLen ) {
[b87a5ed]271                        os << "variable-length array of ";
272                } else {
273                        os << "open array of ";
[68cd1ce]274                } // if
[b87a5ed]275                if ( base ) {
276                        base->print( os, indent );
[68cd1ce]277                } // if
[b87a5ed]278                break;
279          case Function:
280                os << "function" << endl;
[8f6f47d7]281                if ( function.params ) {
[721f17a]282                        os << string( indent + 2, ' ' ) << "with parameters " << endl;
[8f6f47d7]283                        function.params->printList( os, indent + 4 );
[b87a5ed]284                } else {
[721f17a]285                        os << string( indent + 2, ' ' ) << "with no parameters " << endl;
[68cd1ce]286                } // if
[8f6f47d7]287                if ( function.idList ) {
[721f17a]288                        os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
[8f6f47d7]289                        function.idList->printList( os, indent + 4 );
[68cd1ce]290                } // if
[8f6f47d7]291                if ( function.oldDeclList ) {
[721f17a]292                        os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
[8f6f47d7]293                        function.oldDeclList->printList( os, indent + 4 );
[68cd1ce]294                } // if
[721f17a]295                os << string( indent + 2, ' ' ) << "returning ";
[b87a5ed]296                if ( base ) {
[721f17a]297                        base->print( os, indent + 4 );
[b87a5ed]298                } else {
299                        os << "nothing ";
[68cd1ce]300                } // if
[b87a5ed]301                os << endl;
[8f6f47d7]302                if ( function.body ) {
[ca1a547]303                        os << string( indent + 2, ' ' ) << "with body " << endl;
[8f6f47d7]304                        function.body->printList( os, indent + 2 );
[68cd1ce]305                } // if
[b87a5ed]306                break;
307          case Aggregate:
[dd020c0]308                os << DeclarationNode::aggregateNames[ aggregate.kind ] << ' ' << *aggregate.name << endl;
[8f6f47d7]309                if ( aggregate.params ) {
[721f17a]310                        os << string( indent + 2, ' ' ) << "with type parameters " << endl;
[8f6f47d7]311                        aggregate.params->printList( os, indent + 4 );
[68cd1ce]312                } // if
[8f6f47d7]313                if ( aggregate.actuals ) {
[721f17a]314                        os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
[8f6f47d7]315                        aggregate.actuals->printList( os, indent + 4 );
[68cd1ce]316                } // if
[8f6f47d7]317                if ( aggregate.fields ) {
[721f17a]318                        os << string( indent + 2, ' ' ) << "with members " << endl;
[8f6f47d7]319                        aggregate.fields->printList( os, indent + 4 );
[5d125e4]320                } // if
[8f6f47d7]321                if ( aggregate.body ) {
[5d125e4]322                        os << string( indent + 2, ' ' ) << " with body " << endl;
[68cd1ce]323                } // if
[b87a5ed]324                break;
325          case AggregateInst:
[8f6f47d7]326                if ( aggInst.aggregate ) {
[b87a5ed]327                        os << "instance of " ;
[8f6f47d7]328                        aggInst.aggregate->print( os, indent );
[b87a5ed]329                } else {
330                        os << "instance of an unspecified aggregate ";
[68cd1ce]331                } // if
[8f6f47d7]332                if ( aggInst.params ) {
[721f17a]333                        os << string( indent + 2, ' ' ) << "with parameters " << endl;
[8f6f47d7]334                        aggInst.params->printList( os, indent + 2 );
[68cd1ce]335                } // if
[b87a5ed]336                break;
337          case Enum:
338                os << "enumeration ";
[8f6f47d7]339                if ( enumeration.constants ) {
[b87a5ed]340                        os << "with constants" << endl;
[8f6f47d7]341                        enumeration.constants->printList( os, indent + 2 );
[68cd1ce]342                } // if
[ca1a547]343                if ( enumeration.body ) {
344                        os << string( indent + 2, ' ' ) << " with body " << endl;
345                } // if
[b87a5ed]346                break;
347          case SymbolicInst:
[2298f728]348                os << "instance of type " << *symbolic.name;
[8f6f47d7]349                if ( symbolic.actuals ) {
[b87a5ed]350                        os << " with parameters" << endl;
[8f6f47d7]351                        symbolic.actuals->printList( os, indent + 2 );
[68cd1ce]352                } // if
[b87a5ed]353                break;
354          case Symbolic:
[8f6f47d7]355                if ( symbolic.isTypedef ) {
[b87a5ed]356                        os << "typedef definition ";
357                } else {
358                        os << "type definition ";
[68cd1ce]359                } // if
[8f6f47d7]360                if ( symbolic.params ) {
[721f17a]361                        os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
[8f6f47d7]362                        symbolic.params->printList( os, indent + 2 );
[68cd1ce]363                } // if
[8f6f47d7]364                if ( symbolic.assertions ) {
[721f17a]365                        os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
[8f6f47d7]366                        symbolic.assertions->printList( os, indent + 4 );
[721f17a]367                        os << string( indent + 2, ' ' );
[68cd1ce]368                } // if
[b87a5ed]369                if ( base ) {
370                        os << "for ";
371                        base->print( os, indent + 2 );
[68cd1ce]372                } // if
[b87a5ed]373                break;
374          case Tuple:
375                os << "tuple ";
[8f6f47d7]376                if ( tuple ) {
[b87a5ed]377                        os << "with members " << endl;
[8f6f47d7]378                        tuple->printList( os, indent + 2 );
[68cd1ce]379                } // if
[b87a5ed]380                break;
381          case Typeof:
382                os << "type-of expression ";
[8f6f47d7]383                if ( typeexpr ) {
384                        typeexpr->print( os, indent + 2 );
[68cd1ce]385                } // if
[b87a5ed]386                break;
[90c3b1c]387          case Builtin:
388                os << "gcc builtin type";
389                break;
[1db21619]390          default:
[5b639ee]391                os << "internal error: TypeData::print " << kind << endl;
[1db21619]392                assert( false );
[68cd1ce]393        } // switch
[413ad05]394} // TypeData::print
[51b7345]395
[8c49c0e]396template< typename ForallList >
397void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
[b87a5ed]398        buildList( firstNode, outputList );
[8c49c0e]399        for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i ) {
[a7c90d4]400                TypeDecl * td = static_cast<TypeDecl *>(*i);
[8c49c0e]401                if ( td->get_kind() == TypeDecl::Any ) {
[6943f051]402                        // add assertion parameters to `type' tyvars in reverse order
403                        // add dtor:  void ^?{}(T *)
[413ad05]404                        FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
[68fe077a]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 ) );
[4cc4286]407
[a9a259c]408                        // add copy ctor:  void ?{}(T *, T)
[413ad05]409                        FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
[68fe077a]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 ) );
[a9a259c]413
[6943f051]414                        // add default ctor:  void ?{}(T *)
[413ad05]415                        FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
[68fe077a]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 ) );
[6943f051]418
419                        // add assignment operator:  T * ?=?(T *, T)
[413ad05]420                        FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
[68fe077a]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 ) );
[68cd1ce]425                } // if
426        } // for
[51b7345]427}
428
[413ad05]429Type * typebuild( const TypeData * td ) {
430        assert( td );
431        switch ( td->kind ) {
432          case TypeData::Unknown:
[b87a5ed]433                // fill in implicit int
[413ad05]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:
[b87a5ed]446                // the name gets filled in later -- by SymTab::Validate
[413ad05]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:
[148f7290]455                if(td->builtintype == DeclarationNode::Zero) {
[ac10576]456                        return new ZeroType( noQualifiers );
[148f7290]457                }
458                else if(td->builtintype == DeclarationNode::One) {
[ac10576]459                        return new OneType( noQualifiers );
[148f7290]460                }
461                else {
462                        return new VarArgsType( buildQualifiers( td ) );
463                }
[413ad05]464          case TypeData::Symbolic:
465          case TypeData::Enum:
466          case TypeData::Aggregate:
[b87a5ed]467                assert( false );
[68cd1ce]468        } // switch
[2298f728]469        return nullptr;
[413ad05]470} // typebuild
471
472TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
[2298f728]473        TypeData * ret = nullptr;
[51b7345]474
[413ad05]475        switch ( td->kind ) {
476          case TypeData::Aggregate:
[8f6f47d7]477                if ( ! toplevel && td->aggregate.fields ) {
[413ad05]478                        ret = td->clone();
479                } // if
480                break;
481          case TypeData::Enum:
[8f6f47d7]482                if ( ! toplevel && td->enumeration.constants ) {
[413ad05]483                        ret = td->clone();
484                } // if
485                break;
486          case TypeData::AggregateInst:
[8f6f47d7]487                if ( td->aggInst.aggregate ) {
488                        ret = typeextractAggregate( td->aggInst.aggregate, false );
[413ad05]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 ) {
[738e304]500        return td->qualifiers;
[413ad05]501} // buildQualifiers
[51b7345]502
[413ad05]503Type * buildBasicType( const TypeData * td ) {
[b87a5ed]504        BasicType::Kind ret;
505
[5b639ee]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 );
[68cd1ce]510                } // if
[b87a5ed]511
[5b639ee]512                return new VoidType( buildQualifiers( td ) );
513                break;
514
515          case DeclarationNode::Bool:
516                if ( td->signedness != DeclarationNode::NoSignedness ) {
[dd020c0]517                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
[5b639ee]518                } // if
519                if ( td->length != DeclarationNode::NoLength ) {
[dd020c0]520                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
[68cd1ce]521                } // if
[51b7345]522
[5b639ee]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.
[8c49c0e]530                static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
[5b639ee]531
532                if ( td->length != DeclarationNode::NoLength ) {
[dd020c0]533                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
[5b639ee]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 ) {
[dd020c0]563                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
[5b639ee]564                } // if
565                if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
[dd020c0]566                        throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
[5b639ee]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;
[2ee5426]587          default:
588                assert(false);
589                return nullptr;
[5b639ee]590        } // switch
591
592        BasicType * bt = new BasicType( buildQualifiers( td ), ret );
[413ad05]593        buildForall( td->forall, bt->get_forall() );
[b87a5ed]594        return bt;
[413ad05]595} // buildBasicType
[51b7345]596
[413ad05]597PointerType * buildPointer( const TypeData * td ) {
598        PointerType * pt;
599        if ( td->base ) {
600                pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
[b87a5ed]601        } else {
[413ad05]602                pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[68cd1ce]603        } // if
[413ad05]604        buildForall( td->forall, pt->get_forall() );
[b87a5ed]605        return pt;
[413ad05]606} // buildPointer
[51b7345]607
[413ad05]608ArrayType * buildArray( const TypeData * td ) {
609        ArrayType * at;
610        if ( td->base ) {
[8f6f47d7]611                at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
612                                                        td->array.isVarLen, td->array.isStatic );
[b87a5ed]613        } else {
[413ad05]614                at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
[8f6f47d7]615                                                        maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
[68cd1ce]616        } // if
[413ad05]617        buildForall( td->forall, at->get_forall() );
[b87a5ed]618        return at;
[413ad05]619} // buildPointer
[51b7345]620
[fa4805f]621AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
[413ad05]622        assert( td->kind == TypeData::Aggregate );
623        AggregateDecl * at;
[8f6f47d7]624        switch ( td->aggregate.kind ) {
[b87a5ed]625          case DeclarationNode::Struct:
[6ea87486]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                }
[409433da]631          case DeclarationNode::Coroutine:
632          case DeclarationNode::Monitor:
633          case DeclarationNode::Thread:
[fa4805f]634                at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
[8f6f47d7]635                buildForall( td->aggregate.params, at->get_parameters() );
[b87a5ed]636                break;
637          case DeclarationNode::Union:
[c0aa336]638                at = new UnionDecl( *td->aggregate.name, attributes );
[8f6f47d7]639                buildForall( td->aggregate.params, at->get_parameters() );
[b87a5ed]640                break;
[4040425]641          case DeclarationNode::Trait:
[c0aa336]642                at = new TraitDecl( *td->aggregate.name, attributes );
[8f6f47d7]643                buildList( td->aggregate.params, at->get_parameters() );
[b87a5ed]644                break;
645          default:
646                assert( false );
[68cd1ce]647        } // switch
[4e06c1e]648
[8f6f47d7]649        buildList( td->aggregate.fields, at->get_members() );
650        at->set_body( td->aggregate.body );
[b87a5ed]651
652        return at;
[413ad05]653} // buildAggregate
[51b7345]654
[fa4805f]655ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
[43c89a7]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 ) {
[fa4805f]668                          AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
[43c89a7]669                          switch ( type->aggregate.kind ) {
670                                case DeclarationNode::Struct:
[409433da]671                                case DeclarationNode::Coroutine:
672                                case DeclarationNode::Monitor:
673                                case DeclarationNode::Thread:
[43c89a7]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:
[409433da]689                                case DeclarationNode::Coroutine:
690                                case DeclarationNode::Monitor:
691                                case DeclarationNode::Thread:
[43c89a7]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
[413ad05]711ReferenceToType * buildAggInst( const TypeData * td ) {
712        assert( td->kind == TypeData::AggregateInst );
[b87a5ed]713
[43c89a7]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:
[409433da]724                        case DeclarationNode::Coroutine:
725                        case DeclarationNode::Monitor:
726                        case DeclarationNode::Thread:
[43c89a7]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 );
[8f6f47d7]745        buildList( td->aggInst.params, ret->get_parameters() );
[413ad05]746        buildForall( td->forall, ret->get_forall() );
[b87a5ed]747        return ret;
[413ad05]748} // buildAggInst
749
[68fe077a]750NamedTypeDecl * buildSymbolic( const TypeData * td, const string & name, Type::StorageClasses scs ) {
[413ad05]751        assert( td->kind == TypeData::Symbolic );
752        NamedTypeDecl * ret;
753        assert( td->base );
[8f6f47d7]754        if ( td->symbolic.isTypedef ) {
[a7c90d4]755                ret = new TypedefDecl( name, scs, typebuild( td->base ) );
[b87a5ed]756        } else {
[a7c90d4]757                ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Any );
[68cd1ce]758        } // if
[8f6f47d7]759        buildList( td->symbolic.params, ret->get_parameters() );
760        buildList( td->symbolic.assertions, ret->get_assertions() );
[b87a5ed]761        return ret;
[413ad05]762} // buildSymbolic
[51b7345]763
[c0aa336]764EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes ) {
[413ad05]765        assert( td->kind == TypeData::Enum );
[c0aa336]766        EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes );
[8f6f47d7]767        buildList( td->enumeration.constants, ret->get_members() );
[2298f728]768        list< Declaration * >::iterator members = ret->get_members().begin();
[8f6f47d7]769        for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
[4f147cc]770                if ( cur->has_enumeratorValue() ) {
[413ad05]771                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
[e4d829b]772                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
[90c3b1c]773                } // if
774        } // for
[ca1a547]775        ret->set_body( td->enumeration.body );
[b87a5ed]776        return ret;
[413ad05]777} // buildEnum
[51b7345]778
[413ad05]779TypeInstType * buildSymbolicInst( const TypeData * td ) {
780        assert( td->kind == TypeData::SymbolicInst );
[2298f728]781        TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
[8f6f47d7]782        buildList( td->symbolic.actuals, ret->get_parameters() );
[413ad05]783        buildForall( td->forall, ret->get_forall() );
[b87a5ed]784        return ret;
[413ad05]785} // buildSymbolicInst
[51b7345]786
[413ad05]787TupleType * buildTuple( const TypeData * td ) {
788        assert( td->kind == TypeData::Tuple );
[62423350]789        std::list< Type * > types;
790        buildTypeList( td->tuple, types );
791        TupleType * ret = new TupleType( buildQualifiers( td ), types );
[413ad05]792        buildForall( td->forall, ret->get_forall() );
[b87a5ed]793        return ret;
[413ad05]794} // buildTuple
795
796TypeofType * buildTypeof( const TypeData * td ) {
797        assert( td->kind == TypeData::Typeof );
798        assert( td->typeexpr );
[8f6f47d7]799        // assert( td->typeexpr->expr );
800        return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
[413ad05]801} // buildTypeof
802
[ddfd945]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 ) {
[413ad05]804        if ( td->kind == TypeData::Function ) {
[ca1a547]805                if ( td->function.idList ) {                                    // KR function ?
806                        buildKRFunction( td->function );                        // transform into C11 function
[3a5131ed]807                } // if
808
[413ad05]809                FunctionDecl * decl;
[ca1a547]810                Statement * stmt = maybeBuild<Statement>( td->function.body );
[a7c90d4]811                CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
812                decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
[58dd019]813                return decl->set_asmName( asmName );
[413ad05]814        } else if ( td->kind == TypeData::Aggregate ) {
[fa4805f]815                return buildAggregate( td, attributes, linkage );
[413ad05]816        } else if ( td->kind == TypeData::Enum ) {
[c0aa336]817                return buildEnum( td, attributes );
[413ad05]818        } else if ( td->kind == TypeData::Symbolic ) {
[a7c90d4]819                return buildSymbolic( td, name, scs );
[413ad05]820        } else {
[a7c90d4]821                return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
[413ad05]822        } // if
[2298f728]823        return nullptr;
[413ad05]824} // buildDecl
825
826FunctionType * buildFunction( const TypeData * td ) {
827        assert( td->kind == TypeData::Function );
[8f6f47d7]828        bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true;
829        if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle;
[413ad05]830        FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis );
[8f6f47d7]831        buildList( td->function.params, ft->get_parameters() );
[413ad05]832        buildForall( td->forall, ft->get_forall() );
833        if ( td->base ) {
834                switch ( td->base->kind ) {
835                  case TypeData::Tuple:
[8f6f47d7]836                        buildList( td->base->tuple, ft->get_returnVals() );
[413ad05]837                        break;
838                  default:
[ddfd945]839                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
[413ad05]840                } // switch
841        } else {
[68fe077a]842                ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
[413ad05]843        } // if
844        return ft;
845} // buildFunction
[b87a5ed]846
[a7c4921]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
[3a5131ed]860void buildKRFunction( const TypeData::Function_t & function ) {
861        assert( ! function.params );
[a7c4921]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
[a7c90d4]865                for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
[3a5131ed]866                        if ( *decl->name == *param->name ) {
[a7c4921]867                                // type set => parameter name already transformed by a declaration names so there is a duplicate
868                                // declaration name attempting a second transformation
[3a5131ed]869                                if ( param->type ) throw SemanticError( string( "duplicate declaration name " ) + *param->name );
[a7c4921]870                                // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
871                                // parameter name attempting a second transformation
[3a5131ed]872                                if ( ! decl->type ) throw SemanticError( string( "duplicate parameter name " ) + *param->name );
[a7c4921]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
[3a5131ed]876                        } // if
877                } // for
[a7c4921]878                // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
[3a5131ed]879                if ( decl->type ) throw SemanticError( string( "missing name in parameter list " ) + *decl->name );
880        } // for
[a7c4921]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
[a7c90d4]886        for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
[ca1a547]887                if ( ! param->type ) {                                                  // generate type int for empty parameter type
[3a5131ed]888                        param->type = new TypeData( TypeData::Basic );
889                        param->type->basictype = DeclarationNode::Int;
890                } // if
891        } // for
892
[a7c4921]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
[3a5131ed]897} // buildKRFunction
898
[b87a5ed]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.