aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/turbine/binder/TypeBinder.java
blob: 219bc1fee73f4f080218225bb7fe3f8e5c3672f0 (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
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
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.turbine.binder;

import static com.google.common.base.Verify.verify;

import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.turbine.binder.bound.HeaderBoundClass;
import com.google.turbine.binder.bound.SourceHeaderBoundClass;
import com.google.turbine.binder.bound.SourceTypeBoundClass;
import com.google.turbine.binder.bound.SourceTypeBoundClass.FieldInfo;
import com.google.turbine.binder.bound.SourceTypeBoundClass.MethodInfo;
import com.google.turbine.binder.bound.SourceTypeBoundClass.ParamInfo;
import com.google.turbine.binder.bound.TypeBoundClass.TyVarInfo;
import com.google.turbine.binder.env.Env;
import com.google.turbine.binder.lookup.CompoundScope;
import com.google.turbine.binder.lookup.LookupKey;
import com.google.turbine.binder.lookup.LookupResult;
import com.google.turbine.binder.lookup.Scope;
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.binder.sym.FieldSymbol;
import com.google.turbine.binder.sym.MethodSymbol;
import com.google.turbine.binder.sym.Symbol;
import com.google.turbine.binder.sym.TyVarSymbol;
import com.google.turbine.model.TurbineConstantTypeKind;
import com.google.turbine.model.TurbineFlag;
import com.google.turbine.model.TurbineTyKind;
import com.google.turbine.model.TurbineVisibility;
import com.google.turbine.tree.Tree;
import com.google.turbine.tree.TurbineModifier;
import com.google.turbine.type.Type;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/** Type binding. */
public class TypeBinder {

  /** A scope containing a single {@link Symbol}. */
  private static class SingletonScope implements Scope {

    private final String name;
    private final Symbol sym;

    public SingletonScope(String name, Symbol sym) {
      this.name = name;
      this.sym = sym;
    }

    @Override
    public LookupResult lookup(LookupKey lookup) {
      if (name.equals(lookup.first())) {
        return new LookupResult(sym, lookup);
      }
      return null;
    }
  }

  /** A scope backed by a map of simple names to {@link Symbol}s. */
  private static class MapScope implements Scope {
    private final ImmutableMap<String, ? extends Symbol> tps;

    public MapScope(ImmutableMap<String, ? extends Symbol> tps) {
      this.tps = tps;
    }

    @Override
    public LookupResult lookup(LookupKey lookupKey) {
      return tps.containsKey(lookupKey.first())
          ? new LookupResult(tps.get(lookupKey.first()), lookupKey)
          : null;
    }
  }

  /**
   * A scope containing all symbols in lexically enclosing scopes of a class, including type
   * parameters, and declared and inherited members
   */
  private static class ClassMemberScope implements Scope {
    private final ClassSymbol sym;
    private final Env<HeaderBoundClass> env;

    public ClassMemberScope(ClassSymbol sym, Env<HeaderBoundClass> env) {
      this.sym = sym;
      this.env = env;
    }

    @Override
    public LookupResult lookup(LookupKey lookup) {
      ClassSymbol curr = sym;
      while (curr != null) {
        HeaderBoundClass info = env.get(curr);
        Symbol result = info.typeParameters().get(lookup.first());
        if (result != null) {
          return new LookupResult(result, lookup);
        }
        result = Resolve.resolve(env, curr, lookup.first());
        if (result != null) {
          return new LookupResult(result, lookup);
        }
        curr = info.owner();
      }
      return null;
    }
  }

  /** Creates {@link SourceTypeBoundClass} nodes for a compilation. */
  public static SourceTypeBoundClass bind(
      final Env<HeaderBoundClass> env, ClassSymbol sym, SourceHeaderBoundClass base) {

    // This method uses two scopes. This first one is built up as we process the signature
    // and its elements become visible to subsequent elements (e.g. type parameters can
    // refer to previous declared type parameters, the superclass type can refer to
    // type parameters, ...). A second scope is created for finding methods and fields
    // once the signature is fully determined.
    CompoundScope enclosingScope = base.scope();
    enclosingScope = enclosingScope.append(new ClassMemberScope(base.owner(), env));
    enclosingScope = enclosingScope.append(new SingletonScope(base.decl().name(), sym));

    final ImmutableMap<TyVarSymbol, TyVarInfo> typeParameterTypes =
        bindTyParams(env, base.decl().typarams(), enclosingScope, base.typeParameters());
    enclosingScope = enclosingScope.append(new MapScope(base.typeParameters()));

    Type.ClassTy superClassType;
    switch (base.kind()) {
      case ENUM:
        superClassType =
            new Type.ClassTy(
                ImmutableList.of(
                    new Type.ClassTy.SimpleClassTy(
                        ClassSymbol.ENUM,
                        ImmutableList.of(
                            new Type.ConcreteTyArg(Type.ClassTy.asNonParametricClassTy(sym))))));
        break;
      case ANNOTATION:
        superClassType = Type.ClassTy.asNonParametricClassTy(ClassSymbol.ANNOTATION);
        break;
      case CLASS:
      case INTERFACE:
        if (base.decl().xtnds().isPresent()) {
          superClassType =
              (Type.ClassTy) bindClassTy(env, enclosingScope, base.decl().xtnds().get());
          // Members inherited from the superclass are visible to interface types.
          // (The same is not true for interface types declared before other interface
          // types, at least according to javac.)
          enclosingScope =
              enclosingScope.append(
                  new Scope() {
                    @Override
                    public LookupResult lookup(LookupKey lookup) {
                      ClassSymbol result = Resolve.resolve(env, base.superclass(), lookup.first());
                      if (result != null) {
                        return new LookupResult(result, lookup);
                      }
                      return null;
                    }
                  });
        } else {
          superClassType = Type.ClassTy.OBJECT;
        }
        break;
      default:
        throw new AssertionError(base.decl().tykind());
    }

    ImmutableList.Builder<Type.ClassTy> interfaceTypes = ImmutableList.builder();
    for (Tree.ClassTy i : base.decl().impls()) {
      interfaceTypes.add((Type.ClassTy) bindClassTy(env, enclosingScope, i));
    }

    int mods = base.access();

    CompoundScope scope = base.scope().append(new ClassMemberScope(sym, env));

    List<MethodInfo> methods = bindMethods(base, env, scope, sym, base.decl().members());
    addSyntheticMethods(sym, base, methods);

    ImmutableList<FieldInfo> fields = bindFields(base, env, scope, sym, base.decl().members());

    return new SourceTypeBoundClass(
        interfaceTypes.build(),
        superClassType,
        typeParameterTypes,
        mods,
        ImmutableList.copyOf(methods),
        fields,
        base.owner(),
        base.kind(),
        base.children(),
        base.superclass(),
        base.interfaces(),
        base.typeParameters());
  }

  /** Add synthetic and implicit methods, including default constructors and enum methods. */
  static void addSyntheticMethods(
      ClassSymbol sym, HeaderBoundClass base, List<MethodInfo> methods) {
    switch (base.kind()) {
      case CLASS:
        maybeAddDefaultConstructor(sym, base, methods);
        break;
      case ENUM:
        addEnumMethods(sym, methods);
        break;
      default:
        break;
    }
  }

  private static void maybeAddDefaultConstructor(
      ClassSymbol owner, HeaderBoundClass info, List<MethodInfo> methods) {
    if (hasConstructor(methods)) {
      return;
    }
    ImmutableList<ParamInfo> formals;
    if (hasEnclosingInstance(info)) {
      formals =
          ImmutableList.of(new ParamInfo(Type.ClassTy.asNonParametricClassTy(info.owner()), true));
    } else {
      formals = ImmutableList.of();
    }
    int access = TurbineVisibility.fromAccess(info.access()).flag();
    access |= TurbineFlag.ACC_SYNTH_CTOR;
    methods.add(
        new MethodInfo(
            new MethodSymbol(owner, "<init>"),
            ImmutableMap.of(),
            Type.VOID,
            formals,
            ImmutableList.of(),
            access));
  }

  private static void addEnumMethods(ClassSymbol owner, List<MethodInfo> methods) {
    if (!hasConstructor(methods)) {
      methods.add(
          new MethodInfo(
              new MethodSymbol(owner, "<init>"),
              ImmutableMap.of(),
              Type.VOID,
              ImmutableList.of(
                  new ParamInfo(Type.ClassTy.STRING, true),
                  new ParamInfo(new Type.PrimTy(TurbineConstantTypeKind.INT), true)),
              ImmutableList.of(),
              TurbineFlag.ACC_PRIVATE | TurbineFlag.ACC_SYNTH_CTOR));
    }

    methods.add(
        new MethodInfo(
            new MethodSymbol(owner, "valueOf"),
            ImmutableMap.of(),
            Type.ClassTy.asNonParametricClassTy(owner),
            ImmutableList.of(new ParamInfo(Type.ClassTy.STRING, false)),
            ImmutableList.of(),
            TurbineFlag.ACC_PUBLIC | TurbineFlag.ACC_STATIC));

    methods.add(
        new MethodInfo(
            new MethodSymbol(owner, "values"),
            ImmutableMap.of(),
            new Type.ArrayTy(1, Type.ClassTy.asNonParametricClassTy(owner)),
            ImmutableList.of(),
            ImmutableList.of(),
            TurbineFlag.ACC_PUBLIC | TurbineFlag.ACC_STATIC));
  }

  private static boolean hasConstructor(List<MethodInfo> methods) {
    for (MethodInfo m : methods) {
      if (m.name().equals("<init>")) {
        return true;
      }
    }
    return false;
  }

  /** Bind type parameter types. */
  private static ImmutableMap<TyVarSymbol, TyVarInfo> bindTyParams(
      Env<HeaderBoundClass> env,
      ImmutableList<Tree.TyParam> trees,
      CompoundScope scope,
      Map<String, TyVarSymbol> symbols) {
    ImmutableMap.Builder<TyVarSymbol, TyVarInfo> result = ImmutableMap.builder();
    for (Tree.TyParam tree : trees) {
      TyVarSymbol sym = symbols.get(tree.name());
      // type parameters can refer to themselves (f-bounds), so update the scope first
      scope = scope.append(new SingletonScope(tree.name(), sym));
      Type classBound = null;
      ImmutableList.Builder<Type> interfaceBounds = ImmutableList.builder();
      boolean first = true;
      for (Tree bound : tree.bounds()) {
        Type ty = bindTy(env, scope, bound);
        if (first && !isInterface(env, ty)) {
          classBound = ty;
        } else {
          interfaceBounds.add(ty);
        }
        first = false;
      }
      result.put(sym, new TyVarInfo(classBound, interfaceBounds.build()));
    }
    return result.build();
  }

  private static boolean isInterface(Env<HeaderBoundClass> env, Type ty) {
    if (ty.tyKind() != Type.TyKind.CLASS_TY) {
      return false;
    }
    HeaderBoundClass hi = env.get(((Type.ClassTy) ty).sym());
    return hi.kind() == TurbineTyKind.INTERFACE;
  }

  private static List<MethodInfo> bindMethods(
      HeaderBoundClass base,
      Env<HeaderBoundClass> env,
      CompoundScope scope,
      ClassSymbol sym,
      ImmutableList<Tree> members) {
    List<MethodInfo> methods = new ArrayList<>();
    for (Tree member : members) {
      if (member.kind() == Tree.Kind.METH_DECL) {
        methods.add(bindMethod(base, env, scope, sym, (Tree.MethDecl) member));
      }
    }
    return methods;
  }

  private static MethodInfo bindMethod(
      HeaderBoundClass base,
      Env<HeaderBoundClass> env,
      CompoundScope scope,
      ClassSymbol owner,
      Tree.MethDecl t) {

    MethodSymbol sym = new MethodSymbol(owner, t.name());

    ImmutableMap<String, TyVarSymbol> typeParameters;
    {
      ImmutableMap.Builder<String, TyVarSymbol> builder = ImmutableMap.builder();
      for (Tree.TyParam pt : t.typarams()) {
        builder.put(pt.name(), new TyVarSymbol(sym, pt.name()));
      }
      typeParameters = builder.build();
    }

    ImmutableMap<TyVarSymbol, TyVarInfo> typeParameterTypes =
        bindTyParams(env, t.typarams(), scope, typeParameters);
    scope = scope.append(new MapScope(typeParameters));

    Type returnType;
    if (t.ret().isPresent()) {
      returnType = bindTy(env, scope, t.ret().get());
    } else {
      returnType = Type.VOID;
    }

    ImmutableList.Builder<ParamInfo> parameters = ImmutableList.builder();
    String name = t.name();
    if (name.equals("<init>")) {
      if (hasEnclosingInstance(base)) {
        parameters.add(
            new ParamInfo(Type.ClassTy.asNonParametricClassTy(base.owner()), /*synthetic*/ true));
      } else if (base.kind() == TurbineTyKind.ENUM && name.equals("<init>")) {
        parameters.add(new ParamInfo(Type.ClassTy.STRING, /*synthetic*/ true));
        parameters.add(
            new ParamInfo(new Type.PrimTy(TurbineConstantTypeKind.INT), /*synthetic*/ true));
      }
    }
    for (Tree.VarDecl p : t.params()) {
      parameters.add(new ParamInfo(bindTy(env, scope, p.ty()), /*synthetic*/ false));
    }
    ImmutableList.Builder<Type> exceptions = ImmutableList.builder();
    for (Tree.ClassTy p : t.exntys()) {
      exceptions.add(bindClassTy(env, scope, p));
    }

    int access = 0;
    for (TurbineModifier m : t.mods()) {
      access |= m.flag();
    }

    switch (base.kind()) {
      case INTERFACE:
      case ANNOTATION:
        access |= TurbineFlag.ACC_PUBLIC;
        if ((access & (TurbineFlag.ACC_DEFAULT | TurbineFlag.ACC_SYNTHETIC)) == 0) {
          access |= TurbineFlag.ACC_ABSTRACT;
        }
        break;
      case ENUM:
        if (name.equals("<init>")) {
          access |= TurbineFlag.ACC_PRIVATE;
        }
        break;
      default:
        break;
    }

    return new MethodInfo(
        sym, typeParameterTypes, returnType, parameters.build(), exceptions.build(), access);
  }

  private static boolean hasEnclosingInstance(HeaderBoundClass base) {
    return base.kind() == TurbineTyKind.CLASS
        && base.owner() != null
        && ((base.access() & TurbineFlag.ACC_STATIC) == 0);
  }

  private static ImmutableList<FieldInfo> bindFields(
      HeaderBoundClass base,
      Env<HeaderBoundClass> env,
      CompoundScope scope,
      ClassSymbol sym,
      ImmutableList<Tree> members) {
    ImmutableList.Builder<FieldInfo> fields = ImmutableList.builder();
    for (Tree member : members) {
      if (member.kind() == Tree.Kind.VAR_DECL) {
        fields.add(bindField(base, env, scope, sym, (Tree.VarDecl) member));
      }
    }
    return fields.build();
  }

  private static FieldInfo bindField(
      HeaderBoundClass hi,
      Env<HeaderBoundClass> env,
      CompoundScope scope,
      ClassSymbol owner,
      Tree.VarDecl t) {
    FieldSymbol sym = new FieldSymbol(owner, t.name());
    Type type = bindTy(env, scope, t.ty());
    int access = 0;
    for (TurbineModifier m : t.mods()) {
      access |= m.flag();
    }
    switch (hi.kind()) {
      case INTERFACE:
      case ANNOTATION:
        access |= TurbineFlag.ACC_PUBLIC | TurbineFlag.ACC_FINAL | TurbineFlag.ACC_STATIC;
        break;
      default:
        break;
    }
    return new FieldInfo(sym, type, access);
  }

  private static ImmutableList<Type.TyArg> bindTyArgs(
      Env<HeaderBoundClass> env, CompoundScope scope, ImmutableList<Tree.Type> targs) {
    ImmutableList.Builder<Type.TyArg> result = ImmutableList.builder();
    for (Tree.Type ty : targs) {
      result.add(bindTyArg(env, scope, ty));
    }
    return result.build();
  }

  private static Type.TyArg bindTyArg(
      Env<HeaderBoundClass> env, CompoundScope scope, Tree.Type ty) {
    switch (ty.kind()) {
      case WILD_TY:
        return bindWildTy(env, scope, (Tree.WildTy) ty);
      default:
        return new Type.ConcreteTyArg(bindTy(env, scope, ty));
    }
  }

  private static Type bindTy(Env<HeaderBoundClass> env, CompoundScope scope, Tree t) {
    switch (t.kind()) {
      case CLASS_TY:
        return bindClassTy(env, scope, (Tree.ClassTy) t);
      case PRIM_TY:
        return bindPrimTy((Tree.PrimTy) t);
      case ARR_TY:
        return bindArrTy(env, scope, (Tree.ArrTy) t);
      case VOID_TY:
        return Type.VOID;
      default:
        throw new AssertionError(t.kind());
    }
  }

  private static Type bindClassTy(Env<HeaderBoundClass> env, CompoundScope scope, Tree.ClassTy t) {
    // flatten the components of a qualified class type
    ArrayList<Tree.ClassTy> flat;
    {
      ArrayDeque<Tree.ClassTy> builder = new ArrayDeque<>();
      for (Tree.ClassTy curr = t; curr != null; curr = curr.base().orNull()) {
        builder.addFirst(curr);
      }
      flat = new ArrayList<>(builder);
    }
    // the simple names of all classes in the qualified name
    ArrayList<String> names = new ArrayList<>();
    for (Tree.ClassTy curr : flat) {
      names.add(curr.name());
    }
    // resolve the prefix to a symbol
    LookupResult result = scope.lookup(new LookupKey(names));
    Verify.verifyNotNull(result, "%s", names);
    Symbol sym = result.sym();
    switch (sym.symKind()) {
      case CLASS:
        // resolve any remaining types in the qualified name, and their type arguments
        return bindClassTyRest(env, scope, flat, names, result, (ClassSymbol) sym);
      case TY_PARAM:
        Verify.verify(result.remaining().isEmpty(), "%s", result.remaining());
        return new Type.TyVar((TyVarSymbol) sym);
      default:
        throw new AssertionError(sym.symKind());
    }
  }

  private static Type bindClassTyRest(
      Env<HeaderBoundClass> env,
      CompoundScope scope,
      ArrayList<Tree.ClassTy> flat,
      ArrayList<String> bits,
      LookupResult result,
      ClassSymbol sym) {
    int idx = bits.size() - result.remaining().size() - 1;
    ImmutableList.Builder<Type.ClassTy.SimpleClassTy> classes = ImmutableList.builder();
    classes.add(
        new Type.ClassTy.SimpleClassTy(sym, bindTyArgs(env, scope, flat.get(idx++).tyargs())));
    for (; idx < flat.size(); idx++) {
      Tree.ClassTy curr = flat.get(idx);
      sym = Resolve.resolve(env, sym, curr.name());
      classes.add(new Type.ClassTy.SimpleClassTy(sym, bindTyArgs(env, scope, curr.tyargs())));
    }
    return new Type.ClassTy(classes.build());
  }

  private static Type.PrimTy bindPrimTy(Tree.PrimTy t) {
    return new Type.PrimTy(t.tykind());
  }

  private static Type bindArrTy(Env<HeaderBoundClass> env, CompoundScope scope, Tree.ArrTy t) {
    verify(t.elem().kind() != Tree.Kind.ARR_TY);
    return new Type.ArrayTy(t.dim(), bindTy(env, scope, t.elem()));
  }

  private static Type.TyArg bindWildTy(
      Env<HeaderBoundClass> env, CompoundScope scope, Tree.WildTy t) {
    if (t.lower().isPresent()) {
      return new Type.WildLowerBoundedTy(bindTy(env, scope, t.lower().get()));
    } else if (t.upper().isPresent()) {
      return new Type.WildUpperBoundedTy(bindTy(env, scope, t.upper().get()));
    } else {
      return Type.WILD_TY;
    }
  }
}