source: src/ResolvExpr/PtrsCastable.cc@ 903f7c3

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 903f7c3 was 1d29d46, checked in by Aaron Moss <a3moss@…>, 8 years ago

Function pointer casts allowed by resolver

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[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//
[2c57025]7// PtrsCastable.cc --
[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
12// Last Modified On : Wed Mar 2 17:36:18 2016
13// Update Count : 8
[a32b204]14//
[51b73452]15
16#include "typeops.h"
17#include "SynTree/Type.h"
18#include "SynTree/Declaration.h"
19#include "SynTree/Visitor.h"
20#include "SymTab/Indexer.h"
21
22
23namespace ResolvExpr {
[a32b204]24 class PtrsCastable : public Visitor {
25 public:
26 PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
[2c57025]27
[a32b204]28 int get_result() const { return result; }
29
30 virtual void visit(VoidType *voidType);
31 virtual void visit(BasicType *basicType);
32 virtual void visit(PointerType *pointerType);
33 virtual void visit(ArrayType *arrayType);
34 virtual void visit(FunctionType *functionType);
35 virtual void visit(StructInstType *inst);
36 virtual void visit(UnionInstType *inst);
37 virtual void visit(EnumInstType *inst);
[4040425]38 virtual void visit(TraitInstType *inst);
[a32b204]39 virtual void visit(TypeInstType *inst);
40 virtual void visit(TupleType *tupleType);
[44b7088]41 virtual void visit(VarArgsType *varArgsType);
[89e6ffc]42 virtual void visit(ZeroType *zeroType);
43 virtual void visit(OneType *oneType);
[a32b204]44 private:
45 Type *dest;
46 int result;
47 const TypeEnvironment &env;
48 const SymTab::Indexer &indexer;
49 };
50
51 int objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
52 if ( dynamic_cast< FunctionType* >( src ) ) {
53 return -1;
54 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) {
55 EqvClass eqvClass;
56 if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) {
57 if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) {
58 if ( tyDecl->get_kind() == TypeDecl::Ftype ) {
59 return -1;
60 } // if
61 } //if
62 } else if ( env.lookup( typeInst->get_name(), eqvClass ) ) {
[2c57025]63 if ( eqvClass.data.kind == TypeDecl::Ftype ) {
[a32b204]64 return -1;
65 } // if
66 } // if
67 } //if
68 return 1;
69 }
[1d29d46]70 int functionCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
71 return -1 * objectCast( src, env, indexer ); // reverse the sense of objectCast
72 }
[a32b204]73
74 int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
75 if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
76 EqvClass eqvClass;
77 if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
78 return ptrsAssignable( src, eqvClass.type, env );
79 } // if
80 } // if
81 if ( dynamic_cast< VoidType* >( dest ) ) {
82 return objectCast( src, env, indexer );
83 } else {
84 PtrsCastable ptrs( dest, env, indexer );
85 src->accept( ptrs );
86 return ptrs.get_result();
87 } // if
88 }
89
90 PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer )
91 : dest( dest ), result( 0 ), env( env ), indexer( indexer ) {
92 }
93
94 void PtrsCastable::visit(VoidType *voidType) {
95 result = objectCast( dest, env, indexer );
96 }
97
98 void PtrsCastable::visit(BasicType *basicType) {
99 result = objectCast( dest, env, indexer );
100 }
101
102 void PtrsCastable::visit(PointerType *pointerType) {
103 result = objectCast( dest, env, indexer );
104 }
105
106 void PtrsCastable::visit(ArrayType *arrayType) {
107 result = objectCast( dest, env, indexer );
108 }
109
110 void PtrsCastable::visit(FunctionType *functionType) {
[1d29d46]111 // result = -1;
112 result = functionCast( dest, env, indexer );
[a32b204]113 }
114
115 void PtrsCastable::visit(StructInstType *inst) {
116 result = objectCast( dest, env, indexer );
117 }
118
119 void PtrsCastable::visit(UnionInstType *inst) {
120 result = objectCast( dest, env, indexer );
121 }
122
123 void PtrsCastable::visit(EnumInstType *inst) {
[931dd12]124 if ( dynamic_cast< EnumInstType* >( dest ) ) {
[a32b204]125 result = 1;
[931dd12]126 } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) {
[a32b204]127 if ( bt->get_kind() == BasicType::SignedInt ) {
128 result = 0;
129 } else {
130 result = 1;
131 }
132 } else {
133 result = objectCast( dest, env, indexer );
134 }
135 }
136
[4040425]137 void PtrsCastable::visit(TraitInstType *inst) {
[a32b204]138 // I definitely don't think we should be doing anything here
139 }
140
141 void PtrsCastable::visit(TypeInstType *inst) {
[1d29d46]142 //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1;
143 result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1;
[a32b204]144 }
145
146 void PtrsCastable::visit(TupleType *tupleType) {
147 result = objectCast( dest, env, indexer );
148 }
[44b7088]149
150 void PtrsCastable::visit(VarArgsType *varArgsType) {
151 result = objectCast( dest, env, indexer );
152 }
[89e6ffc]153
154 void PtrsCastable::visit(ZeroType *zeroType) {
155 result = objectCast( dest, env, indexer );
156 }
157
158 void PtrsCastable::visit(OneType *oneType) {
159 result = objectCast( dest, env, indexer );
160 }
[51b73452]161} // namespace ResolvExpr
[a32b204]162
163// Local Variables: //
164// tab-width: 4 //
165// mode: c++ //
166// compile-command: "make install" //
167// End: //
Note: See TracBrowser for help on using the repository browser.