[a32b204] | 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 | //
|
---|
[5ccb10d] | 7 | // PtrsAssignable.cc --
|
---|
[a32b204] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sun May 17 11:44:11 2015
|
---|
[df9317bd] | 11 | // Last Modified By : Andrew
|
---|
| 12 | // Last Modified On : Mon Jun 24 15:29:00 2019
|
---|
| 13 | // Update Count : 9
|
---|
[a32b204] | 14 | //
|
---|
| 15 |
|
---|
[5bf3976] | 16 | #include "PtrsAssignable.hpp"
|
---|
[df9317bd] | 17 |
|
---|
| 18 | #include "AST/Pass.hpp"
|
---|
| 19 | #include "AST/Type.hpp"
|
---|
| 20 | #include "AST/TypeEnvironment.hpp"
|
---|
[51b73452] | 21 |
|
---|
| 22 | namespace ResolvExpr {
|
---|
[5ccb10d] | 23 |
|
---|
[0bd3faf] | 24 | struct PtrsAssignable : public ast::WithShortCircuiting {
|
---|
[df9317bd] | 25 | const ast::Type * dst;
|
---|
| 26 | const ast::TypeEnvironment & typeEnv;
|
---|
| 27 | int result;
|
---|
| 28 |
|
---|
[0bd3faf] | 29 | PtrsAssignable( const ast::Type * dst, const ast::TypeEnvironment & env ) :
|
---|
[df9317bd] | 30 | dst( dst ), typeEnv( env ), result( 0 ) {}
|
---|
| 31 |
|
---|
[0bd3faf] | 32 | void previsit( ast::Type * ) { visit_children = false; }
|
---|
[df9317bd] | 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 ) {
|
---|
[3e5dd913] | 45 | if ( const ast::EqvClass * eqv = typeEnv.lookup( *inst ) ) {
|
---|
[df9317bd] | 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 |
|
---|
[fb2bde4] | 54 | int ptrsAssignable( const ast::Type * src, const ast::Type * dst,
|
---|
| 55 | const ast::TypeEnvironment & env ) {
|
---|
[df9317bd] | 56 | if ( const ast::TypeInstType * dstAsInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
|
---|
[3e5dd913] | 57 | if ( const ast::EqvClass * eqv = env.lookup( *dstAsInst ) ) {
|
---|
[df9317bd] | 58 | return ptrsAssignable( src, eqv->bound, env );
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | if ( dynamic_cast< const ast::VoidType * >( dst ) ) {
|
---|
| 62 | return -1;
|
---|
| 63 | } else {
|
---|
[0bd3faf] | 64 | ast::Pass<PtrsAssignable> visitor( dst, env );
|
---|
[df9317bd] | 65 | src->accept( visitor );
|
---|
[7ff3e522] | 66 | return visitor.core.result;
|
---|
[df9317bd] | 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 | }
|
---|
[fb2bde4] | 99 | return 0;
|
---|
[df9317bd] | 100 | #endif
|
---|
[fb2bde4] | 101 | }
|
---|
| 102 |
|
---|
[51b73452] | 103 | } // namespace ResolvExpr
|
---|
[a32b204] | 104 |
|
---|
| 105 | // Local Variables: //
|
---|
| 106 | // tab-width: 4 //
|
---|
| 107 | // mode: c++ //
|
---|
| 108 | // compile-command: "make install" //
|
---|
| 109 | // End: //
|
---|