Changes in src/ResolvExpr/PtrsCastable.cc [3c89751:00ac42e]
- File:
-
- 1 edited
-
src/ResolvExpr/PtrsCastable.cc (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/PtrsCastable.cc
r3c89751 r00ac42e 14 14 // 15 15 16 #include "AST/Decl.hpp"17 #include "AST/Pass.hpp"18 #include "AST/Type.hpp"19 #include "AST/TypeEnvironment.hpp"20 16 #include "Common/PassVisitor.h" 21 17 #include "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment … … 27 23 28 24 namespace ResolvExpr { 29 struct PtrsCastable _old: public WithShortCircuiting {25 struct PtrsCastable : public WithShortCircuiting { 30 26 public: 31 PtrsCastable _old( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );27 PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ); 32 28 33 29 int get_result() const { return result; } … … 90 86 return objectCast( src, env, indexer ); 91 87 } else { 92 PassVisitor<PtrsCastable _old> ptrs( dest, env, indexer );88 PassVisitor<PtrsCastable> ptrs( dest, env, indexer ); 93 89 src->accept( ptrs ); 94 90 return ptrs.pass.get_result(); … … 96 92 } 97 93 98 PtrsCastable _old::PtrsCastable_old( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer )94 PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) 99 95 : dest( dest ), result( 0 ), env( env ), indexer( indexer ) { 100 96 } 101 97 102 void PtrsCastable _old::postvisit( VoidType * ) {98 void PtrsCastable::postvisit( VoidType * ) { 103 99 result = objectCast( dest, env, indexer ); 104 100 } 105 101 106 void PtrsCastable _old::postvisit( BasicType * ) {102 void PtrsCastable::postvisit( BasicType * ) { 107 103 result = objectCast( dest, env, indexer ); 108 104 } 109 105 110 void PtrsCastable _old::postvisit( PointerType * ) {106 void PtrsCastable::postvisit( PointerType * ) { 111 107 result = objectCast( dest, env, indexer ); 112 108 } 113 109 114 void PtrsCastable _old::postvisit( ArrayType * ) {110 void PtrsCastable::postvisit( ArrayType * ) { 115 111 result = objectCast( dest, env, indexer ); 116 112 } 117 113 118 void PtrsCastable _old::postvisit( FunctionType * ) {114 void PtrsCastable::postvisit( FunctionType * ) { 119 115 // result = -1; 120 116 result = functionCast( dest, env, indexer ); 121 117 } 122 118 123 void PtrsCastable _old::postvisit( StructInstType * ) {119 void PtrsCastable::postvisit( StructInstType * ) { 124 120 result = objectCast( dest, env, indexer ); 125 121 } 126 122 127 void PtrsCastable _old::postvisit( UnionInstType * ) {123 void PtrsCastable::postvisit( UnionInstType * ) { 128 124 result = objectCast( dest, env, indexer ); 129 125 } 130 126 131 void PtrsCastable _old::postvisit( EnumInstType * ) {127 void PtrsCastable::postvisit( EnumInstType * ) { 132 128 if ( dynamic_cast< EnumInstType* >( dest ) ) { 133 129 result = 1; … … 143 139 } 144 140 145 void PtrsCastable _old::postvisit( TraitInstType * ) {}141 void PtrsCastable::postvisit( TraitInstType * ) {} 146 142 147 void PtrsCastable _old::postvisit(TypeInstType *inst) {143 void PtrsCastable::postvisit(TypeInstType *inst) { 148 144 //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1; 149 145 result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1; 150 146 } 151 147 152 void PtrsCastable _old::postvisit( TupleType * ) {148 void PtrsCastable::postvisit( TupleType * ) { 153 149 result = objectCast( dest, env, indexer ); 154 150 } 155 151 156 void PtrsCastable _old::postvisit( VarArgsType * ) {152 void PtrsCastable::postvisit( VarArgsType * ) { 157 153 result = objectCast( dest, env, indexer ); 158 154 } 159 155 160 void PtrsCastable _old::postvisit( ZeroType * ) {156 void PtrsCastable::postvisit( ZeroType * ) { 161 157 result = objectCast( dest, env, indexer ); 162 158 } 163 159 164 void PtrsCastable _old::postvisit( OneType * ) {160 void PtrsCastable::postvisit( OneType * ) { 165 161 result = objectCast( dest, env, indexer ); 166 162 } 167 168 namespace {169 // can this type be cast to an object (1 for yes, -1 for no)170 int objectCast(171 const ast::Type * src, const ast::TypeEnvironment & env, const ast::SymbolTable & symtab172 ) {173 if ( dynamic_cast< const ast::FunctionType * >( src ) ) {174 return -1;175 } else if ( auto inst = dynamic_cast< const ast::TypeInstType * >( src ) ) {176 if ( const ast::NamedTypeDecl * named = symtab.lookupType( inst->name ) ) {177 if ( auto tyDecl = dynamic_cast< const ast::TypeDecl * >( named ) ) {178 if ( tyDecl->kind == ast::TypeVar::Ftype ) {179 return -1;180 }181 }182 } else if ( const ast::EqvClass * eqvClass = env.lookup( inst->name ) ) {183 if ( eqvClass->data.kind == ast::TypeVar::Ftype ) {184 return -1;185 }186 }187 }188 189 return 1;190 }191 192 // can this type be cast to a function (inverse of objectCast)193 int functionCast(194 const ast::Type * src, const ast::TypeEnvironment & env, const ast::SymbolTable & symtab195 ) {196 return -1 * objectCast( src, env, symtab );197 }198 199 class PtrsCastable_new : public ast::WithShortCircuiting {200 const ast::Type * dst;201 const ast::TypeEnvironment & env;202 const ast::SymbolTable & symtab;203 public:204 int result;205 206 PtrsCastable_new(207 const ast::Type * d, const ast::TypeEnvironment & e, const ast::SymbolTable & syms )208 : dst( d ), env( e ), symtab( syms ), result( 0 ) {}209 210 void previsit( const ast::Type * ) { visit_children = false; }211 212 void postvisit( const ast::VoidType * ) {213 result = objectCast( dst, env, symtab );214 }215 216 void postvisit( const ast::BasicType * ) {217 result = objectCast( dst, env, symtab );218 }219 220 void postvisit( const ast::PointerType * ) {221 result = objectCast( dst, env, symtab );222 }223 224 void postvisit( const ast::ArrayType * ) {225 result = objectCast( dst, env, symtab );226 }227 228 void postvisit( const ast::FunctionType * ) {229 result = functionCast( dst, env, symtab );230 }231 232 void postvisit( const ast::StructInstType * ) {233 result = objectCast( dst, env, symtab );234 }235 236 void postvisit( const ast::UnionInstType * ) {237 result = objectCast( dst, env, symtab );238 }239 240 void postvisit( const ast::EnumInstType * ) {241 if ( dynamic_cast< const ast::EnumInstType * >( dst ) ) {242 result = 1;243 } else if ( auto bt = dynamic_cast< const ast::BasicType * >( dst ) ) {244 if ( bt->kind == ast::BasicType::SignedInt ) {245 result = 0;246 } else {247 result = 1;248 }249 } else {250 result = objectCast( dst, env, symtab );251 }252 }253 254 void postvisit( const ast::TraitInstType * ) {}255 256 void postvisit( const ast::TypeInstType * inst ) {257 // check trait and destination type are both object or both function258 result = objectCast( inst, env, symtab ) == objectCast( dst, env, symtab ) ? 1 : -1;259 }260 261 void postvisit( const ast::TupleType * ) {262 result = objectCast( dst, env, symtab );263 }264 265 void postvisit( const ast::VarArgsType * ) {266 result = objectCast( dst, env, symtab );267 }268 269 void postvisit( const ast::ZeroType * ) {270 result = objectCast( dst, env, symtab );271 }272 273 void postvisit( const ast::OneType * ) {274 result = objectCast( dst, env, symtab );275 }276 277 };278 } // anonymous namespace279 280 int ptrsCastable(281 const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab,282 const ast::TypeEnvironment & env283 ) {284 if ( auto inst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {285 if ( const ast::EqvClass * eqvClass = env.lookup( inst->name ) ) {286 return ptrsAssignable( src, eqvClass->bound, env );287 }288 }289 290 if ( dynamic_cast< const ast::VoidType * >( dst ) ) {291 return objectCast( src, env, symtab );292 } else {293 ast::Pass< PtrsCastable_new > ptrs{ dst, env, symtab };294 src->accept( ptrs );295 return ptrs.pass.result;296 }297 }298 299 163 } // namespace ResolvExpr 300 164
Note:
See TracChangeset
for help on using the changeset viewer.