aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/turbine/processing/TurbineMessager.java
blob: 9c333b228ad5b8355a67b32da8d869c27cb17e3c (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
/*
 * Copyright 2019 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.processing;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.turbine.binder.bound.SourceTypeBoundClass;
import com.google.turbine.binder.bound.TurbineAnnotationValue;
import com.google.turbine.binder.bound.TypeBoundClass;
import com.google.turbine.binder.bound.TypeBoundClass.MethodInfo;
import com.google.turbine.binder.bound.TypeBoundClass.TyVarInfo;
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.ParamSymbol;
import com.google.turbine.binder.sym.Symbol;
import com.google.turbine.binder.sym.TyVarSymbol;
import com.google.turbine.diag.SourceFile;
import com.google.turbine.diag.TurbineError;
import com.google.turbine.diag.TurbineLog;
import com.google.turbine.model.Const;
import com.google.turbine.processing.TurbineElement.TurbineNoTypeElement;
import com.google.turbine.tree.Tree;
import com.google.turbine.type.AnnoInfo;
import java.util.Iterator;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic;
import org.checkerframework.checker.nullness.qual.Nullable;

/** Turbine's implementation of {@link Messager}. */
public class TurbineMessager implements Messager {
  private final ModelFactory factory;
  private final TurbineLog log;

  public TurbineMessager(ModelFactory factory, TurbineLog log) {
    this.factory = factory;
    this.log = log;
  }

  @Override
  public void printMessage(Diagnostic.Kind kind, CharSequence msg) {
    // TODO(cushon): null-check `msg` after fixing affected processors
    log.diagnostic(kind, String.valueOf(msg));
  }

  @Override
  public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e) {
    if (e == null || e instanceof TurbineNoTypeElement) {
      printMessage(kind, msg);
      return;
    }
    Symbol sym = ((TurbineElement) e).sym();
    SourceFile source = getSource(sym);
    int position = getPosition(sym);
    log.withSource(source).diagnostic(kind, position, TurbineError.ErrorKind.PROC, msg);
  }

  @Override
  public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a) {
    if (a == null || e == null || e instanceof TurbineNoTypeElement) {
      printMessage(kind, msg, e);
      return;
    }
    SourceFile source = getSource(((TurbineElement) e).sym());
    int position = ((TurbineAnnotationMirror) a).anno().tree().position();
    log.withSource(source).diagnostic(kind, position, TurbineError.ErrorKind.PROC, msg);
  }

  @Override
  public void printMessage(
      Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) {
    if (a == null || e == null || e instanceof TurbineNoTypeElement || v == null) {
      printMessage(kind, msg, e, a);
      return;
    }
    SourceFile source = getSource(((TurbineElement) e).sym());
    AnnoInfo anno = ((TurbineAnnotationMirror) a).anno();
    int position = locateInAnnotation(((TurbineAnnotationValueMirror) v).value(), anno);
    if (position == -1) {
      position = anno.tree().position();
    }
    log.withSource(source).diagnostic(kind, position, TurbineError.ErrorKind.PROC, msg);
  }

  /**
   * Returns the {@link SourceFile} that contains the declaration of the given {@link Symbol}, or
   * {@code null} if the symbol was not compiled from source.
   */
  @Nullable
  private SourceFile getSource(Symbol sym) {
    ClassSymbol encl = ModelFactory.enclosingClass(sym);
    TypeBoundClass info = factory.getSymbol(encl);
    if (!(info instanceof SourceTypeBoundClass)) {
      return null;
    }
    return ((SourceTypeBoundClass) info).source();
  }

  /**
   * Returns the position of the given {@link Symbol}'s declaration, or {@code null} if it was not
   * compiled from source.
   */
  private int getPosition(Symbol sym) {
    switch (sym.symKind()) {
      case CLASS:
        return classPosition((ClassSymbol) sym);
      case TY_PARAM:
        return tyParamPosition((TyVarSymbol) sym);
      case METHOD:
        return methodPosition((MethodSymbol) sym);
      case FIELD:
        return fieldPosition((FieldSymbol) sym);
      case PARAMETER:
        return paramPosition((ParamSymbol) sym);
      case MODULE:
      case PACKAGE:
        break;
    }
    throw new AssertionError(sym.symKind());
  }

  private int fieldPosition(FieldSymbol sym) {
    Tree.VarDecl decl = factory.getFieldInfo(sym).decl();
    return decl != null ? decl.position() : -1;
  }

  private int paramPosition(ParamSymbol sym) {
    MethodInfo minfo = factory.getMethodInfo(sym.owner());
    if (minfo.decl() == null) {
      return -1;
    }
    int idx = minfo.parameters().indexOf(factory.getParamInfo(sym));
    return minfo.decl().params().get(idx).position();
  }

  private int methodPosition(MethodSymbol sym) {
    MethodInfo methodInfo = factory.getMethodInfo(sym);
    Tree.MethDecl decl = methodInfo.decl();
    if (decl != null) {
      return decl.position();
    }
    // use the enclosing class position for synthetic methods
    int position = classPosition(sym.owner());
    if (position == -1) {
      return -1;
    }
    // TODO(b/139079081): change diagnostic position of declarations instead of the -= 6 fixup
    position -= 6; // adjust to start of `class ` for parity with javac
    return position;
  }

  private int classPosition(ClassSymbol owner) {
    TypeBoundClass symbol = factory.getSymbol(owner);
    if (!(symbol instanceof SourceTypeBoundClass)) {
      return -1;
    }
    return ((SourceTypeBoundClass) symbol).decl().position();
  }

  private int tyParamPosition(TyVarSymbol sym) {
    TyVarSymbol tyVarSymbol = sym;
    Symbol owner = tyVarSymbol.owner();
    ImmutableMap<TyVarSymbol, TyVarInfo> tyVars;
    ImmutableList<Tree.TyParam> trees;
    switch (owner.symKind()) {
      case CLASS:
        TypeBoundClass cinfo = factory.getSymbol((ClassSymbol) owner);
        if (!(cinfo instanceof SourceTypeBoundClass)) {
          return -1;
        }
        tyVars = cinfo.typeParameterTypes();
        trees = ((SourceTypeBoundClass) cinfo).decl().typarams();
        break;
      case METHOD:
        MethodInfo minfo = factory.getMethodInfo((MethodSymbol) owner);
        if (minfo.decl() == null) {
          return -1;
        }
        tyVars = minfo.tyParams();
        trees = minfo.decl().typarams();
        break;
      default:
        throw new AssertionError(owner.symKind());
    }
    return trees.get(tyVars.keySet().asList().indexOf(tyVarSymbol)).position();
  }

  /** Returns the position of the given annotation value {@code v} in the given annotation. */
  private static int locateInAnnotation(Const v, AnnoInfo anno) {
    return locate(v, anno.values().values().asList(), anno.tree().args());
  }

  /**
   * Returns the position of the given annotation value {@code toFind} within the given constant
   * {@code v} (which may be a compound value, i.e. a nested annotation or array value), and given
   * the corresponding expression tree.
   */
  private static int locate(Const toFind, Const v, Tree.Expression t) {
    // the element name can be omitted for `value`, e.g. in `@A({1, 2, 3})`
    t = t.kind().equals(Tree.Kind.ASSIGN) ? ((Tree.Assign) t).expr() : t;
    if (toFind.equals(v)) {
      return t.position();
    }
    switch (v.kind()) {
      case ARRAY:
        ImmutableList<Tree.Expression> elements =
            t.kind().equals(Tree.Kind.ARRAY_INIT)
                ? ((Tree.ArrayInit) t).exprs()
                : ImmutableList.of(t);
        return locate(toFind, ((Const.ArrayInitValue) v).elements(), elements);
      case ANNOTATION:
        return locateInAnnotation(toFind, ((TurbineAnnotationValue) v).info());
      default:
        return -1;
    }
  }

  /**
   * Returns the position of the given annotation value {@code toFind}, given a list of annotation
   * values corresponding to the element values of a (possibly nested) annotation or an array value,
   * and the corresponding expression trees.
   */
  private static int locate(
      Const toFind, ImmutableList<Const> vx, ImmutableList<Tree.Expression> tx) {
    Iterator<Const> vi = vx.iterator();
    Iterator<Tree.Expression> ti = tx.iterator();
    while (vi.hasNext()) {
      int result = locate(toFind, vi.next(), ti.next());
      if (result != -1) {
        return result;
      }
    }
    return -1;
  }
}