summaryrefslogtreecommitdiff
path: root/graphics/asymptote/env.cc
blob: 3b0a59b7a328bdcc9fe3778048c7d0387310bb1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*****
 * env.cc
 * Andy Hammerlindl 2002/6/20
 *
 * Keeps track of the namespaces of variables and types when traversing
 * the abstract syntax.
 *****/

#include "env.h"
#include "record.h"
#include "genv.h"
#include "builtin.h"

using namespace types;

namespace trans {

// Instances of this class are passed to types::ty objects so that they can call
// back to env when checking casting of subtypes.
class envCaster : public caster {
  protoenv &e;
  symbol name;
public:
  envCaster(protoenv &e, symbol name)
    : e(e), name(name) {}

  access *operator() (ty *target, ty *source) {
    return e.lookupCast(target, source, name);
  }

  bool castable(ty *target, ty *source) {
    return e.castable(target, source, name);
  }
};

access *protoenv::baseLookupCast(ty *target, ty *source, symbol name) {
  static identAccess id;

  assert(target->kind != ty_overloaded &&
         source->kind != ty_overloaded);

  // If errors already exist, don't report more.  This may, however, cause
  // problems with resoving the signature of an overloaded function.  The
  // abstract syntax should check if any of the parameters had an error before
  // finding the signature.
  if (target->kind == ty_error || source->kind == ty_error)
    return &id;
  else if (equivalent(target,source))
    return &id;
  else {
    varEntry *v=lookupVarByType(name,new function(target,source));
    return v ? v->getLocation() : 0;
  }
}

access *protoenv::lookupCast(ty *target, ty *source, symbol name) {
  access *a=baseLookupCast(target, source, name);
  if (a)
    return a;

  envCaster ec(*this, name);
  return source->castTo(target, ec);
}

bool protoenv::castable(ty *target, ty *source, symbol name) {
  struct castTester : public tester {
    protoenv &e;
    symbol name;

    castTester(protoenv &e, symbol name)
      : e(e), name(name) {}

    bool base(ty *t, ty *s) {
      access *a=e.baseLookupCast(t, s, name);
      if (a)
        return true;

      envCaster ec(e, name);
      return s->castable(t, ec);
    }
  };

  castTester ct(*this, name);
  return ct.test(target,source);
}

bool protoenv::fastCastable(ty *target, ty *source) {
  assert(target->kind != types::ty_overloaded);
  assert(target->kind != types::ty_error);
  assert(source->kind != types::ty_error);

  // To avoid memory allocation, fill one static variable with new parameters
  // in each call.
  // Warning: This is not re-entrant if asy ever goes multi-threaded.
  static types::function castFunc(primVoid(), primVoid());
  castFunc.result = target;

  if (source->kind == types::ty_overloaded) {
    bool result = false;
    types::ty_vector& v = ((overloaded *)source)->sub;
    for (size_t i = 0; i < v.size(); ++i) {
      castFunc.sig.formals[0].t = v[i];
      if (lookupVarByType(symbol::castsym, &castFunc)) {
        result = true;
        break;
      }
    }
    //assert(result == castable(target, source, symbol::castsym));
    //cout << "fc OVERLOADED " << (result ? "CAST" : "FAIL") << endl;
    return result;
  }
  //else cout << "fc SIMPLE" << endl;

  // Don't test for equivalent, as that is already done by the castScore
  // code.  Assert disabled for speed.
#if 0
  assert(!equivalent(target, source));
#endif

  castFunc.sig.formals[0].t = source;

  if (lookupVarByType(symbol::castsym, &castFunc))
    return true;

  // Test for generic casts of null.  This should be moved to a types.h
  // routine.
  return source->kind == ty_null && target->isReference();
}

access *protoenv::fastLookupCast(ty *target, ty *source) {
  assert(target->kind != types::ty_overloaded);
  assert(target->kind != types::ty_error);
  assert(source->kind != types::ty_overloaded);
  assert(source->kind != types::ty_error);

  // Warning: This is not re-entrant.
  static types::function castFunc(primVoid(), primVoid());
  castFunc.result = target;
  castFunc.sig.formals[0].t = source;

  varEntry *ve = lookupVarByType(symbol::castsym, &castFunc);
  if (ve)
    return ve->getLocation();

  // Fall back on slow routine.
  return lookupCast(target, source, symbol::castsym);
}


ty *protoenv::castTarget(ty *target, ty *source, symbol name) {
  struct resolver : public collector {
    protoenv &e;
    symbol name;

    resolver(protoenv &e, symbol name)
      : e(e), name(name) {}

    types::ty *base(types::ty *target, types::ty *source) {
      return e.castable(target, source, name) ? target : 0;
    }
  };

  resolver r(*this, name);
  return r.collect(target, source);
}

ty *protoenv::castSource(ty *target, ty *source, symbol name) {
  struct resolver : public collector {
    protoenv &e;
    symbol name;

    resolver(protoenv &e, symbol name)
      : e(e), name(name) {}

    types::ty *base(types::ty *target, types::ty *source) {
      return e.castable(target, source, name) ? source : 0;
    }
  };

  resolver r(*this, name);
  return r.collect(target, source);
}

void protoenv::addArrayOps(array *a)
{
  trans::addArrayOps(ve, a);
}

void protoenv::addRecordOps(record *r)
{
  trans::addRecordOps(ve, r);
}

void protoenv::addFunctionOps(function *f)
{
  trans::addFunctionOps(ve, f);
}

env::env(genv &ge)
  : protoenv(venv::file_env_tag()), ge(ge)
{
  // NOTE: May want to make this initial environment into a "builtin" module,
  // and then import the builtin module.
  base_tenv(te);
  base_venv(ve);
}

env::~env()
{
}

record *env::getModule(symbol id, string filename)
{
  return ge.getModule(id, filename);
}

}