src/share/classes/com/sun/tools/javac/tree/TreeInfo.java

changeset 1138
7375d4979bd3
parent 1127
ca49d50318dc
child 1143
ec59a2ce9114
equal deleted inserted replaced
1137:c1238fcc9515 1138:7375d4979bd3
26 package com.sun.tools.javac.tree; 26 package com.sun.tools.javac.tree;
27 27
28 import com.sun.source.tree.Tree; 28 import com.sun.source.tree.Tree;
29 import com.sun.tools.javac.comp.AttrContext; 29 import com.sun.tools.javac.comp.AttrContext;
30 import com.sun.tools.javac.comp.Env; 30 import com.sun.tools.javac.comp.Env;
31 import java.util.Map;
32 import com.sun.tools.javac.util.*; 31 import com.sun.tools.javac.util.*;
33 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 32 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
34 import com.sun.tools.javac.code.*; 33 import com.sun.tools.javac.code.*;
34 import com.sun.tools.javac.parser.EndPosTable;
35 import com.sun.tools.javac.tree.JCTree.*; 35 import com.sun.tools.javac.tree.JCTree.*;
36 36
37 import static com.sun.tools.javac.code.Flags.*; 37 import static com.sun.tools.javac.code.Flags.*;
38 import static com.sun.tools.javac.tree.JCTree.Tag.*; 38 import static com.sun.tools.javac.tree.JCTree.Tag.*;
39 import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK; 39 import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
344 return tree.pos; 344 return tree.pos;
345 } 345 }
346 346
347 /** The end position of given tree, given a table of end positions generated by the parser 347 /** The end position of given tree, given a table of end positions generated by the parser
348 */ 348 */
349 public static int getEndPos(JCTree tree, Map<JCTree, Integer> endPositions) { 349 public static int getEndPos(JCTree tree, EndPosTable endPosTable) {
350 if (tree == null) 350 if (tree == null)
351 return Position.NOPOS; 351 return Position.NOPOS;
352 352
353 if (endPositions == null) { 353 if (endPosTable == null) {
354 // fall back on limited info in the tree 354 // fall back on limited info in the tree
355 return endPos(tree); 355 return endPos(tree);
356 } 356 }
357 357
358 Integer mapPos = endPositions.get(tree); 358 int mapPos = endPosTable.getEndPos(tree);
359 if (mapPos != null) 359 if (mapPos != Position.NOPOS)
360 return mapPos; 360 return mapPos;
361 361
362 switch(tree.getTag()) { 362 switch(tree.getTag()) {
363 case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG: 363 case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
364 case SL_ASG: case SR_ASG: case USR_ASG: 364 case SL_ASG: case SR_ASG: case USR_ASG:
365 case PLUS_ASG: case MINUS_ASG: case MUL_ASG: 365 case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
366 case DIV_ASG: case MOD_ASG: 366 case DIV_ASG: case MOD_ASG:
367 return getEndPos(((JCAssignOp) tree).rhs, endPositions); 367 return getEndPos(((JCAssignOp) tree).rhs, endPosTable);
368 case OR: case AND: case BITOR: 368 case OR: case AND: case BITOR:
369 case BITXOR: case BITAND: case EQ: 369 case BITXOR: case BITAND: case EQ:
370 case NE: case LT: case GT: 370 case NE: case LT: case GT:
371 case LE: case GE: case SL: 371 case LE: case GE: case SL:
372 case SR: case USR: case PLUS: 372 case SR: case USR: case PLUS:
373 case MINUS: case MUL: case DIV: 373 case MINUS: case MUL: case DIV:
374 case MOD: 374 case MOD:
375 return getEndPos(((JCBinary) tree).rhs, endPositions); 375 return getEndPos(((JCBinary) tree).rhs, endPosTable);
376 case CASE: 376 case CASE:
377 return getEndPos(((JCCase) tree).stats.last(), endPositions); 377 return getEndPos(((JCCase) tree).stats.last(), endPosTable);
378 case CATCH: 378 case CATCH:
379 return getEndPos(((JCCatch) tree).body, endPositions); 379 return getEndPos(((JCCatch) tree).body, endPosTable);
380 case CONDEXPR: 380 case CONDEXPR:
381 return getEndPos(((JCConditional) tree).falsepart, endPositions); 381 return getEndPos(((JCConditional) tree).falsepart, endPosTable);
382 case FORLOOP: 382 case FORLOOP:
383 return getEndPos(((JCForLoop) tree).body, endPositions); 383 return getEndPos(((JCForLoop) tree).body, endPosTable);
384 case FOREACHLOOP: 384 case FOREACHLOOP:
385 return getEndPos(((JCEnhancedForLoop) tree).body, endPositions); 385 return getEndPos(((JCEnhancedForLoop) tree).body, endPosTable);
386 case IF: { 386 case IF: {
387 JCIf node = (JCIf)tree; 387 JCIf node = (JCIf)tree;
388 if (node.elsepart == null) { 388 if (node.elsepart == null) {
389 return getEndPos(node.thenpart, endPositions); 389 return getEndPos(node.thenpart, endPosTable);
390 } else { 390 } else {
391 return getEndPos(node.elsepart, endPositions); 391 return getEndPos(node.elsepart, endPosTable);
392 } 392 }
393 } 393 }
394 case LABELLED: 394 case LABELLED:
395 return getEndPos(((JCLabeledStatement) tree).body, endPositions); 395 return getEndPos(((JCLabeledStatement) tree).body, endPosTable);
396 case MODIFIERS: 396 case MODIFIERS:
397 return getEndPos(((JCModifiers) tree).annotations.last(), endPositions); 397 return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable);
398 case SYNCHRONIZED: 398 case SYNCHRONIZED:
399 return getEndPos(((JCSynchronized) tree).body, endPositions); 399 return getEndPos(((JCSynchronized) tree).body, endPosTable);
400 case TOPLEVEL: 400 case TOPLEVEL:
401 return getEndPos(((JCCompilationUnit) tree).defs.last(), endPositions); 401 return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable);
402 case TRY: { 402 case TRY: {
403 JCTry node = (JCTry)tree; 403 JCTry node = (JCTry)tree;
404 if (node.finalizer != null) { 404 if (node.finalizer != null) {
405 return getEndPos(node.finalizer, endPositions); 405 return getEndPos(node.finalizer, endPosTable);
406 } else if (!node.catchers.isEmpty()) { 406 } else if (!node.catchers.isEmpty()) {
407 return getEndPos(node.catchers.last(), endPositions); 407 return getEndPos(node.catchers.last(), endPosTable);
408 } else { 408 } else {
409 return getEndPos(node.body, endPositions); 409 return getEndPos(node.body, endPosTable);
410 } 410 }
411 } 411 }
412 case WILDCARD: 412 case WILDCARD:
413 return getEndPos(((JCWildcard) tree).inner, endPositions); 413 return getEndPos(((JCWildcard) tree).inner, endPosTable);
414 case TYPECAST: 414 case TYPECAST:
415 return getEndPos(((JCTypeCast) tree).expr, endPositions); 415 return getEndPos(((JCTypeCast) tree).expr, endPosTable);
416 case TYPETEST: 416 case TYPETEST:
417 return getEndPos(((JCInstanceOf) tree).clazz, endPositions); 417 return getEndPos(((JCInstanceOf) tree).clazz, endPosTable);
418 case POS: 418 case POS:
419 case NEG: 419 case NEG:
420 case NOT: 420 case NOT:
421 case COMPL: 421 case COMPL:
422 case PREINC: 422 case PREINC:
423 case PREDEC: 423 case PREDEC:
424 return getEndPos(((JCUnary) tree).arg, endPositions); 424 return getEndPos(((JCUnary) tree).arg, endPosTable);
425 case WHILELOOP: 425 case WHILELOOP:
426 return getEndPos(((JCWhileLoop) tree).body, endPositions); 426 return getEndPos(((JCWhileLoop) tree).body, endPosTable);
427 case ERRONEOUS: { 427 case ERRONEOUS: {
428 JCErroneous node = (JCErroneous)tree; 428 JCErroneous node = (JCErroneous)tree;
429 if (node.errs != null && node.errs.nonEmpty()) 429 if (node.errs != null && node.errs.nonEmpty())
430 return getEndPos(node.errs.last(), endPositions); 430 return getEndPos(node.errs.last(), endPosTable);
431 } 431 }
432 } 432 }
433 return Position.NOPOS; 433 return Position.NOPOS;
434 } 434 }
435 435
442 final int endPos = TreeInfo.endPos(tree); 442 final int endPos = TreeInfo.endPos(tree);
443 return new DiagnosticPosition() { 443 return new DiagnosticPosition() {
444 public JCTree getTree() { return tree; } 444 public JCTree getTree() { return tree; }
445 public int getStartPosition() { return TreeInfo.getStartPos(tree); } 445 public int getStartPosition() { return TreeInfo.getStartPos(tree); }
446 public int getPreferredPosition() { return endPos; } 446 public int getPreferredPosition() { return endPos; }
447 public int getEndPosition(Map<JCTree, Integer> endPosTable) { 447 public int getEndPosition(EndPosTable endPosTable) {
448 return TreeInfo.getEndPos(tree, endPosTable); 448 return TreeInfo.getEndPos(tree, endPosTable);
449 } 449 }
450 }; 450 };
451 } 451 }
452 452

mercurial