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 "Common/PassVisitor.h"
|
---|
17 | #include "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment
|
---|
18 | #include "SymTab/Indexer.h" // for Indexer
|
---|
19 | #include "SynTree/Declaration.h" // for TypeDecl, TypeDecl::Kind::Ftype
|
---|
20 | #include "SynTree/Type.h" // for TypeInstType, Type, BasicType
|
---|
21 | #include "SynTree/Visitor.h" // for Visitor
|
---|
22 | #include "typeops.h" // for ptrsAssignable
|
---|
23 |
|
---|
24 | namespace ResolvExpr {
|
---|
25 | struct PtrsCastable : public WithShortCircuiting {
|
---|
26 | public:
|
---|
27 | PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
|
---|
28 |
|
---|
29 | int get_result() const { return result; }
|
---|
30 |
|
---|
31 | void previsit( Type * ) { visit_children = false; }
|
---|
32 |
|
---|
33 | void postvisit( VoidType * voidType );
|
---|
34 | void postvisit( BasicType * basicType );
|
---|
35 | void postvisit( PointerType * pointerType );
|
---|
36 | void postvisit( ArrayType * arrayType );
|
---|
37 | void postvisit( FunctionType * functionType );
|
---|
38 | void postvisit( StructInstType * inst );
|
---|
39 | void postvisit( UnionInstType * inst );
|
---|
40 | void postvisit( EnumInstType * inst );
|
---|
41 | void postvisit( TraitInstType * inst );
|
---|
42 | void postvisit( TypeInstType * inst );
|
---|
43 | void postvisit( TupleType * tupleType );
|
---|
44 | void postvisit( VarArgsType * varArgsType );
|
---|
45 | void postvisit( ZeroType * zeroType );
|
---|
46 | void postvisit( OneType * oneType );
|
---|
47 | private:
|
---|
48 | Type *dest;
|
---|
49 | int result;
|
---|
50 | const TypeEnvironment &env;
|
---|
51 | const SymTab::Indexer &indexer;
|
---|
52 | };
|
---|
53 |
|
---|
54 | namespace {
|
---|
55 | int objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
|
---|
56 | if ( dynamic_cast< FunctionType* >( src ) ) {
|
---|
57 | return -1;
|
---|
58 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) {
|
---|
59 | if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) {
|
---|
60 | if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) {
|
---|
61 | if ( tyDecl->get_kind() == TypeDecl::Ftype ) {
|
---|
62 | return -1;
|
---|
63 | } // if
|
---|
64 | } //if
|
---|
65 | } else if ( const EqvClass *eqvClass = env.lookup( typeInst->get_name() ) ) {
|
---|
66 | if ( eqvClass->data.kind == TypeDecl::Ftype ) {
|
---|
67 | return -1;
|
---|
68 | } // if
|
---|
69 | } // if
|
---|
70 | } //if
|
---|
71 | return 1;
|
---|
72 | }
|
---|
73 | int functionCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
|
---|
74 | return -1 * objectCast( src, env, indexer ); // reverse the sense of objectCast
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
|
---|
79 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
|
---|
80 | if ( const EqvClass *eqvClass = env.lookup( destAsTypeInst->get_name() ) ) {
|
---|
81 | // xxx - should this be ptrsCastable?
|
---|
82 | return ptrsAssignable( src, eqvClass->type, env );
|
---|
83 | } // if
|
---|
84 | } // if
|
---|
85 | if ( dynamic_cast< VoidType* >( dest ) ) {
|
---|
86 | return objectCast( src, env, indexer );
|
---|
87 | } else {
|
---|
88 | PassVisitor<PtrsCastable> ptrs( dest, env, indexer );
|
---|
89 | src->accept( ptrs );
|
---|
90 | return ptrs.pass.get_result();
|
---|
91 | } // if
|
---|
92 | }
|
---|
93 |
|
---|
94 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer )
|
---|
95 | : dest( dest ), result( 0 ), env( env ), indexer( indexer ) {
|
---|
96 | }
|
---|
97 |
|
---|
98 | void PtrsCastable::postvisit( VoidType * ) {
|
---|
99 | result = objectCast( dest, env, indexer );
|
---|
100 | }
|
---|
101 |
|
---|
102 | void PtrsCastable::postvisit( BasicType * ) {
|
---|
103 | result = objectCast( dest, env, indexer );
|
---|
104 | }
|
---|
105 |
|
---|
106 | void PtrsCastable::postvisit( PointerType * ) {
|
---|
107 | result = objectCast( dest, env, indexer );
|
---|
108 | }
|
---|
109 |
|
---|
110 | void PtrsCastable::postvisit( ArrayType * ) {
|
---|
111 | result = objectCast( dest, env, indexer );
|
---|
112 | }
|
---|
113 |
|
---|
114 | void PtrsCastable::postvisit( FunctionType * ) {
|
---|
115 | // result = -1;
|
---|
116 | result = functionCast( dest, env, indexer );
|
---|
117 | }
|
---|
118 |
|
---|
119 | void PtrsCastable::postvisit( StructInstType * ) {
|
---|
120 | result = objectCast( dest, env, indexer );
|
---|
121 | }
|
---|
122 |
|
---|
123 | void PtrsCastable::postvisit( UnionInstType * ) {
|
---|
124 | result = objectCast( dest, env, indexer );
|
---|
125 | }
|
---|
126 |
|
---|
127 | void PtrsCastable::postvisit( EnumInstType * ) {
|
---|
128 | if ( dynamic_cast< EnumInstType* >( dest ) ) {
|
---|
129 | result = 1;
|
---|
130 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) {
|
---|
131 | if ( bt->get_kind() == BasicType::SignedInt ) {
|
---|
132 | result = 0;
|
---|
133 | } else {
|
---|
134 | result = 1;
|
---|
135 | }
|
---|
136 | } else {
|
---|
137 | result = objectCast( dest, env, indexer );
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | void PtrsCastable::postvisit( TraitInstType * ) {}
|
---|
142 |
|
---|
143 | void PtrsCastable::postvisit(TypeInstType *inst) {
|
---|
144 | //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1;
|
---|
145 | result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1;
|
---|
146 | }
|
---|
147 |
|
---|
148 | void PtrsCastable::postvisit( TupleType * ) {
|
---|
149 | result = objectCast( dest, env, indexer );
|
---|
150 | }
|
---|
151 |
|
---|
152 | void PtrsCastable::postvisit( VarArgsType * ) {
|
---|
153 | result = objectCast( dest, env, indexer );
|
---|
154 | }
|
---|
155 |
|
---|
156 | void PtrsCastable::postvisit( ZeroType * ) {
|
---|
157 | result = objectCast( dest, env, indexer );
|
---|
158 | }
|
---|
159 |
|
---|
160 | void PtrsCastable::postvisit( OneType * ) {
|
---|
161 | result = objectCast( dest, env, indexer );
|
---|
162 | }
|
---|
163 | } // namespace ResolvExpr
|
---|
164 |
|
---|
165 | // Local Variables: //
|
---|
166 | // tab-width: 4 //
|
---|
167 | // mode: c++ //
|
---|
168 | // compile-command: "make install" //
|
---|
169 | // End: //
|
---|