Changeset 6b224a52 for src/SynTree


Ignore:
Timestamp:
Aug 25, 2017, 12:11:53 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
bf7b9da7
Parents:
135b431 (diff), f676b84 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/SynTree
Files:
1 added
16 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/AddressExpr.cc

    r135b431 r6b224a52  
    2121#include "Type.h"            // for PointerType, Type, Type::Qualifiers
    2222
     23// Address expressions are typed based on the following inference rules:
     24//    E : lvalue T  &..& (n references)
     25//   &E :        T *&..& (n references)
     26//
     27//    E : T  &..&        (m references)
     28//   &E : T *&..&        (m-1 references)
     29//
     30// That is, lvalues becomes
     31
     32namespace {
     33        Type * addrType( Type * type ) {
     34                if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) {
     35                        return new ReferenceType( refType->get_qualifiers(), addrType( refType->get_base() ) );
     36                } else {
     37                        return new PointerType( Type::Qualifiers(), type->clone() );
     38                }
     39        }
     40}
     41
    2342AddressExpr::AddressExpr( Expression *arg, Expression *_aname ) : Expression( _aname ), arg( arg ) {
    2443        if ( arg->has_result() ) {
    25                 set_result( new PointerType( Type::Qualifiers(), arg->get_result()->clone() ) );
     44                if ( arg->get_result()->get_lvalue() ) {
     45                        // lvalue, retains all layers of reference and gains a pointer inside the references
     46                        set_result( addrType( arg->get_result() ) );
     47                } else {
     48                        // taking address of non-lvalue -- must be a reference, loses one layer of reference
     49                        ReferenceType * refType = safe_dynamic_cast< ReferenceType * >( arg->get_result() );
     50                        set_result( addrType( refType->get_base() ) );
     51                }
     52                // result of & is never an lvalue
     53                get_result()->set_lvalue( false );
    2654        }
    2755}
  • src/SynTree/ApplicationExpr.cc

    r135b431 r6b224a52  
    4949}
    5050
    51 ApplicationExpr::ApplicationExpr( Expression *funcExpr, const std::list< Expression * > & argList ) : function( funcExpr ), args( argList ) {
     51ApplicationExpr::ApplicationExpr( Expression *funcExpr, const std::list<Expression *> & args ) : function( funcExpr ), args( args ) {
    5252        PointerType *pointer = safe_dynamic_cast< PointerType* >( funcExpr->get_result() );
    5353        FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() );
  • src/SynTree/Declaration.h

    r135b431 r6b224a52  
    109109        virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0;
    110110
    111         virtual Type *get_type() const = 0;
     111        virtual Type * get_type() const = 0;
    112112        virtual void set_type(Type *) = 0;
    113113
     
    128128        virtual ~ObjectDecl();
    129129
    130         virtual Type *get_type() const { return type; }
     130        virtual Type * get_type() const { return type; }
    131131        virtual void set_type(Type *newType) { type = newType; }
    132132
     
    155155        virtual ~FunctionDecl();
    156156
    157         Type *get_type() const;
     157        Type * get_type() const;
    158158        virtual void set_type(Type *);
    159159
    160         FunctionType *get_functionType() const { return type; }
     160        FunctionType * get_functionType() const { return type; }
    161161        void set_functionType( FunctionType *newValue ) { type = newValue; }
    162162        CompoundStmt *get_statements() const { return statements; }
  • src/SynTree/Expression.cc

    r135b431 r6b224a52  
    3131#include "TypeSubstitution.h"        // for TypeSubstitution
    3232
     33#include "GenPoly/Lvalue.h"
    3334
    3435Expression::Expression( Expression *_aname ) : result( 0 ), env( 0 ), argName( _aname ) {}
     
    8990}
    9091
     92VariableExpr * VariableExpr::functionPointer( FunctionDecl * func ) {
     93        VariableExpr * funcExpr = new VariableExpr( func );
     94        funcExpr->set_result( new PointerType( Type::Qualifiers(), funcExpr->get_result() ) );
     95        return funcExpr;
     96}
     97
    9198void VariableExpr::print( std::ostream &os, int indent ) const {
    9299        os << "Variable Expression: ";
     
    149156
    150157void AlignofExpr::print( std::ostream &os, int indent) const {
    151         os << std::string( indent, ' ' ) << "Alignof Expression on: ";
     158        os << "Alignof Expression on: ";
    152159
    153160        if (isType)
     
    258265
    259266void AttrExpr::print( std::ostream &os, int indent) const {
    260         os << std::string( indent, ' ' ) << "Attr ";
     267        os << "Attr ";
    261268        attr->print( os, indent + 2 );
    262269        if ( isType || expr ) {
     
    357364namespace {
    358365        TypeSubstitution makeSub( Type * t ) {
    359                 if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) {
     366                if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( t ) ) {
     367                        return makeSub( refType->get_base() );
     368                } else if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) {
    360369                        return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() );
    361370                } else if ( UnionInstType * aggInst = dynamic_cast< UnionInstType * >( t ) ) {
     
    422431        if ( Type * type = expr->get_result() ) {
    423432                Type * base = InitTweak::getPointerBase( type );
    424                 if ( ! base ) {
    425                         std::cerr << type << std::endl;
     433                assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() );
     434                ret->set_result( base->clone() );
     435                if ( GenPoly::referencesPermissable() ) {
     436                        // if references are still allowed in the AST, dereference returns a reference
     437                        ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) );
     438                } else {
     439                        // references have been removed, in which case dereference returns an lvalue of the base type.
     440                        ret->get_result()->set_lvalue( true );
    426441                }
    427                 assertf( base, "expected pointer type in dereference\n" );
    428                 ret->set_result( maybeClone( base ) );
    429442        }
    430443        return ret;
     
    490503
    491504void LogicalExpr::print( std::ostream &os, int indent )const {
    492         os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
     505        os << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
    493506        arg1->print(os);
    494507        os << " and ";
     
    595608CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) {
    596609        assert( type && initializer );
     610        type->set_lvalue( true );
    597611        set_result( type );
    598612}
  • src/SynTree/Expression.h

    r135b431 r6b224a52  
    284284        void set_var( DeclarationWithType * newValue ) { var = newValue; }
    285285
     286        static VariableExpr * functionPointer( FunctionDecl * decl );
     287
    286288        virtual VariableExpr * clone() const { return new VariableExpr( * this ); }
    287289        virtual void accept( Visitor & v ) { v.visit( this ); }
  • src/SynTree/Mutator.cc

    r135b431 r6b224a52  
    502502}
    503503
    504 Type * Mutator::mutate( FunctionType *functionType ) {
     504Type * Mutator::mutate( ReferenceType * refType ) {
     505        mutateAll( refType->get_forall(), *this );
     506        refType->set_base( maybeMutate( refType->get_base(), *this ) );
     507        return refType;
     508}
     509
     510Type * Mutator::mutate( FunctionType * functionType ) {
    505511        mutateAll( functionType->get_forall(), *this );
    506512        mutateAll( functionType->get_returnVals(), *this );
  • src/SynTree/Mutator.h

    r135b431 r6b224a52  
    9393        virtual Type* mutate( PointerType *pointerType );
    9494        virtual Type* mutate( ArrayType *arrayType );
     95        virtual Type* mutate( ReferenceType *refType );
    9596        virtual Type* mutate( FunctionType *functionType );
    9697        virtual Type* mutate( StructInstType *aggregateUseType );
  • src/SynTree/SynTree.h

    r135b431 r6b224a52  
    102102class PointerType;
    103103class ArrayType;
     104class ReferenceType;
    104105class FunctionType;
    105106class ReferenceToType;
  • src/SynTree/TupleExpr.cc

    r135b431 r6b224a52  
    6767        assertf( type->size() > index, "TupleIndexExpr index out of bounds: tuple size %d, requested index %d in expr %s", type->size(), index, toString( tuple ).c_str() );
    6868        set_result( (*std::next( type->get_types().begin(), index ))->clone() );
    69         get_result()->set_lvalue( type->get_lvalue() );
     69        // like MemberExpr, TupleIndexExpr is always an lvalue
     70        get_result()->set_lvalue( true );
    7071}
    7172
  • src/SynTree/Type.cc

    r135b431 r6b224a52  
    6464const char * Type::QualifiersNames[] = { "const", "restrict", "volatile", "lvalue", "mutex", "_Atomic" };
    6565
    66 Type *Type::stripDeclarator() {
     66Type * Type::stripDeclarator() {
    6767        Type * type = this;
    6868        while ( Type * at = InitTweak::getPointerBase( type ) ) {
     
    7171        return type;
    7272}
     73
     74Type * Type::stripReferences() {
     75        Type * type = this;
     76        while ( ReferenceType * ref = dynamic_cast<ReferenceType *>( type ) ) {
     77                type = ref->get_base();
     78        }
     79        return type;
     80}
     81
     82int Type::referenceDepth() const { return 0; }
    7383
    7484void Type::print( std::ostream &os, int indent ) const {
  • src/SynTree/Type.h

    r135b431 r6b224a52  
    168168
    169169        /// return type without outer pointers and arrays
    170         Type *stripDeclarator();
     170        Type * stripDeclarator();
     171
     172        /// return type without outer references
     173        Type * stripReferences();
     174
     175        /// return the number of references occuring consecutively on the outermost layer of this type (i.e. do not count references nested within other types)
     176        virtual int referenceDepth() const;
    171177
    172178        virtual bool isComplete() const { return true; }
     
    262268        bool is_array() const { return isStatic || isVarLen || dimension; }
    263269
     270        virtual bool isComplete() const { return ! isVarLen; }
     271
    264272        virtual PointerType *clone() const { return new PointerType( *this ); }
    265273        virtual void accept( Visitor & v ) { v.visit( this ); }
     
    296304};
    297305
     306class ReferenceType : public Type {
     307public:
     308        Type *base;
     309
     310        ReferenceType( const Type::Qualifiers & tq, Type *base, const std::list< Attribute * > & attributes = std::list< Attribute * >() );
     311        ReferenceType( const ReferenceType & );
     312        virtual ~ReferenceType();
     313
     314        Type *get_base() { return base; }
     315        void set_base( Type *newValue ) { base = newValue; }
     316
     317        virtual int referenceDepth() const;
     318
     319        // Since reference types act like value types, their size is the size of the base.
     320        // This makes it simple to cast the empty tuple to a reference type, since casts that increase
     321        // the number of values are disallowed.
     322        virtual unsigned size() const { return base->size(); }
     323
     324        virtual ReferenceType *clone() const { return new ReferenceType( *this ); }
     325        virtual void accept( Visitor & v ) { v.visit( this ); }
     326        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
     327        virtual void print( std::ostream & os, int indent = 0 ) const;
     328};
     329
    298330class FunctionType : public Type {
    299331  public:
  • src/SynTree/TypeExpr.cc

    r135b431 r6b224a52  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // TypeExpr.cc -- 
     7// TypeExpr.cc --
    88//
    99// Author           : Richard C. Bilson
  • src/SynTree/TypeSubstitution.h

    r135b431 r6b224a52  
    117117                                } // if
    118118                        } else {
    119                                 throw SemanticError( "Attempt to provide non-type parameter for type parameter", formal );
     119                                throw SemanticError( toString( "Attempt to provide non-type parameter: ", toString( *actualIt ).c_str(), " for type parameter " ), formal );
    120120                        } // if
    121121                } else {
  • src/SynTree/Visitor.cc

    r135b431 r6b224a52  
    389389void Visitor::visit( PointerType *pointerType ) {
    390390        acceptAll( pointerType->get_forall(), *this );
     391        // xxx - should PointerType visit/mutate dimension?
    391392        maybeAccept( pointerType->get_base(), *this );
    392393}
     
    396397        maybeAccept( arrayType->get_dimension(), *this );
    397398        maybeAccept( arrayType->get_base(), *this );
     399}
     400
     401void Visitor::visit( ReferenceType *refType ) {
     402        acceptAll( refType->get_forall(), *this );
     403        maybeAccept( refType->get_base(), *this );
    398404}
    399405
  • src/SynTree/Visitor.h

    r135b431 r6b224a52  
    9595        virtual void visit( PointerType *pointerType );
    9696        virtual void visit( ArrayType *arrayType );
     97        virtual void visit( ReferenceType *refType );
    9798        virtual void visit( FunctionType *functionType );
    9899        virtual void visit( StructInstType *aggregateUseType );
  • src/SynTree/module.mk

    r135b431 r6b224a52  
    2020       SynTree/PointerType.cc \
    2121       SynTree/ArrayType.cc \
     22       SynTree/ReferenceType.cc \
    2223       SynTree/FunctionType.cc \
    2324       SynTree/ReferenceToType.cc \
Note: See TracChangeset for help on using the changeset viewer.