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 | // PtrsAssignable.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 11:44:11 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Wed Mar 2 17:36:05 2016 |
---|
13 | // Update Count : 8 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Common/PassVisitor.h" |
---|
17 | #include "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment |
---|
18 | #include "SynTree/Type.h" // for TypeInstType, Type, BasicType |
---|
19 | #include "SynTree/Visitor.h" // for Visitor |
---|
20 | |
---|
21 | |
---|
22 | namespace ResolvExpr { |
---|
23 | struct PtrsAssignable : public WithShortCircuiting { |
---|
24 | PtrsAssignable( Type *dest, const TypeEnvironment &env ); |
---|
25 | |
---|
26 | int get_result() const { return result; } |
---|
27 | |
---|
28 | void previsit( Type * ) { visit_children = false; } |
---|
29 | |
---|
30 | void postvisit( VoidType * voidType ); |
---|
31 | void postvisit( BasicType * basicType ); |
---|
32 | void postvisit( PointerType * pointerType ); |
---|
33 | void postvisit( ArrayType * arrayType ); |
---|
34 | void postvisit( FunctionType * functionType ); |
---|
35 | void postvisit( StructInstType * inst ); |
---|
36 | void postvisit( UnionInstType * inst ); |
---|
37 | void postvisit( EnumInstType * inst ); |
---|
38 | void postvisit( TraitInstType * inst ); |
---|
39 | void postvisit( TypeInstType * inst ); |
---|
40 | void postvisit( TupleType * tupleType ); |
---|
41 | void postvisit( VarArgsType * varArgsType ); |
---|
42 | void postvisit( ZeroType * zeroType ); |
---|
43 | void postvisit( OneType * oneType ); |
---|
44 | private: |
---|
45 | Type *dest; |
---|
46 | int result; |
---|
47 | const TypeEnvironment &env; |
---|
48 | }; |
---|
49 | |
---|
50 | int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) { |
---|
51 | // std::cerr << "assignable: " << src << " | " << dest << std::endl; |
---|
52 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
53 | if ( const EqvClass *eqvClass = env.lookup( destAsTypeInst->get_name() ) ) { |
---|
54 | return ptrsAssignable( src, eqvClass->type, env ); |
---|
55 | } // if |
---|
56 | } // if |
---|
57 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
58 | // void * = T * for any T is unsafe |
---|
59 | // xxx - this should be safe, but that currently breaks the build |
---|
60 | return -1; |
---|
61 | } else { |
---|
62 | PassVisitor<PtrsAssignable> ptrs( dest, env ); |
---|
63 | src->accept( ptrs ); |
---|
64 | return ptrs.pass.get_result(); |
---|
65 | } // if |
---|
66 | } |
---|
67 | |
---|
68 | PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {} |
---|
69 | |
---|
70 | void PtrsAssignable::postvisit( VoidType * ) { |
---|
71 | // T * = void * is disallowed - this is a change from C, where any |
---|
72 | // void * can be assigned or passed to a non-void pointer without a cast. |
---|
73 | } |
---|
74 | |
---|
75 | void PtrsAssignable::postvisit( __attribute__((unused)) BasicType *basicType ) {} |
---|
76 | void PtrsAssignable::postvisit( __attribute__((unused)) PointerType *pointerType ) {} |
---|
77 | void PtrsAssignable::postvisit( __attribute__((unused)) ArrayType *arrayType ) {} |
---|
78 | void PtrsAssignable::postvisit( __attribute__((unused)) FunctionType *functionType ) {} |
---|
79 | |
---|
80 | void PtrsAssignable::postvisit( __attribute__((unused)) StructInstType *inst ) {} |
---|
81 | void PtrsAssignable::postvisit( __attribute__((unused)) UnionInstType *inst ) {} |
---|
82 | |
---|
83 | void PtrsAssignable::postvisit( EnumInstType * ) { |
---|
84 | if ( dynamic_cast< BasicType* >( dest ) ) { |
---|
85 | // int * = E *, etc. is safe. This isn't technically correct, as each |
---|
86 | // enum has one basic type that it is compatible with, an that type can |
---|
87 | // differ from enum to enum. Without replicating GCC's internal logic, |
---|
88 | // there is no way to know which type this particular enum is compatible |
---|
89 | // with, so punt on this for now. |
---|
90 | result = 1; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | void PtrsAssignable::postvisit( __attribute__((unused)) TraitInstType *inst ) {} |
---|
95 | void PtrsAssignable::postvisit( TypeInstType *inst ) { |
---|
96 | if ( const EqvClass *eqvClass = env.lookup( inst->get_name() ) ) { |
---|
97 | if ( eqvClass->type ) { |
---|
98 | // T * = S * for any S depends on the type bound to T |
---|
99 | result = ptrsAssignable( eqvClass->type, dest, env ); |
---|
100 | } |
---|
101 | } // if |
---|
102 | } |
---|
103 | |
---|
104 | void PtrsAssignable::postvisit( __attribute__((unused)) TupleType *tupleType ) {} |
---|
105 | void PtrsAssignable::postvisit( __attribute__((unused)) VarArgsType *varArgsType ) {} |
---|
106 | void PtrsAssignable::postvisit( __attribute__((unused)) ZeroType *zeroType ) {} |
---|
107 | void PtrsAssignable::postvisit( __attribute__((unused)) OneType *oneType ) {} |
---|
108 | |
---|
109 | } // namespace ResolvExpr |
---|
110 | |
---|
111 | // Local Variables: // |
---|
112 | // tab-width: 4 // |
---|
113 | // mode: c++ // |
---|
114 | // compile-command: "make install" // |
---|
115 | // End: // |
---|