summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/entry.cc
blob: 93fb999705433d7ef57e79bef671608ee77a9f3e (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*****
 * entry.cc
 * Andy Hammerlindl 2002/08/29
 *
 * All variables, built-in functions and user-defined functions reside
 * within the same namespace.  To keep track of all these, table of
 * "entries" is used.
 *****/

#include <iostream>

#include <cmath>
#include <utility>
#include "entry.h"
#include "coder.h"

using types::signature;

namespace trans {

bool entry::pr::check(action act, coder &c) {
  // We assume PUBLIC permissions and one's without an associated record are not
  // stored.
  assert(perm!=PUBLIC && r!=0);
  return c.inTranslation(r->getLevel()) ||
    (perm == RESTRICTED && act != WRITE);
}

void entry::pr::report(action act, position pos, coder &c) {
  if (!c.inTranslation(r->getLevel())) {
    if (perm == PRIVATE) {
      em.error(pos);
      em << "accessing private field outside of structure";
    }
    else if (perm == RESTRICTED && act == WRITE) {
      em.error(pos);
      em << "modifying non-public field outside of structure";
    }
  }
}

entry::entry(entry &e1, entry &e2) : where(e2.where), pos(e2.pos) {
  perms.insert(perms.end(), e1.perms.begin(), e1.perms.end());
  perms.insert(perms.end(), e2.perms.begin(), e2.perms.end());
}

entry::entry(entry &base, permission perm, record *r)
  : where(base.where), pos(base.pos) {
  perms.insert(perms.end(), base.perms.begin(), base.perms.end());
  addPerm(perm, r);
}

bool entry::checkPerm(action act, coder &c) {
  for (mem::list<pr>::iterator p=perms.begin(); p != perms.end(); ++p)
    if (!p->check(act, c))
      return false;
  return true;
}

void entry::reportPerm(action act, position pos, coder &c) {
  for (mem::list<pr>::iterator p=perms.begin(); p != perms.end(); ++p)
    p->report(act, pos, c);
}


varEntry::varEntry(varEntry &qv, varEntry &v)
  : entry(qv,v), t(v.t),
    location(new qualifiedAccess(qv.location, qv.getLevel(), v.location)) {}

frame *varEntry::getLevel() {
  record *r=dynamic_cast<record *>(t);
  assert(r);
  return r->getLevel();
}

void varEntry::encode(action act, position pos, coder &c) {
  reportPerm(act, pos, c);
  getLocation()->encode(act, pos, c);
}

void varEntry::encode(action act, position pos, coder &c, frame *top) {
  reportPerm(act, pos, c);
  getLocation()->encode(act, pos, c, top);
}

varEntry *qualifyVarEntry(varEntry *qv, varEntry *v)
{
  return qv ? (v ? new varEntry(*qv,*v) : qv) : v;
}


bool tenv::add(symbol dest,
               names_t::value_type &x, varEntry *qualifier, coder &c)
{
  if (!x.second.empty()) {
    tyEntry *ent=x.second.front();
    if (ent->checkPerm(READ, c)) {
      enter(dest, qualifyTyEntry(qualifier, ent));
      return true;
    }
  }
  return false;
}

void tenv::add(tenv& source, varEntry *qualifier, coder &c) {
  // Enter each distinct (unshadowed) name,type pair.
  for(names_t::iterator p = source.names.begin(); p != source.names.end(); ++p)
    add(p->first, *p, qualifier, c);
}

bool tenv::add(symbol src, symbol dest,
               tenv& source, varEntry *qualifier, coder &c) {
  names_t::iterator p = source.names.find(src);
  if (p != source.names.end())
    return add(dest, *p, qualifier, c);
  else
    return false;
}

#ifdef NOHASH //{{{
void venv::add(venv& source, varEntry *qualifier, coder &c)
{
  // Enter each distinct (unshadowed) name,type pair.
  for(names_t::iterator p = source.names.begin();
      p != source.names.end();
      ++p)
    add(p->first, p->first, source, qualifier, c);
}

bool venv::add(symbol src, symbol dest,
               venv& source, varEntry *qualifier, coder &c)
{
  bool added=false;
  name_t &list=source.names[src];
  types::overloaded set; // To keep track of what is shadowed.

  for(name_iterator p = list.begin();
      p != list.end();
      ++p) {
    varEntry *v=*p;
    if (!equivalent(v->getType(), &set)) {
      set.addDistinct(v->getType(), src->special());
      if (v->checkPerm(READ, c)) {
        enter(dest, qualifyVarEntry(qualifier, v));
        added=true;
      }
    }
  }
  
  return added;
}

varEntry *venv::lookByType(symbol name, ty *t)
{
  // Find first applicable function.
  name_t &list = names[name];
  for(name_iterator p = list.begin();
      p != list.end();
      ++p) {
    if (equivalent((*p)->getType(), t))
      return *p;
  }
  return 0;
}

void venv::list(record *module)
{
  bool where=settings::getSetting<bool>("where");
  // List all functions and variables.
  for(names_t::iterator N = names.begin(); N != names.end(); ++N) {
    symbol s=N->first;
    name_t &list=names[s];
    for(name_iterator p = list.begin(); p != list.end(); ++p) {
      if(!module || (*p)->whereDefined() == module) {
        if(where) cout << (*p)->getPos();
        (*p)->getType()->printVar(cout, s);
        cout << ";\n";
      }
    }
  }
  flush(cout);
}

ty *venv::getType(symbol name)
{
  types::overloaded set;

  // Find all applicable functions in scope.
  name_t &list = names[name];
  
  for(name_iterator p = list.begin();
      p != list.end();
      ++p) {
    set.addDistinct((*p)->getType(), name->special());
  }

  return set.simplify();
}
// }}}
#else // {{{

ostream& operator<< (ostream& out, const venv::key &k) {
  k.t->printVar(out, k.name);
  return out;
}

#if TEST_COLLISION
bool venv::keyeq::operator()(const key k, const key l) const {
  keyhash kh;
  if (kh(k)==kh(l)) {
    if (base(k,l))
      return true;
    else {
      cerr << "collision: " << endl;
      cerr << "  " << k << " -> " << kh(k) << endl;
      cerr << "  " << l << " -> " << kh(l) << endl;
    }
  }
  return false;
}
#else
bool venv::keyeq::operator()(const key k, const key l) const {
#if 0
  cerr << "k.t = " << k.t << " " << k << endl;
  cerr << "l.t = " << l.t << " " << l << endl;
#endif
  return k.name==l.name &&
    (k.name.special() ? equivalent(k.t, l.t) :
     equivalent(k.t->getSignature(),
                l.t->getSignature()));
}
#endif

void venv::remove(key k) {
  //cerr << "removing: " << k << endl;
  value *&val=all[k];
  assert(val);
  if (val->next) {
    val->next->shadowed=false;
    value *temp=val->next;
    val->next=0;
    val=temp;
  }
  else
    all.erase(k);

  // Don't erase it from scopes.top() as that will be popped of the stack at
  // the end of endScope anyway.

  names[k.name].pop_front();
}

#ifdef CALLEE_SEARCH
size_t numArgs(ty *t) {
  signature *sig = t->getSignature();
  return sig ? sig->getNumFormals() : 0;
}

void checkMaxArgs(venv *ve, symbol name, size_t expected) {
  size_t maxFormals = 0;
  ty *t = ve->getType(name);
  if (types::overloaded *o=dynamic_cast<types::overloaded *>(t)) {
    for (types::ty_vector::iterator i=o->sub.begin(); i != o->sub.end(); ++i)
    {
      size_t n = numArgs(*i);
      if (n > maxFormals)
        maxFormals = n;
    }
  } else {
    maxFormals = numArgs(t);
  }
  if (expected != maxFormals) {
    cout << "expected: " << expected << " computed: " << maxFormals << endl;
    cout << "types: " << endl;
    if (types::overloaded *o=dynamic_cast<types::overloaded *>(t)) {
      cout << " overloaded" << endl;
      for (types::ty_vector::iterator i=o->sub.begin(); i != o->sub.end();
          ++i)
      {
        cout << "  " << **i << endl;
      }
    } else {
      cout << " non-overloaded" << endl;
      cout << "  " << *t << endl;
    }
    cout.flush();
  }
  assert(expected == maxFormals);
}
#endif

void venv::enter(symbol name, varEntry *v) {
  assert(!scopes.empty());
  key k(name, v);
#ifdef DEBUG_ENTRY
  cout << "entering: " << k << " (t=" << k.t << ")" << endl;
#endif
  value *val=new value(v);

#ifdef DEBUG_ENTRY
  keymap::iterator p=all.find(k);
  if (p!=all.end()) {
    cout << "  over: " << p->first << endl;
  }
#endif
  
  val->next=all[k];
  if (val->next)
    val->next->shadowed=true;

  all[k]=val;
  scopes.top().insert(keymultimap::value_type(k,val));

#ifdef CALLEE_SEARCH
  // I'm not sure if this works properly with rest arguments.
  signature *sig = v->getSignature();
  size_t newmax = sig ? sig->getNumFormals() : 0;
  mem::list<value *>& namelist = names[k.name];
  if (!namelist.empty()) {
    size_t oldmax = namelist.front()->maxFormals;
    if (oldmax > newmax)
      newmax = oldmax;
  }
  val->maxFormals = newmax;
  namelist.push_front(val);

  // A sanity check, disabled for speed reasons.
#ifdef DEBUG_CACHE
  checkMaxArgs(this, k.name, val->maxFormals);
#endif
#else
  names[k.name].push_front(val);
#endif
}


varEntry *venv::lookBySignature(symbol name, signature *sig) {
#ifdef CALLEE_SEARCH
  // Rest arguments are complicated and rare.  Don't handle them here.
  if (sig->hasRest()) {
#if 0
    if (lookByType(key(name, sig)))
      cout << "FAIL BY REST ARG" << endl;
    else
      cout << "FAIL BY REST ARG AND NO-MATCH" << endl;
#endif
    return 0;
  }

  // Likewise with the special operators.
  if (name.special()) {
    //cout << "FAIL BY SPECIAL" << endl;
    return 0;
  }

  mem::list<value *>& namelist = names[name];
  if (namelist.empty()) {
    // No variables of this name.
    //cout << "FAIL BY EMPTY" << endl;
    return 0;
  }

  // Avoid ambiguities with default parameters.
  if (namelist.front()->maxFormals != sig->getNumFormals()) {
#if 0
    if (lookByType(key(name, sig)))
      cout << "FAIL BY NUMARGS" << endl;
    else
      cout << "FAIL BY NUMARGS AND NO-MATCH" << endl;
#endif
    return 0;
  }

  // At this point, any function with an equivalent an signature will be equal
  // to the result of the normal overloaded function resolution.  We may
  // safely return it.
  varEntry *result = lookByType(key(name, sig));
#if 0
  if (!result)
    cout << "FAIL BY NO-MATCH" << endl;
#endif
  return result;
#else
  // The maxFormals field is necessary for this optimization.
  return 0;
#endif
}

void venv::add(venv& source, varEntry *qualifier, coder &c)
{
  // Enter each distinct (unshadowed) name,type pair.
  for(keymap::iterator p = source.all.begin(); p != source.all.end(); ++p) {
    varEntry *v=p->second->v;
    if (v->checkPerm(READ, c))
      enter(p->first.name, qualifyVarEntry(qualifier, v));
  }
}

bool venv::add(symbol src, symbol dest,
               venv& source, varEntry *qualifier, coder &c)
{
  bool added=false;
  values &list=source.names[src];

  for (values::iterator p=list.begin(); p!=list.end(); ++p)
    if (!(*p)->shadowed) {
      varEntry *v=(*p)->v;
      if (v->checkPerm(READ, c)) {
        enter(dest, qualifyVarEntry(qualifier, v));
        added=true;
      }
    }

  return added;
}


ty *venv::getType(symbol name)
{
#if 0
  cout << "getType: " << name << endl;
#endif
  types::overloaded set;
  values &list=names[name];

  for (values::iterator p=list.begin(); p!=list.end(); ++p)
    if (!(*p)->shadowed)
      set.add((*p)->v->getType());

  return set.simplify();
}

void venv::listValues(symbol name, values &vals, record *module)
{
  ostream& out=cout;

  bool where=settings::getSetting<bool>("where");
  for(values::iterator p = vals.begin(); p != vals.end(); ++p) {
    if(!module || (*p)->v->whereDefined() == module) {
      if(where) out << (*p)->v->getPos();
      if ((*p)->shadowed)
        out << "  <shadowed> ";
      (*p)->v->getType()->printVar(out, name);
      out << ";\n";
    }
  }
  flush(out);
}

void venv::list(record *module)
{
  // List all functions and variables.
  for(namemap::iterator N = names.begin(); N != names.end(); ++N)
    listValues(N->first, N->second,module);
}

void venv::completions(mem::list<symbol >& l, string start)
{
  for(namemap::iterator N = names.begin(); N != names.end(); ++N)
    if (prefix(start, N->first) && !N->second.empty())
      l.push_back(N->first);
}

#endif // }}}

} // namespace trans