Ignore:
Timestamp:
Apr 4, 2023, 2:25:52 PM (15 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
beeff61e, e02e13f
Parents:
4541b09
Message:

Translated parser to the new ast. This incuded a small fix in the resolver so larger expressions can be used in with statements and some updated tests. errors/declaration just is a formatting update. attributes now actually preserves more attributes (unknown if all versions work).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r4541b09 rbb7422a  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Mar 14 12:00:00 2023
    13 // Update Count     : 1082
     12// Last Modified On : Tue Apr  4 11:07:00 2023
     13// Update Count     : 1083
    1414//
    1515
     
    2121#include <string>                  // for string, operator+, operator==
    2222
     23#include "AST/Expr.hpp"            // for NameExpr
     24#include "AST/Type.hpp"            // for BaseType, SueInstType
    2325#include "Common/SemanticError.h"  // for SemanticError
    2426#include "Common/utility.h"        // for maybeMoveBuild, maybeBuild, CodeLo...
    2527#include "ParseNode.h"             // for ExpressionNode, maybeMoveBuildType
    26 #include "SynTree/Constant.h"      // for Constant
    27 #include "SynTree/Declaration.h"   // for EnumDecl, StructDecl, UnionDecl
    28 #include "SynTree/Expression.h"    // for Expression, ConstantExpr, NameExpr
    29 #include "SynTree/Statement.h"     // for CompoundStmt, Statement
    30 #include "SynTree/Type.h"          // for BasicType, Type, Type::Qualifiers
    3128#include "parserutility.h"         // for notZeroExpr
    32 
    33 class Initializer;
    3429
    3530using namespace std;
     
    4843// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
    4944// type.
    50 
    51 extern const Type::Qualifiers noQualifiers;                             // no qualifiers on constants
    5245
    5346// static inline bool checkH( char c ) { return c == 'h' || c == 'H'; }
     
    127120} // scanbin
    128121
    129 Expression * build_constantInteger( string & str ) {
    130         static const BasicType::Kind kind[2][6] = {
     122ast::Expr * build_constantInteger(
     123                const CodeLocation & location, string & str ) {
     124        static const ast::BasicType::Kind kind[2][6] = {
    131125                // short (h) must be before char (hh) because shorter type has the longer suffix
    132                 { BasicType::ShortSignedInt, BasicType::SignedChar, BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, /* BasicType::SignedInt128 */ BasicType::LongLongSignedInt, },
    133                 { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, /* BasicType::UnsignedInt128 */ BasicType::LongLongUnsignedInt, },
     126                { ast::BasicType::ShortSignedInt, ast::BasicType::SignedChar, ast::BasicType::SignedInt, ast::BasicType::LongSignedInt, ast::BasicType::LongLongSignedInt, /* BasicType::SignedInt128 */ ast::BasicType::LongLongSignedInt, },
     127                { ast::BasicType::ShortUnsignedInt, ast::BasicType::UnsignedChar, ast::BasicType::UnsignedInt, ast::BasicType::LongUnsignedInt, ast::BasicType::LongLongUnsignedInt, /* BasicType::UnsignedInt128 */ ast::BasicType::LongLongUnsignedInt, },
    134128        };
    135129
     
    141135        string str2( "0x0" );
    142136        unsigned long long int v, v2 = 0;                                       // converted integral value
    143         Expression * ret, * ret2;
     137        ast::Expr * ret, * ret2;
    144138
    145139        int type = -1;                                                                          // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128
     
    149143        // special constants
    150144        if ( str == "0" ) {
    151                 ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) );
     145                ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0 );
    152146                goto CLEANUP;
    153147        } // if
    154148        if ( str == "1" ) {
    155                 ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) );
     149                ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1 );
    156150                goto CLEANUP;
    157151        } // if
     
    304298
    305299        // Constant type is correct for overload resolving.
    306         ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) );
     300        ret = new ast::ConstantExpr( location,
     301                new ast::BasicType( kind[Unsigned][type] ), str, v );
    307302        if ( Unsigned && type < 2 ) {                                           // hh or h, less than int ?
    308303                // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
    309                 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
     304                ret = new ast::CastExpr( location,
     305                        ret,
     306                        new ast::BasicType( kind[Unsigned][type] ),
     307                        ast::ExplicitCast );
    310308        } else if ( ltype != -1 ) {                                                     // explicit length ?
    311309                if ( ltype == 6 ) {                                                             // int128, (int128)constant
    312 //                      ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
    313                         ret2 = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::LongLongSignedInt ), str2, v2 ) );
    314                         ret = build_compoundLiteral(
    315                                 DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ),
    316                                 new InitializerNode( (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) );
     310                        ret2 = new ast::ConstantExpr( location,
     311                                new ast::BasicType( ast::BasicType::LongLongSignedInt ),
     312                                str2,
     313                                v2 );
     314                        ret = build_compoundLiteral( location,
     315                                DeclarationNode::newBasicType(
     316                                        DeclarationNode::Int128
     317                                )->addType(
     318                                        DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ),
     319                                new InitializerNode(
     320                                        (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true )
     321                        );
    317322                } else {                                                                                // explicit length, (length_type)constant
    318                         ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false );
     323                        ret = new ast::CastExpr( location,
     324                                ret,
     325                                new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ),
     326                                ast::ExplicitCast );
    319327                        if ( ltype == 5 ) {                                                     // pointer, intptr( (uintptr_t)constant )
    320                                 ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) );
     328                                ret = build_func( location,
     329                                        new ExpressionNode(
     330                                                build_varref( location, new string( "intptr" ) ) ),
     331                                        new ExpressionNode( ret ) );
    321332                        } // if
    322333                } // if
     
    362373
    363374
    364 Expression * build_constantFloat( string & str ) {
    365         static const BasicType::Kind kind[2][12] = {
    366                 { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x },
    367                 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::NUMBER_OF_BASIC_TYPES, BasicType::NUMBER_OF_BASIC_TYPES, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex },
     375ast::Expr * build_constantFloat(
     376                const CodeLocation & location, string & str ) {
     377        static const ast::BasicType::Kind kind[2][12] = {
     378                { ast::BasicType::Float, ast::BasicType::Double, ast::BasicType::LongDouble, ast::BasicType::uuFloat80, ast::BasicType::uuFloat128, ast::BasicType::uFloat16, ast::BasicType::uFloat32, ast::BasicType::uFloat32x, ast::BasicType::uFloat64, ast::BasicType::uFloat64x, ast::BasicType::uFloat128, ast::BasicType::uFloat128x },
     379                { ast::BasicType::FloatComplex, ast::BasicType::DoubleComplex, ast::BasicType::LongDoubleComplex, ast::BasicType::NUMBER_OF_BASIC_TYPES, ast::BasicType::NUMBER_OF_BASIC_TYPES, ast::BasicType::uFloat16Complex, ast::BasicType::uFloat32Complex, ast::BasicType::uFloat32xComplex, ast::BasicType::uFloat64Complex, ast::BasicType::uFloat64xComplex, ast::BasicType::uFloat128Complex, ast::BasicType::uFloat128xComplex },
    368380        };
    369381
     
    402414
    403415        assert( 0 <= type && type < 12 );
    404         Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) );
    405         if ( explnth ) {                                                                        // explicit length ?
    406                 ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false );
     416        ast::Expr * ret = new ast::ConstantExpr( location,
     417                new ast::BasicType( kind[complx][type] ),
     418                str,
     419                v );
     420        // explicit length ?
     421        if ( explnth ) {
     422                ret = new ast::CastExpr( location,
     423                        ret,
     424                        new ast::BasicType( kind[complx][type] ),
     425                        ast::ExplicitCast );
    407426        } // if
    408427
     
    419438} // sepString
    420439
    421 Expression * build_constantChar( string & str ) {
     440ast::Expr * build_constantChar( const CodeLocation & location, string & str ) {
    422441        string units;                                                                           // units
    423442        sepString( str, units, '\'' );                                          // separate constant from units
    424443
    425         Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
     444        ast::Expr * ret = new ast::ConstantExpr( location,
     445                new ast::BasicType( ast::BasicType::Char ),
     446                str,
     447                (unsigned long long int)(unsigned char)str[1] );
    426448        if ( units.length() != 0 ) {
    427                 ret = new UntypedExpr( new NameExpr( units ), { ret } );
     449                ret = new ast::UntypedExpr( location,
     450                        new ast::NameExpr( location, units ),
     451                        { ret } );
    428452        } // if
    429453
     
    432456} // build_constantChar
    433457
    434 Expression * build_constantStr( string & str ) {
     458ast::Expr * build_constantStr(
     459                const CodeLocation & location,
     460                string & str ) {
    435461        assert( str.length() > 0 );
    436462        string units;                                                                           // units
    437463        sepString( str, units, '"' );                                           // separate constant from units
    438464
    439         Type * strtype;
     465        ast::Type * strtype;
    440466        switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
    441467        case 'u':
    442468                if ( str[1] == '8' ) goto Default;                              // utf-8 characters => array of char
    443469                // lookup type of associated typedef
    444                 strtype = new TypeInstType( Type::Qualifiers( ), "char16_t", false );
     470                strtype = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype );
    445471                break;
    446472        case 'U':
    447                 strtype = new TypeInstType( Type::Qualifiers( ), "char32_t", false );
     473                strtype = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype );
    448474                break;
    449475        case 'L':
    450                 strtype = new TypeInstType( Type::Qualifiers( ), "wchar_t", false );
     476                strtype = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype );
    451477                break;
    452478        Default:                                                                                        // char default string type
    453479        default:
    454                 strtype = new BasicType( Type::Qualifiers( ), BasicType::Char );
     480                strtype = new ast::BasicType( ast::BasicType::Char );
    455481        } // switch
    456         ArrayType * at = new ArrayType( noQualifiers, strtype,
    457                                                                         new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
    458                                                                         false, false );
    459         Expression * ret = new ConstantExpr( Constant( at, str, std::nullopt ) );
     482        ast::ArrayType * at = new ast::ArrayType(
     483                strtype,
     484                // Length is adjusted: +1 for '\0' and -2 for '"'
     485                ast::ConstantExpr::from_ulong( location, str.size() + 1 - 2 ),
     486                ast::FixedLen,
     487                ast::DynamicDim );
     488        ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt );
    460489        if ( units.length() != 0 ) {
    461                 ret = new UntypedExpr( new NameExpr( units ), { ret } );
     490                ret = new ast::UntypedExpr( location,
     491                        new ast::NameExpr( location, units ),
     492                        { ret } );
    462493        } // if
    463494
     
    466497} // build_constantStr
    467498
    468 Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
     499ast::Expr * build_field_name_FLOATING_FRACTIONconstant(
     500                const CodeLocation & location, const string & str ) {
    469501        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str );
    470         Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
     502        ast::Expr * ret = build_constantInteger( location,
     503                *new string( str.substr(1) ) );
    471504        delete &str;
    472505        return ret;
    473506} // build_field_name_FLOATING_FRACTIONconstant
    474507
    475 Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) {
     508ast::Expr * build_field_name_FLOATING_DECIMALconstant(
     509                const CodeLocation & location, const string & str ) {
    476510        if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str );
    477         Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
     511        ast::Expr * ret = build_constantInteger(
     512                location, *new string( str.substr( 0, str.size()-1 ) ) );
    478513        delete &str;
    479514        return ret;
    480515} // build_field_name_FLOATING_DECIMALconstant
    481516
    482 Expression * build_field_name_FLOATINGconstant( const string & str ) {
     517ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location,
     518                const string & str ) {
    483519        // str is of the form A.B -> separate at the . and return member expression
    484520        int a, b;
     
    486522        stringstream ss( str );
    487523        ss >> a >> dot >> b;
    488         UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
     524        auto ret = new ast::UntypedMemberExpr( location,
     525                ast::ConstantExpr::from_int( location, b ),
     526                ast::ConstantExpr::from_int( location, a )
     527        );
    489528        delete &str;
    490529        return ret;
    491530} // build_field_name_FLOATINGconstant
    492531
    493 Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
    494         if ( fracts ) {
    495                 if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
    496                         memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
    497                         return memberExpr;
    498                 } else {
    499                         return new UntypedMemberExpr( fracts, fieldName );
    500                 } // if
    501         } // if
    502         return fieldName;
     532ast::Expr * make_field_name_fraction_constants( const CodeLocation & location,
     533                ast::Expr * fieldName,
     534                ast::Expr * fracts ) {
     535        if ( nullptr == fracts ) {
     536                return fieldName;
     537        } else if ( auto memberExpr = dynamic_cast<ast::UntypedMemberExpr *>( fracts ) ) {
     538                memberExpr->member = make_field_name_fraction_constants( location,
     539                        fieldName,
     540                        ast::mutate( memberExpr->aggregate.get() ) );
     541                return memberExpr;
     542        } else {
     543                return new ast::UntypedMemberExpr( location, fracts, fieldName );
     544        } // if
    503545} // make_field_name_fraction_constants
    504546
    505 Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
    506         return make_field_name_fraction_constants( fieldName, maybeMoveBuild( fracts ) );
     547ast::Expr * build_field_name_fraction_constants( const CodeLocation & location,
     548                ast::Expr * fieldName,
     549                ExpressionNode * fracts ) {
     550        return make_field_name_fraction_constants( location, fieldName, maybeMoveBuild( fracts ) );
    507551} // build_field_name_fraction_constants
    508552
    509 NameExpr * build_varref( const string * name ) {
    510         NameExpr * expr = new NameExpr( *name );
     553ast::NameExpr * build_varref( const CodeLocation & location,
     554                const string * name ) {
     555        ast::NameExpr * expr = new ast::NameExpr( location, *name );
    511556        delete name;
    512557        return expr;
    513558} // build_varref
    514559
    515 QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name ) {
    516         Declaration * newDecl = maybeBuild(decl_node);
    517         if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) {
    518                 const Type * t = newDeclWithType->get_type();
    519                 if ( t ) {
    520                         if ( const TypeInstType * typeInst = dynamic_cast<const TypeInstType *>( t ) ) {
    521                                 newDecl= new EnumDecl( typeInst->name );
     560ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location,
     561                const DeclarationNode * decl_node,
     562                const ast::NameExpr * name ) {
     563        ast::Decl * newDecl = maybeBuild( decl_node );
     564        if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
     565                if ( const ast::Type * t = newDeclWithType->get_type() ) {
     566                        if ( auto typeInst = dynamic_cast<const ast::TypeInstType *>( t ) ) {
     567                                newDecl = new ast::EnumDecl( location, typeInst->name );
    522568                        }
    523569                }
    524570        }
    525         return new QualifiedNameExpr( newDecl, name->name );
     571        return new ast::QualifiedNameExpr( location, newDecl, name->name );
    526572}
    527573
    528 QualifiedNameExpr * build_qualified_expr( const EnumDecl * decl_node, const NameExpr * name ) {
    529         EnumDecl * newDecl = const_cast< EnumDecl * >( decl_node );
    530         return new QualifiedNameExpr( newDecl, name->name );
     574ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location,
     575                const ast::EnumDecl * decl,
     576                const ast::NameExpr * name ) {
     577        return new ast::QualifiedNameExpr( location, decl, name->name );
    531578}
    532579
    533 DimensionExpr * build_dimensionref( const string * name ) {
    534         DimensionExpr * expr = new DimensionExpr( *name );
     580ast::DimensionExpr * build_dimensionref( const CodeLocation & location,
     581                const string * name ) {
     582        ast::DimensionExpr * expr = new ast::DimensionExpr( location, *name );
    535583        delete name;
    536584        return expr;
     
    548596}; // OperName
    549597
    550 Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
    551         Type * targetType = maybeMoveBuildType( decl_node );
    552         if ( dynamic_cast< VoidType * >( targetType ) ) {
     598ast::Expr * build_cast( const CodeLocation & location,
     599                DeclarationNode * decl_node,
     600                ExpressionNode * expr_node ) {
     601        ast::Type * targetType = maybeMoveBuildType( decl_node );
     602        if ( dynamic_cast<ast::VoidType *>( targetType ) ) {
    553603                delete targetType;
    554                 return new CastExpr( maybeMoveBuild( expr_node ), false );
     604                return new ast::CastExpr( location,
     605                        maybeMoveBuild( expr_node ),
     606                        ast::ExplicitCast );
    555607        } else {
    556                 return new CastExpr( maybeMoveBuild( expr_node ), targetType, false );
     608                return new ast::CastExpr( location,
     609                        maybeMoveBuild( expr_node ),
     610                        targetType,
     611                        ast::ExplicitCast );
    557612        } // if
    558613} // build_cast
    559614
    560 Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) {
    561         return new KeywordCastExpr( maybeMoveBuild( expr_node ), target );
     615ast::Expr * build_keyword_cast( const CodeLocation & location,
     616                ast::AggregateDecl::Aggregate target,
     617                ExpressionNode * expr_node ) {
     618        return new ast::KeywordCastExpr( location,
     619                maybeMoveBuild( expr_node ),
     620                target
     621        );
    562622}
    563623
    564 Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
    565         return new VirtualCastExpr( maybeMoveBuild( expr_node ), maybeMoveBuildType( decl_node ) );
     624ast::Expr * build_virtual_cast( const CodeLocation & location,
     625                DeclarationNode * decl_node,
     626                ExpressionNode * expr_node ) {
     627        return new ast::VirtualCastExpr( location,
     628                maybeMoveBuild( expr_node ),
     629                maybeMoveBuildType( decl_node )
     630        );
    566631} // build_virtual_cast
    567632
    568 Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
    569         return new UntypedMemberExpr( member, maybeMoveBuild( expr_node ) );
     633ast::Expr * build_fieldSel( const CodeLocation & location,
     634                ExpressionNode * expr_node,
     635                ast::Expr * member ) {
     636        return new ast::UntypedMemberExpr( location,
     637                member,
     638                maybeMoveBuild( expr_node )
     639        );
    570640} // build_fieldSel
    571641
    572 Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
    573         UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
     642ast::Expr * build_pfieldSel( const CodeLocation & location,
     643                ExpressionNode * expr_node,
     644                ast::Expr * member ) {
     645        auto deref = new ast::UntypedExpr( location,
     646                new ast::NameExpr( location, "*?" )
     647        );
    574648        deref->location = expr_node->location;
    575         deref->get_args().push_back( maybeMoveBuild( expr_node ) );
    576         UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
     649        deref->args.push_back( maybeMoveBuild( expr_node ) );
     650        auto ret = new ast::UntypedMemberExpr( location, member, deref );
    577651        return ret;
    578652} // build_pfieldSel
    579653
    580 Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
    581         Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
     654ast::Expr * build_offsetOf( const CodeLocation & location,
     655                DeclarationNode * decl_node,
     656                ast::NameExpr * member ) {
     657        ast::Expr * ret = new ast::UntypedOffsetofExpr( location,
     658                maybeMoveBuildType( decl_node ),
     659                member->name
     660        );
     661        ret->result = new ast::BasicType( ast::BasicType::LongUnsignedInt );
    582662        delete member;
    583663        return ret;
    584664} // build_offsetOf
    585665
    586 Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
    587         return new LogicalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), notZeroExpr( maybeMoveBuild( expr_node2 ) ), kind );
     666ast::Expr * build_and_or( const CodeLocation & location,
     667                ExpressionNode * expr_node1,
     668                ExpressionNode * expr_node2,
     669                ast::LogicalFlag flag ) {
     670        return new ast::LogicalExpr( location,
     671                notZeroExpr( maybeMoveBuild( expr_node1 ) ),
     672                notZeroExpr( maybeMoveBuild( expr_node2 ) ),
     673                flag
     674        );
    588675} // build_and_or
    589676
    590 Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
    591         list< Expression * > args;
     677ast::Expr * build_unary_val( const CodeLocation & location,
     678                OperKinds op,
     679                ExpressionNode * expr_node ) {
     680        std::vector<ast::ptr<ast::Expr>> args;
    592681        args.push_back( maybeMoveBuild( expr_node ) );
    593         return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     682        return new ast::UntypedExpr( location,
     683                new ast::NameExpr( location, OperName[ (int)op ] ),
     684                std::move( args )
     685        );
    594686} // build_unary_val
    595687
    596 Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
    597         list< Expression * > args;
     688ast::Expr * build_binary_val( const CodeLocation & location,
     689                OperKinds op,
     690                ExpressionNode * expr_node1,
     691                ExpressionNode * expr_node2 ) {
     692        std::vector<ast::ptr<ast::Expr>> args;
    598693        args.push_back( maybeMoveBuild( expr_node1 ) );
    599694        args.push_back( maybeMoveBuild( expr_node2 ) );
    600         return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     695        return new ast::UntypedExpr( location,
     696                new ast::NameExpr( location, OperName[ (int)op ] ),
     697                std::move( args )
     698        );
    601699} // build_binary_val
    602700
    603 Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
    604         list< Expression * > args;
    605         args.push_back( maybeMoveBuild( expr_node1 ) );
    606         args.push_back( maybeMoveBuild( expr_node2 ) );
    607         return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     701ast::Expr * build_binary_ptr( const CodeLocation & location,
     702                OperKinds op,
     703                ExpressionNode * expr_node1,
     704                ExpressionNode * expr_node2 ) {
     705        return build_binary_val( location, op, expr_node1, expr_node2 );
    608706} // build_binary_ptr
    609707
    610 Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
    611         return new ConditionalExpr( notZeroExpr( maybeMoveBuild( expr_node1 ) ), maybeMoveBuild( expr_node2 ), maybeMoveBuild( expr_node3 ) );
     708ast::Expr * build_cond( const CodeLocation & location,
     709                ExpressionNode * expr_node1,
     710                ExpressionNode * expr_node2,
     711                ExpressionNode * expr_node3 ) {
     712        return new ast::ConditionalExpr( location,
     713                notZeroExpr( maybeMoveBuild( expr_node1 ) ),
     714                maybeMoveBuild( expr_node2 ),
     715                maybeMoveBuild( expr_node3 )
     716        );
    612717} // build_cond
    613718
    614 Expression * build_tuple( ExpressionNode * expr_node ) {
    615         list< Expression * > exprs;
     719ast::Expr * build_tuple( const CodeLocation & location,
     720                ExpressionNode * expr_node ) {
     721        std::vector<ast::ptr<ast::Expr>> exprs;
    616722        buildMoveList( expr_node, exprs );
    617         return new UntypedTupleExpr( exprs );;
     723        return new ast::UntypedTupleExpr( location, std::move( exprs ) );
    618724} // build_tuple
    619725
    620 Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
    621         list< Expression * > args;
     726ast::Expr * build_func( const CodeLocation & location,
     727                ExpressionNode * function,
     728                ExpressionNode * expr_node ) {
     729        std::vector<ast::ptr<ast::Expr>> args;
    622730        buildMoveList( expr_node, args );
    623         return new UntypedExpr( maybeMoveBuild( function ), args );
     731        return new ast::UntypedExpr( location,
     732                maybeMoveBuild( function ),
     733                std::move( args )
     734        );
    624735} // build_func
    625736
    626 Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
    627         Declaration * newDecl = maybeBuild( decl_node ); // compound literal type
    628         if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
    629                 return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild( kids ) );
     737ast::Expr * build_compoundLiteral( const CodeLocation & location,
     738                DeclarationNode * decl_node,
     739                InitializerNode * kids ) {
     740        // compound literal type
     741        ast::Decl * newDecl = maybeBuild( decl_node );
     742        // non-sue compound-literal type
     743        if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
     744                return new ast::CompoundLiteralExpr( location,
     745                        newDeclWithType->get_type(),
     746                        maybeMoveBuild( kids ) );
    630747        // these types do not have associated type information
    631         } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
    632                 if ( newDeclStructDecl->has_body() ) {
    633                         return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild( kids ) );
     748        } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) {
     749                if ( newDeclStructDecl->body ) {
     750                        return new ast::CompoundLiteralExpr( location,
     751                                new ast::StructInstType( newDeclStructDecl ),
     752                                maybeMoveBuild( kids ) );
    634753                } else {
    635                         return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild( kids ) );
    636                 } // if
    637         } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
    638                 if ( newDeclUnionDecl->has_body() ) {
    639                         return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild( kids ) );
     754                        return new ast::CompoundLiteralExpr( location,
     755                                new ast::StructInstType( newDeclStructDecl->name ),
     756                                maybeMoveBuild( kids ) );
     757                } // if
     758        } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl )  ) {
     759                if ( newDeclUnionDecl->body ) {
     760                        return new ast::CompoundLiteralExpr( location,
     761                                new ast::UnionInstType( newDeclUnionDecl ),
     762                                maybeMoveBuild( kids ) );
    640763                } else {
    641                         return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild( kids ) );
    642                 } // if
    643         } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
    644                 if ( newDeclEnumDecl->has_body() ) {
    645                         return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild( kids ) );
     764                        return new ast::CompoundLiteralExpr( location,
     765                                new ast::UnionInstType( newDeclUnionDecl->name ),
     766                                maybeMoveBuild( kids ) );
     767                } // if
     768        } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl )  ) {
     769                if ( newDeclEnumDecl->body ) {
     770                        return new ast::CompoundLiteralExpr( location,
     771                                new ast::EnumInstType( newDeclEnumDecl ),
     772                                maybeMoveBuild( kids ) );
    646773                } else {
    647                         return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild( kids ) );
     774                        return new ast::CompoundLiteralExpr( location,
     775                                new ast::EnumInstType( newDeclEnumDecl->name ),
     776                                maybeMoveBuild( kids ) );
    648777                } // if
    649778        } else {
Note: See TracChangeset for help on using the changeset viewer.