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.data.kind == TypeDecl::Ftype ) { |
---|
64 | return -1; |
---|
65 | } // if |
---|
66 | } // if |
---|
67 | } //if |
---|
68 | return 1; |
---|
69 | } |
---|
70 | int functionCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
71 | return -1 * objectCast( src, env, indexer ); // reverse the sense of objectCast |
---|
72 | } |
---|
73 | |
---|
74 | int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
75 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
76 | EqvClass eqvClass; |
---|
77 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
78 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
79 | } // if |
---|
80 | } // if |
---|
81 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
82 | return objectCast( src, env, indexer ); |
---|
83 | } else { |
---|
84 | PtrsCastable ptrs( dest, env, indexer ); |
---|
85 | src->accept( ptrs ); |
---|
86 | return ptrs.get_result(); |
---|
87 | } // if |
---|
88 | } |
---|
89 | |
---|
90 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
91 | : dest( dest ), result( 0 ), env( env ), indexer( indexer ) { |
---|
92 | } |
---|
93 | |
---|
94 | void PtrsCastable::visit( __attribute__((unused)) VoidType *voidType) { |
---|
95 | result = objectCast( dest, env, indexer ); |
---|
96 | } |
---|
97 | |
---|
98 | void PtrsCastable::visit( __attribute__((unused)) BasicType *basicType) { |
---|
99 | result = objectCast( dest, env, indexer ); |
---|
100 | } |
---|
101 | |
---|
102 | void PtrsCastable::visit( __attribute__((unused)) PointerType *pointerType) { |
---|
103 | result = objectCast( dest, env, indexer ); |
---|
104 | } |
---|
105 | |
---|
106 | void PtrsCastable::visit( __attribute__((unused)) ArrayType *arrayType) { |
---|
107 | result = objectCast( dest, env, indexer ); |
---|
108 | } |
---|
109 | |
---|
110 | void PtrsCastable::visit( __attribute__((unused)) FunctionType *functionType) { |
---|
111 | // result = -1; |
---|
112 | result = functionCast( dest, env, indexer ); |
---|
113 | } |
---|
114 | |
---|
115 | void PtrsCastable::visit( __attribute__((unused)) StructInstType *inst) { |
---|
116 | result = objectCast( dest, env, indexer ); |
---|
117 | } |
---|
118 | |
---|
119 | void PtrsCastable::visit( __attribute__((unused)) UnionInstType *inst) { |
---|
120 | result = objectCast( dest, env, indexer ); |
---|
121 | } |
---|
122 | |
---|
123 | void PtrsCastable::visit( __attribute__((unused)) EnumInstType *inst) { |
---|
124 | if ( dynamic_cast< EnumInstType* >( dest ) ) { |
---|
125 | result = 1; |
---|
126 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) { |
---|
127 | if ( bt->get_kind() == BasicType::SignedInt ) { |
---|
128 | result = 0; |
---|
129 | } else { |
---|
130 | result = 1; |
---|
131 | } |
---|
132 | } else { |
---|
133 | result = objectCast( dest, env, indexer ); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | void PtrsCastable::visit( __attribute__((unused)) TraitInstType *inst ) {} |
---|
138 | |
---|
139 | void PtrsCastable::visit(TypeInstType *inst) { |
---|
140 | //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1; |
---|
141 | result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1; |
---|
142 | } |
---|
143 | |
---|
144 | void PtrsCastable::visit( __attribute__((unused)) TupleType *tupleType) { |
---|
145 | result = objectCast( dest, env, indexer ); |
---|
146 | } |
---|
147 | |
---|
148 | void PtrsCastable::visit( __attribute__((unused)) VarArgsType *varArgsType) { |
---|
149 | result = objectCast( dest, env, indexer ); |
---|
150 | } |
---|
151 | |
---|
152 | void PtrsCastable::visit( __attribute__((unused)) ZeroType *zeroType) { |
---|
153 | result = objectCast( dest, env, indexer ); |
---|
154 | } |
---|
155 | |
---|
156 | void PtrsCastable::visit( __attribute__((unused)) OneType *oneType) { |
---|
157 | result = objectCast( dest, env, indexer ); |
---|
158 | } |
---|
159 | } // namespace ResolvExpr |
---|
160 | |
---|
161 | // Local Variables: // |
---|
162 | // tab-width: 4 // |
---|
163 | // mode: c++ // |
---|
164 | // compile-command: "make install" // |
---|
165 | // End: // |
---|