Ignore:
Timestamp:
May 14, 2015, 1:44:55 PM (11 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
4bf5298
Parents:
d4778a6
Message:

add inline and attribute qualifiers, cfa.y comment formatting, fix error message in isIntegralType

Location:
translator/ResolvExpr
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • translator/ResolvExpr/CastCost.cc

    rd4778a6 rc11e31c  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: CastCost.cc,v 1.11 2005/08/29 20:14:15 rcbilson Exp $
    5  *
    6  */
    7 
    81#include "typeops.h"
    92#include "Cost.h"
     
    158
    169namespace ResolvExpr {
     10    class CastCost : public ConversionCost {
     11      public:
     12        CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
     13 
     14        virtual void visit( BasicType *basicType );
     15        virtual void visit( PointerType *pointerType );
     16    };
    1717
    18 class CastCost : public ConversionCost
    19 {
    20 public:
    21   CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
    22  
    23   virtual void visit(BasicType *basicType);
    24   virtual void visit(PointerType *pointerType);
    25 };
     18    Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
     19        if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
     20            EqvClass eqvClass;
     21            NamedTypeDecl *namedType;
     22            if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
     23                return castCost( src, eqvClass.type, indexer, env );
     24            } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
     25                TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
     26                // all typedefs should be gone by this point
     27                assert( type );
     28                if ( type->get_base() ) {
     29                    return castCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 );
     30                } // if
     31            } // if
     32        } // if
     33        if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
     34            return Cost( 0, 0, 0 );
     35        } else if ( dynamic_cast< VoidType* >( dest ) ) {
     36            return Cost( 0, 0, 1 );
     37        } else {
     38            CastCost converter( dest, indexer, env );
     39            src->accept( converter );
     40            if ( converter.get_cost() == Cost::infinity ) {
     41                return Cost::infinity;
     42            } else {
     43                return converter.get_cost() + Cost( 0, 0, 0 );
     44            } // if
     45        } // if
     46    }
    2647
    27 Cost
    28 castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
    29 {
    30 //  std::cout << "casting" << std::endl;
    31 //  src->print( std::cout, 8 );
    32 //  std::cout << std::endl << "to" << std::endl;
    33 //  dest->print( std::cout, 8 );
    34   if( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
    35     EqvClass eqvClass;
    36     NamedTypeDecl *namedType;
    37     if( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
    38       return castCost( src, eqvClass.type, indexer, env );
    39     } else if( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
    40       TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
    41       // all typedefs should be gone by this point
    42       assert( type );
    43       if( type->get_base() ) {
    44         return castCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 );
    45       }
     48    CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
     49        : ConversionCost( dest, indexer, env ) {
    4650    }
    47   }
    48   if( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
    49 //    std::cout << "types are compatible" << std::endl;
    50     return Cost( 0, 0, 0 );
    51   } else if( dynamic_cast< VoidType* >( dest ) ) {
    52 //    std::cout << "destination is void" << std::endl;
    53     return Cost( 0, 0, 1 );
    54   } else {
    55     CastCost converter( dest, indexer, env );
    56     src->accept( converter );
    57 //    std::cout << "cost is " << converter.get_cost() << std::endl;
    58     if( converter.get_cost() == Cost::infinity ) {
    59       return Cost::infinity;
    60     } else {
    61       return converter.get_cost() + Cost( 0, 0, 0 );
     51
     52    void CastCost::visit( BasicType *basicType ) {
     53        if ( dynamic_cast< PointerType* >( dest ) ) {
     54            cost = Cost( 1, 0, 0 );
     55        } else {
     56            ConversionCost::visit( basicType );
     57        } // if
    6258    }
    63   }
    64 }
    6559
    66 CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
    67   : ConversionCost( dest, indexer, env )
    68 {
    69 }
    70 
    71 void
    72 CastCost::visit(BasicType *basicType)
    73 {
    74   if( dynamic_cast< PointerType* >( dest ) ) {
    75     cost = Cost( 1, 0, 0 );
    76   } else {
    77     ConversionCost::visit( basicType );
    78   }
    79 }
    80 
    81 void
    82 CastCost::visit(PointerType *pointerType)
    83 {
    84   if( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
    85     if( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
    86       cost = Cost( 0, 0, 1 );
    87     } else if( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
    88       if( destAsBasic->isInteger() ) {
    89         cost = Cost( 1, 0, 0 );
    90       }
    91     } else {
    92       TypeEnvironment newEnv( env );
    93       newEnv.add( pointerType->get_forall() );
    94       newEnv.add( pointerType->get_base()->get_forall() );
    95       int assignResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
    96       if( assignResult > 0 ) {
    97         cost = Cost( 0, 0, 1 );
    98       } else if( assignResult < 0 ) {
    99         cost = Cost( 1, 0, 0 );
    100       }
     60    void CastCost::visit( PointerType *pointerType ) {
     61        if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
     62            if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
     63                cost = Cost( 0, 0, 1 );
     64            } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
     65                if ( destAsBasic->isInteger() ) {
     66                    cost = Cost( 1, 0, 0 );
     67                }
     68            } else {
     69                TypeEnvironment newEnv( env );
     70                newEnv.add( pointerType->get_forall() );
     71                newEnv.add( pointerType->get_base()->get_forall() );
     72                int assignResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
     73                if ( assignResult > 0 ) {
     74                    cost = Cost( 0, 0, 1 );
     75                } else if ( assignResult < 0 ) {
     76                    cost = Cost( 1, 0, 0 );
     77                } // if
     78            } // if
     79        } // if
    10180    }
    102   }
    103 }
    104 
    10581} // namespace ResolvExpr
  • translator/ResolvExpr/PtrsAssignable.cc

    rd4778a6 rc11e31c  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: PtrsAssignable.cc,v 1.3 2005/08/29 20:14:16 rcbilson Exp $
    5  *
    6  */
    7 
    81#include "typeops.h"
    92#include "SynTree/Type.h"
     
    136
    147namespace ResolvExpr {
     8    class PtrsAssignable : public Visitor {
     9      public:
     10        PtrsAssignable( Type *dest, const TypeEnvironment &env );
     11 
     12        int get_result() const { return result; }
    1513
    16 class PtrsAssignable : public Visitor
    17 {
    18 public:
    19   PtrsAssignable( Type *dest, const TypeEnvironment &env );
    20  
    21   int get_result() const { return result; }
     14        virtual void visit(VoidType *voidType);
     15        virtual void visit(BasicType *basicType);
     16        virtual void visit(PointerType *pointerType);
     17        virtual void visit(ArrayType *arrayType);
     18        virtual void visit(FunctionType *functionType);
     19        virtual void visit(StructInstType *inst);
     20        virtual void visit(UnionInstType *inst);
     21        virtual void visit(EnumInstType *inst);
     22        virtual void visit(ContextInstType *inst);
     23        virtual void visit(TypeInstType *inst);
     24        virtual void visit(TupleType *tupleType);
     25      private:
     26        Type *dest;
     27        int result;
     28        const TypeEnvironment &env;
     29    };
    2230
    23   virtual void visit(VoidType *voidType);
    24   virtual void visit(BasicType *basicType);
    25   virtual void visit(PointerType *pointerType);
    26   virtual void visit(ArrayType *arrayType);
    27   virtual void visit(FunctionType *functionType);
    28   virtual void visit(StructInstType *inst);
    29   virtual void visit(UnionInstType *inst);
    30   virtual void visit(EnumInstType *inst);
    31   virtual void visit(ContextInstType *inst);
    32   virtual void visit(TypeInstType *inst);
    33   virtual void visit(TupleType *tupleType);
     31    int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) {
     32        if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
     33            EqvClass eqvClass;
     34            if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
     35                return ptrsAssignable( src, eqvClass.type, env );
     36            }
     37        }
     38        if ( dynamic_cast< VoidType* >( dest ) ) {
     39            return 1;
     40        } else {
     41            PtrsAssignable ptrs( dest, env );
     42            src->accept( ptrs );
     43            return ptrs.get_result();
     44        }
     45    }
    3446
    35 private:
    36   Type *dest;
    37   int result;
    38   const TypeEnvironment &env;
    39 };
     47    PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {
     48    }
    4049
    41 int
    42 ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env )
    43 {
    44   if( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
    45     EqvClass eqvClass;
    46     if( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
    47       return ptrsAssignable( src, eqvClass.type, env );
     50    void PtrsAssignable::visit(VoidType *voidType) {
     51        if ( dynamic_cast< FunctionType* >( dest ) ) {
     52            result = 0;
     53        } else {
     54            result = -1;
     55        }
    4856    }
    49   }
    50   if( dynamic_cast< VoidType* >( dest ) ) {
    51     return 1;
    52   } else {
    53     PtrsAssignable ptrs( dest, env );
    54     src->accept( ptrs );
    55     return ptrs.get_result();
    56   }
    57 }
    5857
    59 PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env )
    60   : dest( dest ), result( 0 ), env( env )
    61 {
    62 }
     58    void PtrsAssignable::visit( BasicType *basicType ) {
     59    }
    6360
    64 void
    65 PtrsAssignable::visit(VoidType *voidType)
    66 {
    67   if( dynamic_cast< FunctionType* >( dest ) ) {
    68     result = 0;
    69   } else {
    70     result = -1;
    71   }
    72 }
     61    void PtrsAssignable::visit( PointerType *pointerType ) {
     62    }
    7363
    74 void
    75 PtrsAssignable::visit(BasicType *basicType)
    76 {
    77 }
     64    void PtrsAssignable::visit( ArrayType *arrayType ) {
     65    }
    7866
    79 void
    80 PtrsAssignable::visit(PointerType *pointerType)
    81 {
    82 }
     67    void PtrsAssignable::visit( FunctionType *functionType ) {
     68        result = -1;
     69    }
    8370
    84 void
    85 PtrsAssignable::visit(ArrayType *arrayType)
    86 {
    87 }
     71    void PtrsAssignable::visit( StructInstType *inst ) {
     72        // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong
     73    }
    8874
    89 void
    90 PtrsAssignable::visit(FunctionType *functionType)
    91 {
    92   result = -1;
    93 }
     75    void PtrsAssignable::visit( UnionInstType *inst ) {
     76        // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong
     77    }
    9478
    95 void
    96 PtrsAssignable::visit(StructInstType *inst)
    97 {
    98   // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong
    99 }
     79    void PtrsAssignable::visit( EnumInstType *inst ) {
     80        if ( dynamic_cast< EnumInstType* >( inst ) ) {
     81            result = 1;
     82        } else if ( BasicType *bt = dynamic_cast< BasicType* >( inst ) ) {
     83            result = bt->get_kind() == BasicType::SignedInt;
     84        }
     85    }
    10086
    101 void
    102 PtrsAssignable::visit(UnionInstType *inst)
    103 {
    104   // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong
    105 }
     87    void PtrsAssignable::visit( ContextInstType *inst ) {
     88        // I definitely don't think we should be doing anything here
     89    }
    10690
    107 void
    108 PtrsAssignable::visit(EnumInstType *inst)
    109 {
    110   if( dynamic_cast< EnumInstType* >( inst ) ) {
    111     result = 1;
    112   } else if( BasicType *bt = dynamic_cast< BasicType* >( inst ) ) {
    113     result = bt->get_kind() == BasicType::SignedInt;
    114   }
    115 }
     91    void PtrsAssignable::visit( TypeInstType *inst ) {
     92        EqvClass eqvClass;
     93        if ( env.lookup( inst->get_name(), eqvClass ) ) {
     94            result = ptrsAssignable( eqvClass.type, dest, env );
     95        } else {
     96            result = 0;
     97        }
     98    }
    11699
    117 void
    118 PtrsAssignable::visit(ContextInstType *inst)
    119 {
    120   // I definitely don't think we should be doing anything here
    121 }
    122 
    123 void
    124 PtrsAssignable::visit(TypeInstType *inst)
    125 {
    126   EqvClass eqvClass;
    127   if( env.lookup( inst->get_name(), eqvClass ) ) {
    128     result = ptrsAssignable( eqvClass.type, dest, env );
    129   } else {
    130     result = 0;
    131   }
    132 }
    133 
    134 void
    135 PtrsAssignable::visit(TupleType *tupleType)
    136 {
     100    void PtrsAssignable::visit( TupleType *tupleType ) {
    137101///  // This code doesn't belong here, but it might be useful somewhere else
    138 ///   if( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) {
     102///   if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) {
    139103///     int ret = 0;
    140104///     std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin();
     
    142106///     while( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) {
    143107///       int assignResult = ptrsAssignable( *srcIt++, *destIt++ );
    144 ///       if( assignResult == 0 ) {
     108///       if ( assignResult == 0 ) {
    145109///         result = assignResult;
    146110///         return;
     
    151115///       }
    152116///     }
    153 ///     if( srcIt == tupleType->get_types().end() && destIt == destAsTuple->get_types().end() ) {
     117///     if ( srcIt == tupleType->get_types().end() && destIt == destAsTuple->get_types().end() ) {
    154118///       result = ret;
    155119///     } else {
     
    157121///     }
    158122///   }
    159 }
    160 
     123    }
    161124} // namespace ResolvExpr
  • translator/ResolvExpr/Resolver.cc

    rd4778a6 rc11e31c  
    9898                return bt->isInteger();
    9999            } else {
    100                 return true;
     100                return false;
    101101            } // if
    102102        }
     
    121121                if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) {
    122122                    if ( newExpr ) {
    123                         throw SemanticError( "Too many interpretations for switch control expression", untyped );
     123                        throw SemanticError( "Too many interpretations for case control expression", untyped );
    124124                    } else {
    125125                        newExpr = i->expr->clone();
     
    128128                } // if
    129129            } // for
    130             if ( !newExpr ) {
    131                 throw SemanticError( "Too many interpretations for switch control expression", untyped );
     130            if ( ! newExpr ) {
     131                throw SemanticError( "No interpretations for case control expression", untyped );
    132132            } // if
    133133            finishExpr( newExpr, *newEnv );
Note: See TracChangeset for help on using the changeset viewer.