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 | private: |
---|
43 | Type *dest; |
---|
44 | int result; |
---|
45 | const TypeEnvironment &env; |
---|
46 | const SymTab::Indexer &indexer; |
---|
47 | }; |
---|
48 | |
---|
49 | int objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
50 | if ( dynamic_cast< FunctionType* >( src ) ) { |
---|
51 | return -1; |
---|
52 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) { |
---|
53 | EqvClass eqvClass; |
---|
54 | if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) { |
---|
55 | if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) { |
---|
56 | if ( tyDecl->get_kind() == TypeDecl::Ftype ) { |
---|
57 | return -1; |
---|
58 | } // if |
---|
59 | } //if |
---|
60 | } else if ( env.lookup( typeInst->get_name(), eqvClass ) ) { |
---|
61 | if ( eqvClass.kind == TypeDecl::Ftype ) { |
---|
62 | return -1; |
---|
63 | } // if |
---|
64 | } // if |
---|
65 | } //if |
---|
66 | return 1; |
---|
67 | } |
---|
68 | |
---|
69 | int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
70 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
71 | EqvClass eqvClass; |
---|
72 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
73 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
74 | } // if |
---|
75 | } // if |
---|
76 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
77 | return objectCast( src, env, indexer ); |
---|
78 | } else { |
---|
79 | PtrsCastable ptrs( dest, env, indexer ); |
---|
80 | src->accept( ptrs ); |
---|
81 | return ptrs.get_result(); |
---|
82 | } // if |
---|
83 | } |
---|
84 | |
---|
85 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
86 | : dest( dest ), result( 0 ), env( env ), indexer( indexer ) { |
---|
87 | } |
---|
88 | |
---|
89 | void PtrsCastable::visit(VoidType *voidType) { |
---|
90 | result = objectCast( dest, env, indexer ); |
---|
91 | } |
---|
92 | |
---|
93 | void PtrsCastable::visit(BasicType *basicType) { |
---|
94 | result = objectCast( dest, env, indexer ); |
---|
95 | } |
---|
96 | |
---|
97 | void PtrsCastable::visit(PointerType *pointerType) { |
---|
98 | result = objectCast( dest, env, indexer ); |
---|
99 | } |
---|
100 | |
---|
101 | void PtrsCastable::visit(ArrayType *arrayType) { |
---|
102 | result = objectCast( dest, env, indexer ); |
---|
103 | } |
---|
104 | |
---|
105 | void PtrsCastable::visit(FunctionType *functionType) { |
---|
106 | result = -1; |
---|
107 | } |
---|
108 | |
---|
109 | void PtrsCastable::visit(StructInstType *inst) { |
---|
110 | result = objectCast( dest, env, indexer ); |
---|
111 | } |
---|
112 | |
---|
113 | void PtrsCastable::visit(UnionInstType *inst) { |
---|
114 | result = objectCast( dest, env, indexer ); |
---|
115 | } |
---|
116 | |
---|
117 | void PtrsCastable::visit(EnumInstType *inst) { |
---|
118 | if ( dynamic_cast< EnumInstType* >( dest ) ) { |
---|
119 | result = 1; |
---|
120 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) { |
---|
121 | if ( bt->get_kind() == BasicType::SignedInt ) { |
---|
122 | result = 0; |
---|
123 | } else { |
---|
124 | result = 1; |
---|
125 | } |
---|
126 | } else { |
---|
127 | result = objectCast( dest, env, indexer ); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | void PtrsCastable::visit(TraitInstType *inst) { |
---|
132 | // I definitely don't think we should be doing anything here |
---|
133 | } |
---|
134 | |
---|
135 | void PtrsCastable::visit(TypeInstType *inst) { |
---|
136 | result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1; |
---|
137 | } |
---|
138 | |
---|
139 | void PtrsCastable::visit(TupleType *tupleType) { |
---|
140 | result = objectCast( dest, env, indexer ); |
---|
141 | } |
---|
142 | |
---|
143 | void PtrsCastable::visit(VarArgsType *varArgsType) { |
---|
144 | result = objectCast( dest, env, indexer ); |
---|
145 | } |
---|
146 | } // namespace ResolvExpr |
---|
147 | |
---|
148 | // Local Variables: // |
---|
149 | // tab-width: 4 // |
---|
150 | // mode: c++ // |
---|
151 | // compile-command: "make install" // |
---|
152 | // End: // |
---|