| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.common.collect.Maps; |
| 22 |
import com.google.common.collect.Sets; |
| 23 |
import com.google.javascript.jscomp.GlobalNamespace.Name; |
| 24 |
import com.google.javascript.jscomp.GlobalNamespace.Ref; |
| 25 |
import com.google.javascript.jscomp.NodeTraversal.Callback; |
| 26 |
import com.google.javascript.rhino.JSDocInfo; |
| 27 |
import com.google.javascript.rhino.JSTypeExpression; |
| 28 |
import com.google.javascript.rhino.Node; |
| 29 |
import com.google.javascript.rhino.Token; |
| 30 |
import com.google.javascript.rhino.jstype.JSType; |
| 31 |
import com.google.javascript.rhino.jstype.JSTypeNative; |
| 32 |
|
| 33 |
import java.text.MessageFormat; |
| 34 |
import java.util.ArrayDeque; |
| 35 |
import java.util.Deque; |
| 36 |
import java.util.List; |
| 37 |
import java.util.Map; |
| 38 |
import java.util.Set; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class ProcessDefines implements CompilerPass { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private static final Set<String> KNOWN_DEFINES = |
| 55 |
Sets.newHashSet("COMPILED"); |
| 56 |
|
| 57 |
private final AbstractCompiler compiler; |
| 58 |
private final Map<String, Node> dominantReplacements; |
| 59 |
|
| 60 |
private GlobalNamespace namespace = null; |
| 61 |
|
| 62 |
|
| 63 |
static final DiagnosticType UNKNOWN_DEFINE_WARNING = DiagnosticType.warning( |
| 64 |
"JSC_UNKNOWN_DEFINE_WARNING", |
| 65 |
"unknown @define variable {0}"); |
| 66 |
|
| 67 |
|
| 68 |
static final DiagnosticType INVALID_DEFINE_TYPE_ERROR = |
| 69 |
DiagnosticType.error( |
| 70 |
"JSC_INVALID_DEFINE_TYPE_ERROR", |
| 71 |
"@define tag only permits literal types"); |
| 72 |
|
| 73 |
static final DiagnosticType INVALID_DEFINE_INIT_ERROR = |
| 74 |
DiagnosticType.error( |
| 75 |
"JSC_INVALID_DEFINE_INIT_ERROR", |
| 76 |
"illegal initialization of @define variable {0}"); |
| 77 |
|
| 78 |
static final DiagnosticType NON_GLOBAL_DEFINE_INIT_ERROR = |
| 79 |
DiagnosticType.error( |
| 80 |
"JSC_NON_GLOBAL_DEFINE_INIT_ERROR", |
| 81 |
"@define variable {0} assignment must be global"); |
| 82 |
|
| 83 |
static final DiagnosticType DEFINE_NOT_ASSIGNABLE_ERROR = |
| 84 |
DiagnosticType.error( |
| 85 |
"JSC_DEFINE_NOT_ASSIGNABLE_ERROR", |
| 86 |
"@define variable {0} cannot be reassigned due to code at {1}."); |
| 87 |
|
| 88 |
private static final MessageFormat REASON_DEFINE_NOT_ASSIGNABLE = |
| 89 |
new MessageFormat("line {0} of {1}"); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
ProcessDefines(AbstractCompiler compiler, Map<String, Node> replacements) { |
| 101 |
this.compiler = compiler; |
| 102 |
dominantReplacements = replacements; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
ProcessDefines injectNamespace(GlobalNamespace namespace) { |
| 111 |
this.namespace = namespace; |
| 112 |
return this; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void process(Node externs, Node root) { |
| 117 |
if (namespace == null) { |
| 118 |
namespace = new GlobalNamespace(compiler, root); |
| 119 |
} |
| 120 |
overrideDefines(collectDefines(root, namespace)); |
| 121 |
} |
| 122 |
|
| 123 |
private void overrideDefines(Map<String, DefineInfo> allDefines) { |
| 124 |
boolean changed = false; |
| 125 |
for (Map.Entry<String, DefineInfo> def : allDefines.entrySet()) { |
| 126 |
String defineName = def.getKey(); |
| 127 |
DefineInfo info = def.getValue(); |
| 128 |
Node inputValue = dominantReplacements.get(defineName); |
| 129 |
Node finalValue = inputValue != null ? |
| 130 |
inputValue : info.getLastValue(); |
| 131 |
if (finalValue != info.initialValue) { |
| 132 |
info.initialValueParent.replaceChild( |
| 133 |
info.initialValue, finalValue.cloneTree()); |
| 134 |
compiler.addToDebugLog("Overriding @define variable " + defineName); |
| 135 |
changed = changed || |
| 136 |
finalValue.getType() != info.initialValue.getType() || |
| 137 |
!finalValue.isEquivalentTo(info.initialValue); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (changed) { |
| 142 |
compiler.reportCodeChange(); |
| 143 |
} |
| 144 |
|
| 145 |
Set<String> unusedReplacements = dominantReplacements.keySet(); |
| 146 |
unusedReplacements.removeAll(allDefines.keySet()); |
| 147 |
unusedReplacements.removeAll(KNOWN_DEFINES); |
| 148 |
for (String unknownDefine : unusedReplacements) { |
| 149 |
compiler.report(JSError.make(UNKNOWN_DEFINE_WARNING, unknownDefine)); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
private static String format(MessageFormat format, Object... params) { |
| 154 |
return format.format(params); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
private boolean isValidDefineType(JSTypeExpression expression) { |
| 161 |
JSType type = expression.evaluate(null, compiler.getTypeRegistry()); |
| 162 |
return !type.isUnknownType() && type.isSubtype( |
| 163 |
compiler.getTypeRegistry().getNativeType( |
| 164 |
JSTypeNative.NUMBER_STRING_BOOLEAN)); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private Map<String, DefineInfo> collectDefines(Node root, |
| 173 |
GlobalNamespace namespace) { |
| 174 |
|
| 175 |
List<Name> allDefines = Lists.newArrayList(); |
| 176 |
for (Name name : namespace.getNameIndex().values()) { |
| 177 |
Ref decl = name.getDeclaration(); |
| 178 |
if (name.docInfo != null && name.docInfo.isDefine()) { |
| 179 |
|
| 180 |
|
| 181 |
if (isValidDefineType(name.docInfo.getType())) { |
| 182 |
allDefines.add(name); |
| 183 |
} else { |
| 184 |
JSError error = JSError.make( |
| 185 |
decl.getSourceName(), |
| 186 |
decl.node, INVALID_DEFINE_TYPE_ERROR); |
| 187 |
compiler.report(error); |
| 188 |
} |
| 189 |
} else { |
| 190 |
for (Ref ref : name.getRefs()) { |
| 191 |
if (ref == decl) { |
| 192 |
|
| 193 |
continue; |
| 194 |
} |
| 195 |
|
| 196 |
Node n = ref.node; |
| 197 |
Node parent = ref.node.getParent(); |
| 198 |
JSDocInfo info = n.getJSDocInfo(); |
| 199 |
if (info == null && |
| 200 |
parent.isVar() && parent.hasOneChild()) { |
| 201 |
info = parent.getJSDocInfo(); |
| 202 |
} |
| 203 |
|
| 204 |
if (info != null && info.isDefine()) { |
| 205 |
allDefines.add(name); |
| 206 |
break; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
CollectDefines pass = new CollectDefines(compiler, allDefines); |
| 213 |
NodeTraversal.traverse(compiler, root, pass); |
| 214 |
return pass.getAllDefines(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
private static final class CollectDefines implements Callback { |
| 222 |
|
| 223 |
private final AbstractCompiler compiler; |
| 224 |
private final Map<String, DefineInfo> assignableDefines; |
| 225 |
private final Map<String, DefineInfo> allDefines; |
| 226 |
private final Map<Node, RefInfo> allRefInfo; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
private Node lvalueToRemoveLater = null; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
private final Deque<Integer> assignAllowed; |
| 236 |
|
| 237 |
CollectDefines(AbstractCompiler compiler, List<Name> listOfDefines) { |
| 238 |
this.compiler = compiler; |
| 239 |
this.allDefines = Maps.newHashMap(); |
| 240 |
|
| 241 |
assignableDefines = Maps.newHashMap(); |
| 242 |
assignAllowed = new ArrayDeque<Integer>(); |
| 243 |
assignAllowed.push(1); |
| 244 |
|
| 245 |
|
| 246 |
allRefInfo = Maps.newHashMap(); |
| 247 |
for (Name name : listOfDefines) { |
| 248 |
Ref decl = name.getDeclaration(); |
| 249 |
if (decl != null) { |
| 250 |
allRefInfo.put(decl.node, |
| 251 |
new RefInfo(decl, name)); |
| 252 |
} |
| 253 |
for (Ref ref : name.getRefs()) { |
| 254 |
if (ref == decl) { |
| 255 |
|
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
if (ref.getTwin() == null || !ref.getTwin().isSet()) { |
| 261 |
allRefInfo.put(ref.node, new RefInfo(ref, name)); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
Map<String, DefineInfo> getAllDefines() { |
| 272 |
return allDefines; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
@Override |
| 280 |
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, |
| 281 |
Node parent) { |
| 282 |
updateAssignAllowedStack(n, true); |
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 288 |
RefInfo refInfo = allRefInfo.get(n); |
| 289 |
if (refInfo != null) { |
| 290 |
Ref ref = refInfo.ref; |
| 291 |
Name name = refInfo.name; |
| 292 |
String fullName = name.getFullName(); |
| 293 |
switch (ref.type) { |
| 294 |
case SET_FROM_GLOBAL: |
| 295 |
case SET_FROM_LOCAL: |
| 296 |
Node valParent = getValueParent(ref); |
| 297 |
Node val = valParent.getLastChild(); |
| 298 |
if (valParent.isAssign() && name.isSimpleName() && |
| 299 |
name.getDeclaration() == ref) { |
| 300 |
|
| 301 |
|
| 302 |
compiler.report( |
| 303 |
t.makeError(val, INVALID_DEFINE_INIT_ERROR, fullName)); |
| 304 |
} else if (processDefineAssignment(t, fullName, val, valParent)) { |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
refInfo.name.removeRef(ref); |
| 312 |
lvalueToRemoveLater = valParent; |
| 313 |
} |
| 314 |
break; |
| 315 |
default: |
| 316 |
if (t.inGlobalScope()) { |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
DefineInfo info = assignableDefines.get(fullName); |
| 321 |
if (info != null) { |
| 322 |
setDefineInfoNotAssignable(info, t); |
| 323 |
assignableDefines.remove(fullName); |
| 324 |
} |
| 325 |
} |
| 326 |
break; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (!t.inGlobalScope() && |
| 331 |
n.getJSDocInfo() != null && n.getJSDocInfo().isDefine()) { |
| 332 |
|
| 333 |
compiler.report( |
| 334 |
t.makeError(n, NON_GLOBAL_DEFINE_INIT_ERROR, "")); |
| 335 |
} |
| 336 |
|
| 337 |
if (lvalueToRemoveLater == n) { |
| 338 |
lvalueToRemoveLater = null; |
| 339 |
if (n.isAssign()) { |
| 340 |
Node last = n.getLastChild(); |
| 341 |
n.removeChild(last); |
| 342 |
parent.replaceChild(n, last); |
| 343 |
} else { |
| 344 |
Preconditions.checkState(n.isName()); |
| 345 |
n.removeChild(n.getFirstChild()); |
| 346 |
} |
| 347 |
compiler.reportCodeChange(); |
| 348 |
} |
| 349 |
|
| 350 |
if (n.isCall()) { |
| 351 |
if (t.inGlobalScope()) { |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
for (DefineInfo info : assignableDefines.values()) { |
| 363 |
setDefineInfoNotAssignable(info, t); |
| 364 |
} |
| 365 |
|
| 366 |
assignableDefines.clear(); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
updateAssignAllowedStack(n, false); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private void updateAssignAllowedStack(Node n, boolean entering) { |
| 381 |
switch (n.getType()) { |
| 382 |
case Token.CASE: |
| 383 |
case Token.FOR: |
| 384 |
case Token.FUNCTION: |
| 385 |
case Token.HOOK: |
| 386 |
case Token.IF: |
| 387 |
case Token.SWITCH: |
| 388 |
case Token.WHILE: |
| 389 |
if (entering) { |
| 390 |
assignAllowed.push(0); |
| 391 |
} else { |
| 392 |
assignAllowed.remove(); |
| 393 |
} |
| 394 |
break; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
private boolean isAssignAllowed() { |
| 403 |
return assignAllowed.element() == 1; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
private boolean processDefineAssignment(NodeTraversal t, |
| 416 |
String name, Node value, Node valueParent) { |
| 417 |
if (value == null || !NodeUtil.isValidDefineValue(value, |
| 418 |
allDefines.keySet())) { |
| 419 |
compiler.report( |
| 420 |
t.makeError(value, INVALID_DEFINE_INIT_ERROR, name)); |
| 421 |
} else if (!isAssignAllowed()) { |
| 422 |
compiler.report( |
| 423 |
t.makeError(valueParent, NON_GLOBAL_DEFINE_INIT_ERROR, name)); |
| 424 |
} else { |
| 425 |
DefineInfo info = allDefines.get(name); |
| 426 |
if (info == null) { |
| 427 |
|
| 428 |
info = new DefineInfo(value, valueParent); |
| 429 |
allDefines.put(name, info); |
| 430 |
assignableDefines.put(name, info); |
| 431 |
} else if (info.recordAssignment(value)) { |
| 432 |
|
| 433 |
|
| 434 |
return true; |
| 435 |
} else { |
| 436 |
|
| 437 |
|
| 438 |
compiler.report( |
| 439 |
t.makeError(valueParent, DEFINE_NOT_ASSIGNABLE_ERROR, |
| 440 |
name, info.getReasonWhyNotAssignable())); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
return false; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
private static Node getValueParent(Ref ref) { |
| 454 |
|
| 455 |
return ref.node.getParent() != null && |
| 456 |
ref.node.getParent().isVar() ? |
| 457 |
ref.node : ref.node.getParent(); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
private void setDefineInfoNotAssignable(DefineInfo info, NodeTraversal t) { |
| 468 |
info.setNotAssignable(format(REASON_DEFINE_NOT_ASSIGNABLE, |
| 469 |
t.getLineNumber(), t.getSourceName())); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
private static class RefInfo { |
| 477 |
final Ref ref; |
| 478 |
final Name name; |
| 479 |
|
| 480 |
RefInfo(Ref ref, Name name) { |
| 481 |
this.ref = ref; |
| 482 |
this.name = name; |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
private static final class DefineInfo { |
| 493 |
public final Node initialValueParent; |
| 494 |
public final Node initialValue; |
| 495 |
private Node lastValue; |
| 496 |
private boolean isAssignable; |
| 497 |
private String reasonNotAssignable; |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
public DefineInfo(Node initialValue, Node initialValueParent) { |
| 503 |
this.initialValueParent = initialValueParent; |
| 504 |
this.initialValue = initialValue; |
| 505 |
lastValue = initialValue; |
| 506 |
isAssignable = true; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
public void setNotAssignable(String reason) { |
| 515 |
isAssignable = false; |
| 516 |
reasonNotAssignable = reason; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
public String getReasonWhyNotAssignable() { |
| 523 |
return reasonNotAssignable; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public boolean recordAssignment(Node value) { |
| 532 |
lastValue = value; |
| 533 |
return isAssignable; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
public Node getLastValue() { |
| 540 |
return lastValue; |
| 541 |
} |
| 542 |
} |
| 543 |
} |