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 : Andrew
|
---|
12 | // Last Modified On : Mon Jun 24 15:29:00 2019
|
---|
13 | // Update Count : 9
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "PtrsAssignable.hpp"
|
---|
17 |
|
---|
18 | #include "AST/Pass.hpp"
|
---|
19 | #include "AST/Type.hpp"
|
---|
20 | #include "AST/TypeEnvironment.hpp"
|
---|
21 |
|
---|
22 | namespace ResolvExpr {
|
---|
23 |
|
---|
24 | struct PtrsAssignable : public ast::WithShortCircuiting {
|
---|
25 | const ast::Type * dst;
|
---|
26 | const ast::TypeEnvironment & typeEnv;
|
---|
27 | int result;
|
---|
28 |
|
---|
29 | PtrsAssignable( const ast::Type * dst, const ast::TypeEnvironment & env ) :
|
---|
30 | dst( dst ), typeEnv( env ), result( 0 ) {}
|
---|
31 |
|
---|
32 | void previsit( ast::Type * ) { visit_children = false; }
|
---|
33 |
|
---|
34 | void postvisit( const ast::EnumInstType * ) {
|
---|
35 | if ( dynamic_cast< const ast::BasicType * >( dst ) ) {
|
---|
36 | // int * = E *, etc. is safe. This isn't technically correct, as each
|
---|
37 | // enum has one basic type that it is compatible with, an that type can
|
---|
38 | // differ from enum to enum. Without replicating GCC's internal logic,
|
---|
39 | // there is no way to know which type this particular enum is compatible
|
---|
40 | // with, so punt on this for now.
|
---|
41 | result = 1;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | void postvisit( const ast::TypeInstType * inst ) {
|
---|
45 | if ( const ast::EqvClass * eqv = typeEnv.lookup( *inst ) ) {
|
---|
46 | if ( eqv->bound ) {
|
---|
47 | // T * = S * for any S depends on the type bound to T
|
---|
48 | result = ptrsAssignable( eqv->bound, dst, typeEnv );
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | };
|
---|
53 |
|
---|
54 | int ptrsAssignable( const ast::Type * src, const ast::Type * dst,
|
---|
55 | const ast::TypeEnvironment & env ) {
|
---|
56 | if ( const ast::TypeInstType * dstAsInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
|
---|
57 | if ( const ast::EqvClass * eqv = env.lookup( *dstAsInst ) ) {
|
---|
58 | return ptrsAssignable( src, eqv->bound, env );
|
---|
59 | }
|
---|
60 | }
|
---|
61 | if ( dynamic_cast< const ast::VoidType * >( dst ) ) {
|
---|
62 | return -1;
|
---|
63 | } else {
|
---|
64 | ast::Pass<PtrsAssignable> visitor( dst, env );
|
---|
65 | src->accept( visitor );
|
---|
66 | return visitor.core.result;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // see ticket #136 (this should be able to replace the visitor).
|
---|
70 | #if 0
|
---|
71 | if ( const ast::TypeInstType * dstAsTypeInst =
|
---|
72 | dynamic_cast< const ast::TypeInstType* >( dst ) ) {
|
---|
73 | if ( const ast::EqvClass * eqv = env.lookup( dstAsTypeInst->get_name() ) ) {
|
---|
74 | return ptrsAssignable( src, eqv->type, env );
|
---|
75 | } // if
|
---|
76 | } // if
|
---|
77 | if ( dynamic_cast< VoidType* >( dst ) ) {
|
---|
78 | // void * = T * for any T is unsafe
|
---|
79 | // xxx - this should be safe, but that currently breaks the build
|
---|
80 | return -1;
|
---|
81 | } else if ( dynamic_cast< EnumInstType * >( src ) ) {
|
---|
82 | if ( dynamic_cast< BasicType * >( dst ) ) {
|
---|
83 | // int * = E *, etc. is safe. This isn't technically correct, as each
|
---|
84 | // enum has one basic type that it is compatible with, an that type can
|
---|
85 | // differ from enum to enum. Without replicating GCC's internal logic,
|
---|
86 | // there is no way to know which type this particular enum is compatible
|
---|
87 | // with, so punt on this for now.
|
---|
88 | return 1;
|
---|
89 | }
|
---|
90 | } else if ( const ast::TypeInstType * typeInstType =
|
---|
91 | dynamic_cast< const ast::TypeInstType * >( src ) ) {
|
---|
92 | if ( const ast::EqvClass * eqv = env.lookup( typeInstType->name ) ) {
|
---|
93 | if ( eqv->bound ) {
|
---|
94 | // T * = S * for any S depends on the type bound to T
|
---|
95 | return ptrsAssignable( eqv->bound, dst, env );
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | return 0;
|
---|
100 | #endif
|
---|
101 | }
|
---|
102 |
|
---|
103 | } // namespace ResolvExpr
|
---|
104 |
|
---|
105 | // Local Variables: //
|
---|
106 | // tab-width: 4 //
|
---|
107 | // mode: c++ //
|
---|
108 | // compile-command: "make install" //
|
---|
109 | // End: //
|
---|