source: src/Parser/TypeData.cc @ bf76eab

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

add documentation to routine buildKRFunction

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