source: translator/ResolvExpr/PtrsCastable.cc @ a0d9f94

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since a0d9f94 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * This file is part of the Cforall project
3 *
4 * $Id: PtrsCastable.cc,v 1.5 2005/08/29 20:14:16 rcbilson Exp $
5 *
6 */
7
8#include "typeops.h"
9#include "SynTree/Type.h"
10#include "SynTree/Declaration.h"
11#include "SynTree/Visitor.h"
12#include "SymTab/Indexer.h"
13
14
15namespace ResolvExpr {
16
17class PtrsCastable : public Visitor
18{
19public:
20  PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
21 
22  int get_result() const { return result; }
23
24  virtual void visit(VoidType *voidType);
25  virtual void visit(BasicType *basicType);
26  virtual void visit(PointerType *pointerType);
27  virtual void visit(ArrayType *arrayType);
28  virtual void visit(FunctionType *functionType);
29  virtual void visit(StructInstType *inst);
30  virtual void visit(UnionInstType *inst);
31  virtual void visit(EnumInstType *inst);
32  virtual void visit(ContextInstType *inst);
33  virtual void visit(TypeInstType *inst);
34  virtual void visit(TupleType *tupleType);
35
36private:
37  Type *dest;
38  int result;
39  const TypeEnvironment &env;
40  const SymTab::Indexer &indexer;
41};
42
43int
44objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer )
45{
46  if( dynamic_cast< FunctionType* >( src ) ) {
47    return -1;
48  } else if( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) {
49    EqvClass eqvClass;
50    if( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) {
51      if( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) {
52        if( tyDecl->get_kind() == TypeDecl::Ftype ) {
53          return -1;
54        }
55      }
56    } else if( env.lookup( typeInst->get_name(), eqvClass ) ) {
57      if( eqvClass.kind == TypeDecl::Ftype ) {
58        return -1;
59      }
60    }
61  }
62  return 1;
63}
64
65int
66ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer )
67{
68  if( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
69    EqvClass eqvClass;
70    if( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
71      return ptrsAssignable( src, eqvClass.type, env );
72    }
73  }
74  if( dynamic_cast< VoidType* >( dest ) ) {
75    return objectCast( src, env, indexer );
76  } else {
77    PtrsCastable ptrs( dest, env, indexer );
78    src->accept( ptrs );
79    return ptrs.get_result();
80  }
81}
82
83PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer )
84  : dest( dest ), result( 0 ), env( env ), indexer( indexer )
85{
86}
87
88void 
89PtrsCastable::visit(VoidType *voidType)
90{
91  result = objectCast( dest, env, indexer );
92}
93
94void 
95PtrsCastable::visit(BasicType *basicType)
96{
97  result = objectCast( dest, env, indexer );
98}
99
100void 
101PtrsCastable::visit(PointerType *pointerType)
102{
103  result = objectCast( dest, env, indexer );
104}
105
106void 
107PtrsCastable::visit(ArrayType *arrayType)
108{
109  result = objectCast( dest, env, indexer );
110}
111
112void 
113PtrsCastable::visit(FunctionType *functionType)
114{
115  result = -1;
116}
117
118void 
119PtrsCastable::visit(StructInstType *inst)
120{
121  result = objectCast( dest, env, indexer );
122}
123
124void 
125PtrsCastable::visit(UnionInstType *inst)
126{
127  result = objectCast( dest, env, indexer );
128}
129
130void 
131PtrsCastable::visit(EnumInstType *inst)
132{
133  if( dynamic_cast< EnumInstType* >( inst ) ) {
134    result = 1;
135  } else if( BasicType *bt = dynamic_cast< BasicType* >( inst ) ) {
136    if( bt->get_kind() == BasicType::SignedInt ) {
137      result = 0;
138    } else {
139      result = 1;
140    }
141  } else {
142    result = objectCast( dest, env, indexer );
143  }
144}
145
146void 
147PtrsCastable::visit(ContextInstType *inst)
148{
149  // I definitely don't think we should be doing anything here
150}
151
152void 
153PtrsCastable::visit(TypeInstType *inst)
154{
155  result = objectCast( inst, env, indexer ) && objectCast( dest, env, indexer ) ? 1 : -1;
156}
157
158void 
159PtrsCastable::visit(TupleType *tupleType)
160{
161  result = objectCast( dest, env, indexer );
162}
163
164} // namespace ResolvExpr
Note: See TracBrowser for help on using the repository browser.