[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 | //
|
---|
[5f225f5] | 7 | // PtrsCastable.cpp --
|
---|
[a32b204] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sun May 17 11:48:00 2015
|
---|
[4040425] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[07de76b] | 12 | // Last Modified On : Wed Dec 11 21:48:33 2019
|
---|
| 13 | // Update Count : 9
|
---|
[a32b204] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
[5bf3976] | 16 | #include "PtrsCastable.hpp"
|
---|
| 17 |
|
---|
[3c89751] | 18 | #include "AST/Decl.hpp"
|
---|
| 19 | #include "AST/Pass.hpp"
|
---|
| 20 | #include "AST/Type.hpp"
|
---|
| 21 | #include "AST/TypeEnvironment.hpp"
|
---|
[5bf3976] | 22 | #include "ResolvExpr/PtrsAssignable.hpp" // for ptrsAssignable
|
---|
[51b73452] | 23 |
|
---|
| 24 | namespace ResolvExpr {
|
---|
[3c89751] | 25 |
|
---|
| 26 | namespace {
|
---|
| 27 | // can this type be cast to an object (1 for yes, -1 for no)
|
---|
[7870799] | 28 | int objectCast(
|
---|
| 29 | const ast::Type * src, const ast::TypeEnvironment & env, const ast::SymbolTable & symtab
|
---|
[3c89751] | 30 | ) {
|
---|
| 31 | if ( dynamic_cast< const ast::FunctionType * >( src ) ) {
|
---|
| 32 | return -1;
|
---|
| 33 | } else if ( auto inst = dynamic_cast< const ast::TypeInstType * >( src ) ) {
|
---|
| 34 | if ( const ast::NamedTypeDecl * named = symtab.lookupType( inst->name ) ) {
|
---|
| 35 | if ( auto tyDecl = dynamic_cast< const ast::TypeDecl * >( named ) ) {
|
---|
[07de76b] | 36 | if ( tyDecl->kind == ast::TypeDecl::Ftype ) {
|
---|
[3c89751] | 37 | return -1;
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
[3e5dd913] | 40 | } else if ( const ast::EqvClass * eqvClass = env.lookup( *inst ) ) {
|
---|
[07de76b] | 41 | if ( eqvClass->data.kind == ast::TypeDecl::Ftype ) {
|
---|
[3c89751] | 42 | return -1;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | return 1;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | // can this type be cast to a function (inverse of objectCast)
|
---|
[7870799] | 51 | int functionCast(
|
---|
| 52 | const ast::Type * src, const ast::TypeEnvironment & env, const ast::SymbolTable & symtab
|
---|
[3c89751] | 53 | ) {
|
---|
| 54 | return -1 * objectCast( src, env, symtab );
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[0bd3faf] | 57 | class PtrsCastable : public ast::WithShortCircuiting {
|
---|
[3c89751] | 58 | const ast::Type * dst;
|
---|
| 59 | const ast::TypeEnvironment & env;
|
---|
| 60 | const ast::SymbolTable & symtab;
|
---|
| 61 | public:
|
---|
| 62 | int result;
|
---|
| 63 |
|
---|
[0bd3faf] | 64 | PtrsCastable(
|
---|
[3c89751] | 65 | const ast::Type * d, const ast::TypeEnvironment & e, const ast::SymbolTable & syms )
|
---|
| 66 | : dst( d ), env( e ), symtab( syms ), result( 0 ) {}
|
---|
| 67 |
|
---|
| 68 | void previsit( const ast::Type * ) { visit_children = false; }
|
---|
| 69 |
|
---|
| 70 | void postvisit( const ast::VoidType * ) {
|
---|
| 71 | result = objectCast( dst, env, symtab );
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void postvisit( const ast::BasicType * ) {
|
---|
| 75 | result = objectCast( dst, env, symtab );
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void postvisit( const ast::PointerType * ) {
|
---|
| 79 | result = objectCast( dst, env, symtab );
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void postvisit( const ast::ArrayType * ) {
|
---|
| 83 | result = objectCast( dst, env, symtab );
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void postvisit( const ast::FunctionType * ) {
|
---|
| 87 | result = functionCast( dst, env, symtab );
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void postvisit( const ast::StructInstType * ) {
|
---|
| 91 | result = objectCast( dst, env, symtab );
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | void postvisit( const ast::UnionInstType * ) {
|
---|
| 95 | result = objectCast( dst, env, symtab );
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void postvisit( const ast::EnumInstType * ) {
|
---|
| 99 | if ( dynamic_cast< const ast::EnumInstType * >( dst ) ) {
|
---|
| 100 | result = 1;
|
---|
| 101 | } else if ( auto bt = dynamic_cast< const ast::BasicType * >( dst ) ) {
|
---|
[7a780ad] | 102 | if ( bt->kind == ast::BasicKind::SignedInt ) {
|
---|
[3c89751] | 103 | result = 0;
|
---|
| 104 | } else {
|
---|
| 105 | result = 1;
|
---|
| 106 | }
|
---|
| 107 | } else {
|
---|
| 108 | result = objectCast( dst, env, symtab );
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | void postvisit( const ast::TraitInstType * ) {}
|
---|
| 113 |
|
---|
| 114 | void postvisit( const ast::TypeInstType * inst ) {
|
---|
| 115 | // check trait and destination type are both object or both function
|
---|
| 116 | result = objectCast( inst, env, symtab ) == objectCast( dst, env, symtab ) ? 1 : -1;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | void postvisit( const ast::TupleType * ) {
|
---|
| 120 | result = objectCast( dst, env, symtab );
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | void postvisit( const ast::VarArgsType * ) {
|
---|
| 124 | result = objectCast( dst, env, symtab );
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void postvisit( const ast::ZeroType * ) {
|
---|
| 128 | result = objectCast( dst, env, symtab );
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void postvisit( const ast::OneType * ) {
|
---|
| 132 | result = objectCast( dst, env, symtab );
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | };
|
---|
| 136 | } // anonymous namespace
|
---|
| 137 |
|
---|
[7870799] | 138 | int ptrsCastable(
|
---|
| 139 | const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab,
|
---|
| 140 | const ast::TypeEnvironment & env
|
---|
[3c89751] | 141 | ) {
|
---|
| 142 | if ( auto inst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
|
---|
[3e5dd913] | 143 | if ( const ast::EqvClass * eqvClass = env.lookup( *inst ) ) {
|
---|
[3c89751] | 144 | return ptrsAssignable( src, eqvClass->bound, env );
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | if ( dynamic_cast< const ast::VoidType * >( dst ) ) {
|
---|
| 149 | return objectCast( src, env, symtab );
|
---|
| 150 | } else {
|
---|
[0bd3faf] | 151 | return ast::Pass<PtrsCastable>::read( src, dst, env, symtab );
|
---|
[3c89751] | 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[51b73452] | 155 | } // namespace ResolvExpr
|
---|
[a32b204] | 156 |
|
---|
| 157 | // Local Variables: //
|
---|
| 158 | // tab-width: 4 //
|
---|
| 159 | // mode: c++ //
|
---|
| 160 | // compile-command: "make install" //
|
---|
| 161 | // End: //
|
---|