Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/PtrsCastable.cc

    rc6b4432 r5bf3976  
    2020#include "AST/Type.hpp"
    2121#include "AST/TypeEnvironment.hpp"
     22#include "Common/PassVisitor.h"
    2223#include "ResolvExpr/PtrsAssignable.hpp" // for ptrsAssignable
     24#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
     25#include "SymTab/Indexer.h"              // for Indexer
     26#include "SynTree/Declaration.h"         // for TypeDecl, TypeDecl::Kind::Ftype
     27#include "SynTree/Type.h"                // for TypeInstType, Type, BasicType
     28#include "SynTree/Visitor.h"             // for Visitor
    2329
    2430namespace ResolvExpr {
     31        struct PtrsCastable_old : public WithShortCircuiting  {
     32          public:
     33                PtrsCastable_old( const Type * dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
     34
     35                int get_result() const { return result; }
     36
     37                void previsit( const Type * ) { visit_children = false; }
     38
     39                void postvisit( const VoidType * voidType );
     40                void postvisit( const BasicType * basicType );
     41                void postvisit( const PointerType * pointerType );
     42                void postvisit( const ArrayType * arrayType );
     43                void postvisit( const FunctionType * functionType );
     44                void postvisit( const StructInstType * inst );
     45                void postvisit( const UnionInstType * inst );
     46                void postvisit( const EnumInstType * inst );
     47                void postvisit( const TraitInstType * inst );
     48                void postvisit( const TypeInstType * inst );
     49                void postvisit( const TupleType * tupleType );
     50                void postvisit( const VarArgsType * varArgsType );
     51                void postvisit( const ZeroType * zeroType );
     52                void postvisit( const OneType * oneType );
     53          private:
     54                const Type * dest;
     55                int result;
     56                const TypeEnvironment &env;
     57                const SymTab::Indexer &indexer;
     58        };
     59
     60        namespace {
     61                int objectCast( const Type * src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
     62                        if ( dynamic_cast< const FunctionType* >( src ) ) {
     63                                return -1;
     64                        } else if ( const TypeInstType * typeInst = dynamic_cast< const TypeInstType* >( src ) ) {
     65                                if ( const NamedTypeDecl * ntDecl = indexer.lookupType( typeInst->name ) ) {
     66                                        if ( const TypeDecl * tyDecl = dynamic_cast< const TypeDecl* >( ntDecl ) ) {
     67                                                if ( tyDecl->kind == TypeDecl::Ftype ) {
     68                                                        return -1;
     69                                                } // if
     70                                        } //if
     71                                } else if ( const EqvClass * eqvClass = env.lookup( typeInst->get_name() ) ) {
     72                                        if ( eqvClass->data.kind == TypeDecl::Ftype ) {
     73                                                return -1;
     74                                        } // if
     75                                } // if
     76                        } //if
     77                        return 1;
     78                }
     79                int functionCast( const Type * src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
     80                        return -1 * objectCast( src, env, indexer );  // reverse the sense of objectCast
     81                }
     82        }
     83
     84        int ptrsCastable( const Type * src, const Type * dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
     85                if ( const TypeInstType * destAsTypeInst = dynamic_cast< const TypeInstType* >( dest ) ) {
     86                        if ( const EqvClass * eqvClass = env.lookup( destAsTypeInst->get_name() ) ) {
     87                                // xxx - should this be ptrsCastable?
     88                                return ptrsAssignable( src, eqvClass->type, env );
     89                        } // if
     90                } // if
     91                if ( dynamic_cast< const VoidType* >( dest ) ) {
     92                        return objectCast( src, env, indexer );
     93                } else {
     94                        PassVisitor<PtrsCastable_old> ptrs( dest, env, indexer );
     95                        src->accept( ptrs );
     96                        return ptrs.pass.get_result();
     97                } // if
     98        }
     99
     100        PtrsCastable_old::PtrsCastable_old( const Type * dest, const TypeEnvironment &env, const SymTab::Indexer &indexer )
     101                : dest( dest ), result( 0 ), env( env ), indexer( indexer )     {
     102        }
     103
     104        void PtrsCastable_old::postvisit( const VoidType * ) {
     105                result = objectCast( dest, env, indexer );
     106        }
     107
     108        void PtrsCastable_old::postvisit( const BasicType * ) {
     109                result = objectCast( dest, env, indexer );
     110        }
     111
     112        void PtrsCastable_old::postvisit( const PointerType * ) {
     113                result = objectCast( dest, env, indexer );
     114        }
     115
     116        void PtrsCastable_old::postvisit( const ArrayType * ) {
     117                result = objectCast( dest, env, indexer );
     118        }
     119
     120        void PtrsCastable_old::postvisit( const FunctionType * ) {
     121                // result = -1;
     122                result = functionCast( dest, env, indexer );
     123        }
     124
     125        void PtrsCastable_old::postvisit( const StructInstType * ) {
     126                result = objectCast( dest, env, indexer );
     127        }
     128
     129        void PtrsCastable_old::postvisit( const UnionInstType * ) {
     130                result = objectCast( dest, env, indexer );
     131        }
     132
     133        void PtrsCastable_old::postvisit( const EnumInstType * ) {
     134                if ( dynamic_cast< const EnumInstType * >( dest ) ) {
     135                        result = 1;
     136                } else if ( const BasicType * bt = dynamic_cast< const BasicType * >( dest ) ) {
     137                        if ( bt->kind == BasicType::SignedInt ) {
     138                                result = 0;
     139                        } else {
     140                                result = 1;
     141                        }
     142                } else {
     143                        result = objectCast( dest, env, indexer );
     144                }
     145        }
     146
     147        void PtrsCastable_old::postvisit( const TraitInstType * ) {}
     148
     149        void PtrsCastable_old::postvisit( const TypeInstType *inst ) {
     150                //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1;
     151                result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1;
     152        }
     153
     154        void PtrsCastable_old::postvisit( const TupleType * ) {
     155                result = objectCast( dest, env, indexer );
     156        }
     157
     158        void PtrsCastable_old::postvisit( const VarArgsType * ) {
     159                result = objectCast( dest, env, indexer );
     160        }
     161
     162        void PtrsCastable_old::postvisit( const ZeroType * ) {
     163                result = objectCast( dest, env, indexer );
     164        }
     165
     166        void PtrsCastable_old::postvisit( const OneType * ) {
     167                result = objectCast( dest, env, indexer );
     168        }
    25169
    26170namespace {
Note: See TracChangeset for help on using the changeset viewer.