| [a32b204] | 1 | //
 | 
|---|
 | 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
 | 3 | //
 | 
|---|
 | 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
 | 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
 | 6 | //
 | 
|---|
| [2c57025] | 7 | // PtrsCastable.cc --
 | 
|---|
| [a32b204] | 8 | //
 | 
|---|
 | 9 | // Author           : Richard C. Bilson
 | 
|---|
 | 10 | // Created On       : Sun May 17 11:48:00 2015
 | 
|---|
| [4040425] | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
 | 12 | // Last Modified On : Wed Mar  2 17:36:18 2016
 | 
|---|
 | 13 | // Update Count     : 8
 | 
|---|
| [a32b204] | 14 | //
 | 
|---|
| [51b73452] | 15 | 
 | 
|---|
| [ea6332d] | 16 | #include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
 | 
|---|
 | 17 | #include "SymTab/Indexer.h"              // for Indexer
 | 
|---|
 | 18 | #include "SynTree/Declaration.h"         // for TypeDecl, TypeDecl::Kind::Ftype
 | 
|---|
 | 19 | #include "SynTree/Type.h"                // for TypeInstType, Type, BasicType
 | 
|---|
 | 20 | #include "SynTree/Visitor.h"             // for Visitor
 | 
|---|
 | 21 | #include "typeops.h"                     // for ptrsAssignable
 | 
|---|
| [51b73452] | 22 | 
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | namespace ResolvExpr {
 | 
|---|
| [a32b204] | 25 |         class PtrsCastable : public Visitor {
 | 
|---|
 | 26 |           public:
 | 
|---|
 | 27 |                 PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
 | 
|---|
| [2c57025] | 28 | 
 | 
|---|
| [a32b204] | 29 |                 int get_result() const { return result; }
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 |                 virtual void visit(VoidType *voidType);
 | 
|---|
 | 32 |                 virtual void visit(BasicType *basicType);
 | 
|---|
 | 33 |                 virtual void visit(PointerType *pointerType);
 | 
|---|
 | 34 |                 virtual void visit(ArrayType *arrayType);
 | 
|---|
 | 35 |                 virtual void visit(FunctionType *functionType);
 | 
|---|
 | 36 |                 virtual void visit(StructInstType *inst);
 | 
|---|
 | 37 |                 virtual void visit(UnionInstType *inst);
 | 
|---|
 | 38 |                 virtual void visit(EnumInstType *inst);
 | 
|---|
| [4040425] | 39 |                 virtual void visit(TraitInstType *inst);
 | 
|---|
| [a32b204] | 40 |                 virtual void visit(TypeInstType *inst);
 | 
|---|
 | 41 |                 virtual void visit(TupleType *tupleType);
 | 
|---|
| [44b7088] | 42 |                 virtual void visit(VarArgsType *varArgsType);
 | 
|---|
| [89e6ffc] | 43 |                 virtual void visit(ZeroType *zeroType);
 | 
|---|
 | 44 |                 virtual void visit(OneType *oneType);
 | 
|---|
| [a32b204] | 45 |           private:
 | 
|---|
 | 46 |                 Type *dest;
 | 
|---|
 | 47 |                 int result;
 | 
|---|
 | 48 |                 const TypeEnvironment &env;
 | 
|---|
 | 49 |                 const SymTab::Indexer &indexer;
 | 
|---|
 | 50 |         };
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 |         int objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
 | 
|---|
 | 53 |                 if ( dynamic_cast< FunctionType* >( src ) ) {
 | 
|---|
 | 54 |                         return -1;
 | 
|---|
 | 55 |                 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) {
 | 
|---|
 | 56 |                         EqvClass eqvClass;
 | 
|---|
 | 57 |                         if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) {
 | 
|---|
 | 58 |                                 if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) {
 | 
|---|
 | 59 |                                         if ( tyDecl->get_kind() == TypeDecl::Ftype ) {
 | 
|---|
 | 60 |                                                 return -1;
 | 
|---|
 | 61 |                                         } // if
 | 
|---|
 | 62 |                                 } //if
 | 
|---|
 | 63 |                         } else if ( env.lookup( typeInst->get_name(), eqvClass ) ) {
 | 
|---|
| [2c57025] | 64 |                                 if ( eqvClass.data.kind == TypeDecl::Ftype ) {
 | 
|---|
| [a32b204] | 65 |                                         return -1;
 | 
|---|
 | 66 |                                 } // if
 | 
|---|
 | 67 |                         } // if
 | 
|---|
 | 68 |                 } //if
 | 
|---|
 | 69 |                 return 1;
 | 
|---|
 | 70 |         }
 | 
|---|
| [1d29d46] | 71 |         int functionCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
 | 
|---|
 | 72 |                 return -1 * objectCast( src, env, indexer );  // reverse the sense of objectCast
 | 
|---|
 | 73 |         }
 | 
|---|
| [a32b204] | 74 | 
 | 
|---|
 | 75 |         int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
 | 
|---|
 | 76 |                 if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
 | 
|---|
 | 77 |                         EqvClass eqvClass;
 | 
|---|
 | 78 |                         if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
 | 
|---|
 | 79 |                                 return ptrsAssignable( src, eqvClass.type, env );
 | 
|---|
 | 80 |                         } // if
 | 
|---|
 | 81 |                 } // if
 | 
|---|
 | 82 |                 if ( dynamic_cast< VoidType* >( dest ) ) {
 | 
|---|
 | 83 |                         return objectCast( src, env, indexer );
 | 
|---|
 | 84 |                 } else {
 | 
|---|
 | 85 |                         PtrsCastable ptrs( dest, env, indexer );
 | 
|---|
 | 86 |                         src->accept( ptrs );
 | 
|---|
 | 87 |                         return ptrs.get_result();
 | 
|---|
 | 88 |                 } // if
 | 
|---|
 | 89 |         }
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |         PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer )
 | 
|---|
 | 92 |                 : dest( dest ), result( 0 ), env( env ), indexer( indexer )     {
 | 
|---|
 | 93 |         }
 | 
|---|
 | 94 | 
 | 
|---|
| [7e003011] | 95 |         void PtrsCastable::visit( __attribute__((unused)) VoidType *voidType) {
 | 
|---|
| [a32b204] | 96 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 97 |         }
 | 
|---|
 | 98 | 
 | 
|---|
| [7e003011] | 99 |         void PtrsCastable::visit( __attribute__((unused)) BasicType *basicType) {
 | 
|---|
| [a32b204] | 100 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 101 |         }
 | 
|---|
 | 102 | 
 | 
|---|
| [7e003011] | 103 |         void PtrsCastable::visit( __attribute__((unused)) PointerType *pointerType) {
 | 
|---|
| [a32b204] | 104 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 105 |         }
 | 
|---|
 | 106 | 
 | 
|---|
| [7e003011] | 107 |         void PtrsCastable::visit( __attribute__((unused)) ArrayType *arrayType) {
 | 
|---|
| [a32b204] | 108 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 109 |         }
 | 
|---|
 | 110 | 
 | 
|---|
| [7e003011] | 111 |         void PtrsCastable::visit( __attribute__((unused)) FunctionType *functionType) {
 | 
|---|
| [1d29d46] | 112 |                 // result = -1;
 | 
|---|
 | 113 |                 result = functionCast( dest, env, indexer );
 | 
|---|
| [a32b204] | 114 |         }
 | 
|---|
 | 115 | 
 | 
|---|
| [7e003011] | 116 |         void PtrsCastable::visit( __attribute__((unused)) StructInstType *inst) {
 | 
|---|
| [a32b204] | 117 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 118 |         }
 | 
|---|
 | 119 | 
 | 
|---|
| [7e003011] | 120 |         void PtrsCastable::visit( __attribute__((unused)) UnionInstType *inst) {
 | 
|---|
| [a32b204] | 121 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 122 |         }
 | 
|---|
 | 123 | 
 | 
|---|
| [7e003011] | 124 |         void PtrsCastable::visit( __attribute__((unused)) EnumInstType *inst) {
 | 
|---|
| [931dd12] | 125 |                 if ( dynamic_cast< EnumInstType* >( dest ) ) {
 | 
|---|
| [a32b204] | 126 |                         result = 1;
 | 
|---|
| [931dd12] | 127 |                 } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) {
 | 
|---|
| [a32b204] | 128 |                         if ( bt->get_kind() == BasicType::SignedInt ) {
 | 
|---|
 | 129 |                                 result = 0;
 | 
|---|
 | 130 |                         } else {
 | 
|---|
 | 131 |                                 result = 1;
 | 
|---|
 | 132 |                         }
 | 
|---|
 | 133 |                 } else {
 | 
|---|
 | 134 |                         result = objectCast( dest, env, indexer );
 | 
|---|
 | 135 |                 }
 | 
|---|
 | 136 |         }
 | 
|---|
 | 137 | 
 | 
|---|
| [af397ef8] | 138 |         void PtrsCastable::visit( __attribute__((unused)) TraitInstType *inst ) {}
 | 
|---|
| [a32b204] | 139 | 
 | 
|---|
 | 140 |         void PtrsCastable::visit(TypeInstType *inst) {
 | 
|---|
| [1d29d46] | 141 |                 //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1;
 | 
|---|
 | 142 |                 result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1;
 | 
|---|
| [a32b204] | 143 |         }
 | 
|---|
 | 144 | 
 | 
|---|
| [7e003011] | 145 |         void PtrsCastable::visit( __attribute__((unused)) TupleType *tupleType) {
 | 
|---|
| [a32b204] | 146 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 147 |         }
 | 
|---|
| [44b7088] | 148 | 
 | 
|---|
| [7e003011] | 149 |         void PtrsCastable::visit( __attribute__((unused)) VarArgsType *varArgsType) {
 | 
|---|
| [44b7088] | 150 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 151 |         }
 | 
|---|
| [89e6ffc] | 152 | 
 | 
|---|
| [7e003011] | 153 |         void PtrsCastable::visit( __attribute__((unused)) ZeroType *zeroType) {
 | 
|---|
| [89e6ffc] | 154 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 155 |         }
 | 
|---|
 | 156 | 
 | 
|---|
| [7e003011] | 157 |         void PtrsCastable::visit( __attribute__((unused)) OneType *oneType) {
 | 
|---|
| [89e6ffc] | 158 |                 result = objectCast( dest, env, indexer );
 | 
|---|
 | 159 |         }
 | 
|---|
| [51b73452] | 160 | } // namespace ResolvExpr
 | 
|---|
| [a32b204] | 161 | 
 | 
|---|
 | 162 | // Local Variables: //
 | 
|---|
 | 163 | // tab-width: 4 //
 | 
|---|
 | 164 | // mode: c++ //
 | 
|---|
 | 165 | // compile-command: "make install" //
 | 
|---|
 | 166 | // End: //
 | 
|---|