summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/name.cc
blob: b3853b5a91c9f16fd461e3aafc6339c14bc0c47d (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*****
 * name.cc
 * Andy Hammerlindl2002/07/14
 *
 * Qualified names (such as x, f, builtin.sin, a.b.c.d, etc.) can be used
 * either as variables or a type names.  This class stores qualified
 * names used in nameExp and nameTy in the abstract syntax, and
 * implements the exp and type functions.
 *****/

#include "name.h"
#include "frame.h"
#include "record.h"
#include "coenv.h"
#include "inst.h"

namespace absyntax {
using namespace types;
using trans::access;
using trans::qualifiedAccess;
using trans::action;
using trans::READ;
using trans::WRITE;
using trans::CALL;
using vm::inst;


types::ty *signatureless(types::ty *t) {
  if (overloaded *o=dynamic_cast<overloaded *>(t))
    return o->signatureless();
  else
    return (t && !t->getSignature()) ? t : 0;
}


void name::forceEquivalency(action act, coenv &e,
                            types::ty *target, types::ty *source)
{
  if (act == READ)
    e.implicitCast(getPos(), target, source);
  else if (!equivalent(target, source)) {
    em.compiler(getPos());
    em << "type mismatch in variable: "
       << *target
       << " vs " << *source;
  }
}

frame *name::frameTrans(coenv &e)
{
  if (types::ty *t=signatureless(varGetType(e))) {
    if (t->kind == types::ty_record) {
      varTrans(READ, e, t);
      return ((record *)t)->getLevel();
    }
    else
      return 0;
  }
  else
    return tyFrameTrans(e);
}
    

types::ty *name::getType(coenv &e, bool tacit)
{
  types::ty *t=signatureless(varGetType(e));
  if (!tacit && t && t->kind == ty_error)
    // Report errors associated with regarding the name as a variable.
    varTrans(trans::READ, e, t);
  return t ? t : typeTrans(e, tacit);
}


varEntry *simpleName::getVarEntry(coenv &e)
{
  types::ty *t=signatureless(varGetType(e));
  return t ? e.e.lookupVarByType(id, t) : 0;
}
  
void simpleName::varTrans(action act, coenv &e, types::ty *target)
{
  varEntry *v = e.e.lookupVarByType(id, target);
  
  if (v) {
    v->encode(act, getPos(), e.c);
    forceEquivalency(act, e, target, v->getType());
  }
  else {
    em.error(getPos());
    em << "no matching variable of name \'" << id << "\'";
  }
}

types::ty *simpleName::varGetType(coenv &e)
{
  return e.e.varGetType(id);
}

trans::varEntry *simpleName::getCallee(coenv &e, signature *sig)
{
  varEntry *ve = e.e.lookupVarBySignature(id, sig);
  return ve;
}

types::ty *simpleName::typeTrans(coenv &e, bool tacit)
{
  types::ty *t = e.e.lookupType(id);
  if (t) {
    return t;
  }
  else {
    if (!tacit) {
      em.error(getPos());
      em << "no type of name \'" << id << "\'";
    }
    return primError();
  }
}

tyEntry *simpleName::tyEntryTrans(coenv &e)
{
  tyEntry *ent = e.e.lookupTyEntry(id);
  if (!ent) {
    em.error(getPos());
    em << "no type of name \'" << id << "\'";
    return new tyEntry(primError(), 0, 0, position());
  }
  return ent;
}

frame *simpleName::tyFrameTrans(coenv &e)
{
  tyEntry *ent = e.e.lookupTyEntry(id);
  if (ent && ent->t->kind==types::ty_record && ent->v) {
    ent->v->encode(READ, getPos(), e.c);
    return ent->v->getLevel();
  }
  else 
    return 0;
}

void simpleName::prettyprint(ostream &out, Int indent)
{
  prettyindent(out, indent);
  out << "simpleName '" << id << "'\n";
}


record *qualifiedName::castToRecord(types::ty *t, bool tacit)
{
  switch (t->kind) {
    case ty_overloaded:
      if (!tacit) {
        em.compiler(qualifier->getPos());
        em << "name::getType returned overloaded";
      }
      return 0;
    case ty_record:
      return (record *)t;
    case ty_error:
      return 0;
    default:
      if (!tacit) {
        em.error(qualifier->getPos());
        em << "type \'" << *t << "\' is not a structure";
      }
      return 0;
  }
}

bool qualifiedName::varTransVirtual(action act, coenv &e,
                                    types::ty *target, types::ty *qt)
{
  varEntry *v = qt->virtualField(id, target->getSignature());
  if (v) {
    // Push qualifier onto stack.
    qualifier->varTrans(READ, e, qt);

    v->encode(act, getPos(), e.c);

    // A virtual field was used.
    return true;
  }

  // No virtual field.
  return false;
}

void qualifiedName::varTransField(action act, coenv &e,
                                  types::ty *target, record *r)
{
  varEntry *v = r->e.lookupVarByType(id, target);

  if (v) {
    frame *f = qualifier->frameTrans(e);
    if (f)
      v->encode(act, getPos(), e.c, f);
    else
      v->encode(act, getPos(), e.c);

    forceEquivalency(act, e, target, v->getType());
  }
  else {
    em.error(getPos());
    em << "no matching field of name \'" << id << "\' in \'" << *r << "\'";
  }
}

void qualifiedName::varTrans(action act, coenv &e, types::ty *target)
{
  types::ty *qt = qualifier->getType(e);

  // Use virtual fields if applicable.
  if (varTransVirtual(act, e, target, qt))
    return;

  record *r = castToRecord(qt);
  if (r)
    varTransField(act, e, target, r);
}

types::ty *qualifiedName::varGetType(coenv &e)
{
  types::ty *qt = qualifier->getType(e, true);

  // Look for virtual fields.
  types::ty *t = qt->virtualFieldGetType(id);
  if (t)
    return t;

  record *r = castToRecord(qt, true);
  return r ? r->e.varGetType(id) : 0;
}

trans::varEntry *qualifiedName::getCallee(coenv &e, signature *sig)
{
  // getTypeAsCallee is an optimization attempt.  We don't try optimizing the
  // rarer qualifiedName call case.
  // TODO: See if this is worth implementing.
  //cout << "FAIL BY QUALIFIED NAME" << endl;
  return 0;
}

trans::varEntry *qualifiedName::getVarEntry(coenv &e)
{
  varEntry *qv = qualifier->getVarEntry(e);

  types::ty *qt = qualifier->getType(e, true);
  record *r = castToRecord(qt, true);
  if (r) {
    types::ty *t = signatureless(r->e.varGetType(id));
    varEntry *v = t ? r->e.lookupVarByType(id, t) : 0;
    return trans::qualifyVarEntry(qv,v);
  }
  else
    return qv;
}

types::ty *qualifiedName::typeTrans(coenv &e, bool tacit)
{
  types::ty *rt = qualifier->getType(e, tacit);

  record *r = castToRecord(rt, tacit);
  if (!r)
    return primError();

  tyEntry *ent = r->e.lookupTyEntry(id);
  if (ent) {
    if (!tacit)
      ent->reportPerm(READ, getPos(), e.c);
    return ent->t;
  }
  else {
    if (!tacit) {
      em.error(getPos());
      em << "no matching field or type of name \'" << id << "\' in \'"
         << *r << "\'";
    }
    return primError();
  }
}

tyEntry *qualifiedName::tyEntryTrans(coenv &e)
{
  types::ty *rt = qualifier->getType(e, false);

  record *r = castToRecord(rt, false);
  if (!r)
    return new tyEntry(primError(), 0, 0, position());

  tyEntry *ent = r->e.lookupTyEntry(id);
  if (!ent) {
    em.error(getPos());
    em << "no matching type of name \'" << id << "\' in \'"
       << *r << "\'";
    return new tyEntry(primError(), 0, 0, position());
  }
  ent->reportPerm(READ, getPos(), e.c);

  return trans::qualifyTyEntry(qualifier->getVarEntry(e), ent);
}

frame *qualifiedName::tyFrameTrans(coenv &e)
{
  frame *f=qualifier->frameTrans(e);
  tyEntry *ent = e.e.lookupTyEntry(id);
  if (ent && ent->t->kind==types::ty_record && ent->v) {
    if (f)
      ent->v->encode(READ, getPos(), e.c, f);
    else
      ent->v->encode(READ, getPos(), e.c);
    return ent->v->getLevel();
  }
  else 
    return f;
}

void qualifiedName::prettyprint(ostream &out, Int indent)
{
  prettyindent(out, indent);
  out << "qualifiedName '" << id << "'\n";

  qualifier->prettyprint(out, indent+1);
}

} // namespace absyntax