File: OTMMethod.m

package info (click to toggle)
tom 1.1.1-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,340 kB
  • ctags: 2,244
  • sloc: objc: 27,863; ansic: 9,804; sh: 7,411; yacc: 3,377; lex: 966; asm: 208; makefile: 62; cpp: 10
file content (715 lines) | stat: -rw-r--r-- 14,739 bytes parent folder | download
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
   Written by Pieter J. Schoenmakers <tiggr@ics.ele.tue.nl>

   Copyright (C) 1996-1998 Pieter J. Schoenmakers.

   This file is part of TOM.  TOM is distributed under the terms of the
   TOM License, a copy of which can be found in the TOM distribution; see
   the file LICENSE.

   $Id: OTMMethod.m,v 1.74 1998/03/22 18:39:02 tiggr Exp $  */

#define OTMMETHOD_DECLARE_PRIVATE_METHODS
#import "OTMMethod.h"
#import "OTMArgument.h"
#import "OTMBasic.h"
#import "OTMDynamicType.h"
#import "OTMExtension.h"
#import "OTMInvocation.h"
#import "OTMMeta.h"
#import "OTMRefVar.h"
#import "OTMTuple.h"
#import "OTMType.h"
#import "OTMTypeTuple.h"
#import "OTMVariable.h"

@implementation OTMMethod

+(OTMMethod *) methodWithExtension: (OTMExtension *) ext
			      name: (TLString *) n
			returnType: (id) rt
			     flatp: (BOOL) flatp
{
  return [[self gcAlloc] initWithExtension: ext name: n returnType: rt
			 flatp: flatp];
}

+(OTMMethod *) methodWithExtension: (OTMExtension *) ext
			 nameTypes: (TLCons *) nt
			returnType: (id) rt
			     flatp: (BOOL) flatp
{
  return [[self gcAlloc] initWithExtension: ext nameTypes: nt returnType: rt
			 flatp: flatp];
}

+(LTTSelector *) selectorForMethod: (OTMMethod *) m
			invocation: (OTMInvocation *) inv
			      used: (BOOL) used_p
{
  TLVector *ne = [m nameParts];
  id on, in_args = nil;
  OTMType *rt = inv ? [inv type] : [m returnType];
  LTTSelector *rs;

  on = formac (nil, @"%@", [rt frobnicatedName]);

  if (!ne)
    formac (on, @"_%@", [m methodName]);
  else
    {
      TLVector *a = [m arguments];
      int i, n = [ne length];
      int skip = [m implicitArguments];

      for (i = 0; i < n; i++)
	{
	  OTMType *t = [[a _elementAtIndex: i + skip] type];

	  if (t == the_dynamic_type && inv)
	    t = [[[inv arguments] _elementAtIndex: i] type];

	  if (t != basic_type[BT_VOID])
	    in_args = formac (in_args, @"%@", [t flatFrobnicatedName]);
	  formac (on, @"_%@_%@", [ne _elementAtIndex: i], [t frobnicatedName]);
	}
    }

  rs = [CO_LTTSelector selectorWithName: on
		    inArgs: (in_args ? in_args : @"")
		    outArgs: rt ? [rt flatFrobnicatedName] : @""];

  if (used_p)
    [rs noteUsage];

  return rs;
}

-(void) addOutArgsFrom: (OTMTypeTuple *) tup
		  skip: (BOOL) skip
{
  TLVector *v = [tup elements];
  int mi, mn;

  for (mi = 0, mn = [v length]; mi < mn; mi++)
    {
      OTMType *tp = [v _elementAtIndex: mi];

      if ([tp isTuple])
	[self addOutArgsFrom: (OTMTypeTuple *) tp skip: skip];
      else if (!skip)
	{
	  OTMRefVar *v = [CO_OTMRefVar temporaryVariableWithType: tp];

	  [out_args addElement: v];
	}
      skip = NO;
    }
}

-(BOOL) allowedRedeclaration: (OTMMethod *) m
		  inSubclass: (BOOL) subclass_p
{
  TLVector *others = [m arguments];
  int i, n = [others length];

  if (n != [arguments length])
    return NO;

  for (i = 0; i < n; i++)
    if (![[(OTMVariable *) [arguments _elementAtIndex: i] type]
	  allowedTypeForArgumentRedeclaration:
	    [(OTMVariable *) [others _elementAtIndex: i] type]
	  inSubclass: subclass_p])
      return NO;

  return YES;
}

-(OTMExpr *) argumentDefaultAt: (int) index
{
  return [arg_values _elementAtIndex: index];
}

-(TLVector *) argumentsForNameParts: (TLVector *) name_parts
			  arguments: (TLVector *) args
{
  int en = name_elements ? [name_elements length] : 0;
  int pi, ei, pn = [name_parts length];
  TLVector *r = [CO_TLVector vectorWithCapacity: en];
  id npn = [name_parts _elementAtIndex: 0];
  int ia = [self implicitArguments];

  for (ei = pi = 0; ei < en; ei++)
    {
      id npe = [name_elements _elementAtIndex: ei];

      if (npe == npn)
	{
	  [r addElement: [args _elementAtIndex: pi]];
	  npn = (++pi == pn) ? nil : [name_parts _elementAtIndex: pi];
	}
      else
	{
	  id av = [arg_values _elementAtIndex: ei + ia];

	  if (!av)
	    ABORT ();
	  else
	    {
	      if (![av type])
		resolve_expr (av, nil, nil,
			      [[[extension structure] meta] semantics]);
	      [r addElement: av];
	    }
	}
    }

  return r;
}

-(void) assignOutArgsFrom: (OTMTuple *) tup
{
  id <TLEnumerator> e = [out_args enumerator];

  [self assignOutArgsFrom: tup to: e skip: YES];
}

-(void) assignOutArgsFrom: (OTMTuple *) tup
		       to: (id <TLEnumerator>) to
		     skip: (BOOL) skip
{
  TLVector *v = [tup elements];
  int mi, mn;

  for (mi = 0, mn = [v length]; mi < mn; mi++)
    {
      OTMExpr *tp = [v _elementAtIndex: mi];

      if ([tp isTuple])
	[self assignOutArgsFrom: (OTMTuple *) tp to: to skip: skip];
      else if (!skip)
	{
	  OTMRefVar *rv = [to nextObject];

	  [rv compileAssignment: tp];
	}
      skip = NO;
    }
}

-(OTMVariable *) argumentNamed: (id <TLString>) nm
{
  int i, n = [arguments length];

  for (i = 0; i < n; i++)
    {
      id t = [arguments _elementAtIndex: i];

      if ([t isTuple])
	{
	  TLVector *vec = [t elements];
	  int j, o = [vec length];
	  OTMVariable *v;

	  for (j = 0; j < o; j++)
	    {
	      v = [vec _elementAtIndex: j];
	      if ([(id) nm equal: [v variableName]])
		return v;
	    }
	}
      else if ([(id) nm equal: [(OTMVariable *) t variableName]])
	return t;
    }

  return nil;
}

-(OTMArgument *) argumentNumbered: (int) x
{
  int i, n = [arguments length];

  for (i = 0; i < n; i++)
    {
      OTMArgument *a = [arguments _elementAtIndex: i];
      a = [a argumentNumbered: x];
      if (a)
	return a;
    }

  return nil;
}

-(TLVector *) arguments
{
  return arguments;
}

-(BOOL) argumentsTypeMatch: (TLVector *) a
{
  int i, n = [a length], offset = 0, o;
  int skip = [self implicitArguments];

  if (n != [arguments length] - skip)
    return NO;

  for (i = 0; i < n; i++)
    {
      o = [[(OTMVariable *) [arguments _elementAtIndex: i + skip] type]
	   matchesConvertibly: [[(OTMVariable *) [a _elementAtIndex: i] type]
				actualSelf: [current_either semantics]]];
      if (o < 0)
	return NO;
      offset += o;
    }

  return YES;
}

-(BOOL) builtinp
{
  return NO;
}

-(void) compileDeclarationTypes
{
  int i, n;

  [return_type compileDeclaration];

  for (i = 0, n = [arguments length]; i < n; i++)
    {
      OTMVariable *e = [arguments _elementAtIndex: i];

      [[e type] compileDeclaration];
    }

  if (out_args)
    for (i = 0, n = [out_args length]; i < n; i++)
      {
	OTMVariable *e = [out_args _elementAtIndex: i];

	[[e type] compileDeclaration];
      }
}

-(void) compileDeclaration
{
  int i, na = [arguments length];
  id previous_context = output_current_context;
  id previous_method = output_current_method;

  output_current_context = [[extension structure] meta];
  output_current_method = (id) self;

  [self compileDeclarationTypes];

  /* Output the return type.  */
  formac (of, @"%@\n", [[return_type tupleFirstFlatElement] outputTypeName]);

  /* Output the name.  */
  formac (of, @"%@ (", [self outputName]);

  if (!na)
    formac (of, @"void)\n");
  else
    {
      /* Output the arguments.  */
      for (i = 0; i < na; i++)
	{
	  OTMArgument *arg = [arguments _elementAtIndex: i];

	  if (i)
	    formac (of, @",\n\t");

	  if ([arg type] == the_dynamic_type)
	    {
	      formac (of, @"...");
	      break;
	    }

	  [arg outputDeclaration: of];
	}

      /* Output the second and following tuple return value elements.  */
      if (out_args && i == na)
	{
	  int no = [out_args length];

	  for (i = 0; i < no; i++)
	    {
	      if (i || na)
		formac (of, @",\n\t");
	      [[out_args _elementAtIndex: i] outputDeclaration: of];
	    }
	}

      /* Done.  */
      formac (of, @")");
    }

  output_current_context = previous_context;
  output_current_method = previous_method;
}

-(void) description: (id <TLMutableStream>) stream
{
  [super description: stream];

  formac (stream, @" %@", method_name (self, 0));
}

-(void) dumpInfo: (id <TLOutputStream>) s
{
  if ([self deferredp])
    formac (s, @"\n  (%#)", [[[self selector] lttName] internal]);
  else
    formac (s, @"\n  (%# %@)", [[[self selector] lttName] internal],
	    [self outputName]);
}

-(BOOL) dynamicTyped
{
  return dynamic_typing;
}

-(OTMExtension *) extension
{
  return extension;
}

-(BOOL) fitsInvocationNumArguments: (int) n
{
  int i = [self implicitArguments];
  int num_args = [arguments length];

  if (num_args - i < n
      || num_fixed_arguments - i > n)
    return NO;

  return YES;
}

-(id <TLString>) firstNamePart
{
  return name_elements ? [name_elements _elementAtIndex: 0] : internal_name;
}

-(void) gcReference
{
  MARK (extension);
  MARK (return_type);
  MARK (arguments);
  MARK (arg_values);
  MARK (out_args);
  MARK (name_elements);
  MARK (internal_name);
  MARK (the_selector);
  MARK (output_name);

  [super gcReference];
}

-(BOOL) identical: m inContext: (OTMMeta *) either
{
  OTMType *mrt = [[m returnType] actualSelf: either];
  int i, impl, impl2, num_args;
  TLVector *v;

  if (![return_type matches: mrt])
    return NO;

  num_args = [arguments length];
  v = [m arguments];
  if (num_args != [v length])
    return NO;

  impl = [self implicitArguments];
  impl2 = [m implicitArguments];
  for (i = 0; i < num_args - impl; i++)
    // Used to use `matches:', but that is wrong, sort of...
    // Thu Aug  8 13:58:41 1996, tiggr@cobra.es.ele.tue.nl
    if ([[[v _elementAtIndex: i + impl2] type] actualSelf: either]
	!= [[[arguments _elementAtIndex: i + impl] type] actualSelf: either])
      return NO;

  return YES;
}

-(int) implicitArguments
{
  return flat ? 0 : 2;
}

-initWithExtension: (OTMExtension *) ext
	returnType: (id) rt
	     flatp: (BOOL) flatp
{
  if (![super init])
    return nil;

  extension = ext;
  arguments = [CO_TLVector vector];
  arg_values = [CO_TLVector vector];
  return_type = rt;

  if ([return_type isTuple])
    {
      out_args = [CO_TLVector vector];
      [self addOutArgsFrom: (OTMTypeTuple *) return_type skip: YES];
    }
  else if (return_type == the_dynamic_type)
    dynamic_typing = 1;

  if (flatp)
    flat = YES;
  else
    {
      [arg_values addElement: nil];
      [arguments addElement: [CO_OTMArgument variableWithName: TO_NAME_SELF
					     type: basic_type[BT_RECV]
					     number: 0]];
      [arg_values addElement: nil];
      [arguments addElement: [CO_OTMArgument variableWithName: TO_NAME_CMD
					     type: basic_type[BT_SELECTOR]
					     number: 1]];
      num_fixed_arguments = 2;
    }

  [isa selectorForMethod: self invocation: nil used: NO];

  return self;
}

-initWithExtension: (OTMExtension *) ext
	      name: (TLString *) n
	returnType: (id) rt
	     flatp: (BOOL) flatp
{
  if (![self initWithExtension: ext returnType: rt flatp: flatp])
    return nil;

  internal_name = n;

  return self;
}

-initWithExtension: (OTMExtension *) ext
	 nameTypes: (TLCons *) nt
	returnType: (id) rt
	     flatp: (BOOL) flatp
{
  TLString *np;
  BOOL first = YES;

  if (![self initWithExtension: ext returnType: rt flatp: flatp])
    return nil;

  name_elements = [CO_TLVector vectorWithCapacity: 1];

  while (nt)
    {
      TLCons *o, *av;
      OTMTuple *vp, *val;
      
      DECONS (nt, o, nt);
      DECONS (o, np, av);
      DECONS (av, vp, val);

      if (np)
	{
	  [name_elements addElement: np];
	  internal_name = formac (internal_name,
				  internal_name ? @" %@" : @"%@", np);
	}

      if ([vp checkProperArgument: self])
	dynamic_typing = 1;

      if (first)
	{
	  if (val)
	    {
	      error_for (self, @"optional first argument not allowed");
	      val = nil;
	    }
	  first = NO;
	}

      [arguments addElement: vp];
      [arg_values addElement: val];
      if (!val)
	num_fixed_arguments++;
    }

  [isa selectorForMethod: self invocation: nil used: NO];

  return self;
}

-(id <TLString>) methodName
{
  return internal_name;
}

-(TLVector *) nameParts
{
  return name_elements;
}

-(BOOL) namePartsMatch: (TLVector *) name_parts
{
  int pn = [name_parts length];

  if (!name_elements)
    return ([name_parts length] == 1
	    && internal_name == [name_parts _elementAtIndex: 0]);

  {
    int en = [name_elements length], ei, pi, ia;
    id npn;

    if (en < pn)
      return NO;

    ia = [self implicitArguments];
    npn = [name_parts _elementAtIndex: 0];
    for (ei = pi = 0; ei < en; ei++)
      {
	id npe = [name_elements _elementAtIndex: ei];

	if (npe == npn)
	  npn = (++pi == pn) ? nil : [name_parts _elementAtIndex: pi];
	else if (![arg_values _elementAtIndex: ei + ia])
	  return NO;
      }

    return !npn;
  }
}

-(int) numExplicitArguments
{
  return [arguments length] - [self implicitArguments];
}

-(id <TLString>) outputName
{
  if (!output_name)
    output_name = formac (nil, @"%@_%@", [[extension structure] outputName],
			  [[[self selector] lttName] external]);

  return output_name;
}

-(id) precompile
{
  int i, n = [arguments length];

  for (i = 0; i < n; i++)
    {
      OTMArgument *a = [arguments _elementAtIndex: i];
      OTMType *t = [a type];
      [t precompile];
    }

  return self;
}

-(void) resolveIdentifiers: (OTMMeta *) meta
{
  int i, n = [arg_values length];

  for (i = 0; i < n; i++)
    {
      OTMExpr *o, *p = [arg_values _elementAtIndex: i];

      if (p)
	{
	  o = [p resolveInContext: meta];
	  if (o != p)
	    [arg_values _replaceElementAtIndex: i by: o];
	}
    }
}

-(OTMType *) returnType
{
  return return_type;
}

-(OTMVariable *) searchVariableNamed: (id <TLString>) n
{
  return [self argumentNamed: n];
}

-(LTTSelector *) selector
{
  if (!the_selector)
    the_selector = [isa selectorForMethod: self invocation: nil used: YES];

  return the_selector;
}

-(void) setHaveStackReferences: (OTMVariable *) v
{
}

-(OTMType *) typeForArgumentAtIndex: (int) i
			  inContext: (OTMMeta *) either
		       withNamePart: (TLString *) np
{
  int n = [name_elements length];

  for (; i < n; i++)
    if ([name_elements _elementAtIndex: i] == np)
      return [[[arguments _elementAtIndex: i + [self implicitArguments]]
	       type] actualSelf: either];

  ABORT ();

  return nil;
}

-(BOOL) typesMatch: (OTMMethod *) m
{
  int i, n, skip_self, skip_other;
  TLVector *others;

  if (![return_type matches: [m returnType]])
    return NO;

  skip_self = [self implicitArguments];
  skip_other = [m implicitArguments];
  others = [m arguments];
  n = [others length] - skip_other;
  if (n != [arguments length] - skip_self)
    return NO;

  for (i = 0; i < n; i++)
    if (![[(OTMVariable *) [arguments _elementAtIndex: i + skip_self] type]
	  matches: [(OTMVariable *) [others _elementAtIndex: i + skip_other]
		    type]])
      return NO;

  return YES;
}

-(void) warnDifferingArguments: (OTMMethod *) orig
{
  /* XXX */
}

-(id) resultOfInvocation: (OTMInvocation *) inv
{
  [self subclassResponsibility: _cmd];
}

-(id) resultOfInvocation: (OTMInvocation *) inv
		 toTuple: (OTMTuple *) tup
{
  [self subclassResponsibility: _cmd];
}

@end