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