1 24 25 package org.aspectj.compiler.base.ast; 26 27 import org.aspectj.compiler.base.JavaCompiler; 28 import org.aspectj.compiler.base.CompilerObject; 29 import org.aspectj.compiler.base.TypeManager; 30 import org.aspectj.compiler.crosscuts.ast.AnonymousMethodExpr; 31 32 import org.aspectj.util.JavaStrings; 33 34 import java.lang.reflect.*; 35 import java.util.*; 36 37 public class AST extends CompilerObject { 38 private SourceLocation sourceLocation; 39 40 public AST(SourceLocation location) { 41 super(location.getCompiler()); 42 this.sourceLocation = location; 43 } 44 45 public SourceLocation getSourceLocation() { 46 return sourceLocation; 47 } 48 49 private static String makeLegalIdentifier(String s) { 50 if (s.length() == 0) 51 return "_"; 52 53 char[] chars = s.toCharArray(); 54 if (!Character.isJavaIdentifierStart(chars[0])) { 55 chars[0] = '_'; 56 } 57 58 for (int i = 1; i < chars.length; i++) { 59 if (!Character.isJavaIdentifierPart(chars[i])) 60 chars[i] = '_'; 61 } 62 return new String (chars); 63 } 64 65 public static final String PREFIX = "aspectj$"; 66 67 public String makeGeneratedName(String s) { 68 s = makeLegalIdentifier(s); 69 if (getOptions().XaddSafePrefix) { 70 if (s.startsWith(PREFIX)) 71 return s; 72 else 73 return PREFIX + s; 74 } else { 75 return s; 76 } 77 } 78 79 public StringLiteralExpr makeString(String s) { 80 return new StringLiteralExpr(getSourceLocation(), s); 81 } 82 83 public CallExpr makeStaticCall(MethodDec method, Exprs args) { 84 return makeStaticCall(method.getMethod(), args); 85 } 86 87 public CallExpr makeStaticCall(MethodDec dec, Expr a1) { 88 return makeStaticCall(dec.getMethod(), a1); 89 } 90 91 public CallExpr makeStaticCall(Method method, Exprs args) { 92 return new CallExpr( 93 getSourceLocation(), 94 method, 95 makeTypeExpr(method.getDeclaringType()), 96 args); 97 } 98 99 public CallExpr makeStaticCall(Method dec, Expr a1) { 100 return makeStaticCall(dec, makeExprs(a1)); 101 } 102 103 public CallExpr makeStaticCall(Type type, String name, Exprs args) { 104 return new CallExpr( 105 getSourceLocation(), 106 type.getMethod(name, null, args, true), 107 makeTypeExpr(type), 108 args); 109 } 110 111 public CallExpr makeStaticCall(Type type, String name) { 112 return makeStaticCall(type, name, new Exprs(getSourceLocation())); 113 } 114 public CallExpr makeStaticCall(Type type, String name, Expr a1) { 115 return makeStaticCall(type, name, new Exprs(getSourceLocation(), a1)); 116 } 117 public CallExpr makeStaticCall(Type type, String name, Expr a1, Expr a2) { 118 return makeStaticCall(type, name, new Exprs(getSourceLocation(), a1, a2)); 119 } 120 public CallExpr makeStaticCall( 121 Type type, 122 String name, 123 Expr a1, 124 Expr a2, 125 Expr a3) { 126 return makeStaticCall(type, name, new Exprs(getSourceLocation(), a1, a2, a3)); 127 } 128 public CallExpr makeStaticCall( 129 Type type, 130 String name, 131 Expr a1, 132 Expr a2, 133 Expr a3, 134 Expr a4) { 135 return makeStaticCall(type, name, makeExprs(a1, a2, a3, a4)); 136 } 137 public CallExpr makeStaticCall( 138 Type type, 139 String name, 140 Expr a1, 141 Expr a2, 142 Expr a3, 143 Expr a4, 144 Expr a5) { 145 return makeStaticCall(type, name, makeExprs(a1, a2, a3, a4, a5)); 146 } 147 public CallExpr makeStaticCall( 148 Type type, 149 String name, 150 Expr a1, 151 Expr a2, 152 Expr a3, 153 Expr a4, 154 Expr a5, 155 Expr a6) { 156 return makeStaticCall(type, name, makeExprs(a1, a2, a3, a4, a5, a6)); 157 } 158 public CallExpr makeStaticCall( 159 Type type, 160 String name, 161 Expr a1, 162 Expr a2, 163 Expr a3, 164 Expr a4, 165 Expr a5, 166 Expr a6, 167 Expr a7) { 168 return makeStaticCall(type, name, makeExprs(a1, a2, a3, a4, a5, a6, a7)); 169 } 170 171 public NewInstanceExpr makeNew( 172 Type type, 173 Exprs args, 174 Constructor constructor) { 175 return new NewInstanceExpr( 176 getSourceLocation(), 177 null, 178 type.makeTypeD(), 179 args, 180 null, 181 constructor); 182 } 183 184 public NewInstanceExpr makeNew(Type type, Exprs args) { 185 return new NewInstanceExpr( 186 getSourceLocation(), 187 type.getConstructor(null, args, true), 188 args); 189 } 190 191 public NewInstanceExpr makeNew(Type type) { 192 return makeNew(type, new Exprs(getSourceLocation())); 193 } 194 public NewInstanceExpr makeNew(Type type, Expr a1) { 195 return makeNew(type, new Exprs(getSourceLocation(), a1)); 196 } 197 public NewInstanceExpr makeNew(Type type, Expr a1, Expr a2) { 198 return makeNew(type, new Exprs(getSourceLocation(), a1, a2)); 199 } 200 201 public NewArrayExpr makeNewArray(ArrayType type, ArrayInitializer init) { 202 return new NewArrayExpr(getSourceLocation(), type.makeTypeD(), init, null); 203 } 204 205 public ConstructorCallExpr makeQualifiedConstructorCall( 206 Exprs args, 207 Constructor constructor, 208 Expr primary) { 209 return new ConstructorCallExpr( 210 getSourceLocation(), 211 primary, 212 true, 213 args, 214 constructor); 215 } 216 217 public ConstructorCallExpr makeConstructorCall( 218 boolean isSuper, 219 Exprs args, 220 Constructor constructor) { 221 return new ConstructorCallExpr(getSourceLocation(), isSuper, args, constructor); 222 } 223 224 public ConstructorCallExpr makeSuperConstructorCall( 225 Exprs args, 226 Constructor constructor) { 227 return makeConstructorCall(true, args, constructor); 228 } 229 230 public ConstructorCallExpr makeSuperConstructorCall(Constructor constructor) { 231 return makeConstructorCall(true, makeExprs(), constructor); 232 } 233 234 public ConstructorBody makeConstructorBody(ConstructorCallExpr c, Stmts s) { 235 return new ConstructorBody(getSourceLocation(), s, true, c); 236 } 237 238 public ConstructorDec makeConstructor( 239 Modifiers m, 240 Formals f, 241 TypeDs ts, 242 ConstructorCallExpr c, 243 Stmts s) { 244 return new ConstructorDec( 245 getSourceLocation(), 246 m, 247 f, 248 ts, 249 makeConstructorBody(c, s)); 250 } 251 252 public CallExpr makeCall(Expr instance, String name, Exprs args) { 253 return new CallExpr( 254 getSourceLocation(), 255 instance.getType().getMethod(name, null, args, true), 256 instance, 257 args); 258 } 259 260 public CallExpr makeCall(Expr instance, String name) { 261 return makeCall(instance, name, new Exprs(getSourceLocation())); 262 } 263 264 public CallExpr makeCall(Expr instance, String name, Expr a1) { 265 return makeCall(instance, name, new Exprs(getSourceLocation(), a1)); 266 } 267 268 public CallExpr makeCall(Expr instance, String name, Expr a1, Expr a2) { 269 return makeCall(instance, name, new Exprs(getSourceLocation(), a1, a2)); 270 } 271 272 public CallExpr makeCall(Method md, Expr instance, Exprs args) { 273 if (instance == null) { 274 md.getMethodDec().showError("INTERNAL: instance null in call to"); 275 } 276 return new CallExpr(getSourceLocation(), md, instance, args); 278 } 279 280 public CallExpr makeCall(Method md, Expr instance) { 281 return makeCall(md, instance, new Exprs(getSourceLocation())); 282 } 283 284 public CallExpr makeCall(Method md, Expr instance, Expr a1) { 285 return makeCall(md, instance, new Exprs(getSourceLocation(), a1)); 286 } 287 288 public CallExpr makeCall(Method md, Expr instance, Expr a1, Expr a2) { 289 return makeCall(md, instance, new Exprs(getSourceLocation(), a1, a2)); 290 } 291 292 public CallExpr makeCall(MethodDec md, Expr instance, Exprs args) { 293 if (instance == null) { 294 md.showError("INTERNAL: instance null in call to"); 295 } 296 return new CallExpr(getSourceLocation(), md.getMethod(), instance, args); 298 } 299 300 public CallExpr makeCall(MethodDec md, Expr instance) { 301 return makeCall(md, instance, new Exprs(getSourceLocation())); 302 } 303 304 public CallExpr makeCall(MethodDec md, Expr instance, Expr a1) { 305 return makeCall(md, instance, new Exprs(getSourceLocation(), a1)); 306 } 307 308 public CallExpr makeCall(MethodDec md, Expr instance, Expr a1, Expr a2) { 309 return makeCall(md, instance, new Exprs(getSourceLocation(), a1, a2)); 310 } 311 312 public CallExpr makeSuperCall(Method md, Exprs args) { 313 return new CallExpr( 315 getSourceLocation(), 316 makeThis(md.getDeclaringType()), 317 md.getName(), 318 args, 319 md, 320 true); 321 } 322 323 public LogNotOpExpr makeLogNot(Expr arg) { 324 return new LogNotOpExpr(getSourceLocation(), "!", arg); 325 } 326 327 public Exprs makeExprs() { 328 Exprs exprs = new Exprs(getSourceLocation()); 329 return exprs; 330 } 331 332 public Exprs makeExprs(Expr a1) { 333 Exprs exprs = new Exprs(getSourceLocation(), a1); 334 return exprs; 335 } 336 337 public Exprs makeExprs(Expr a1, Expr a2) { 338 Exprs exprs = new Exprs(getSourceLocation(), a1, a2); 339 return exprs; 340 } 341 342 public Exprs makeExprs(Expr a1, Expr a2, Expr a3) { 343 Exprs exprs = new Exprs(getSourceLocation(), a1, a2, a3); 344 return exprs; 345 } 346 347 public Exprs makeExprs(Expr a1, Expr a2, Expr a3, Expr a4) { 348 Exprs exprs = new Exprs(getSourceLocation()); 349 exprs.add(a1); 350 exprs.add(a2); 351 exprs.add(a3); 352 exprs.add(a4); 353 return exprs; 354 } 355 public Exprs makeExprs(Expr a1, Expr a2, Expr a3, Expr a4, Expr a5) { 356 Exprs exprs = new Exprs(getSourceLocation()); 357 exprs.add(a1); 358 exprs.add(a2); 359 exprs.add(a3); 360 exprs.add(a4); 361 exprs.add(a5); 362 return exprs; 363 } 364 public Exprs makeExprs(Expr a1, Expr a2, Expr a3, Expr a4, Expr a5, Expr a6) { 365 Exprs exprs = new Exprs(getSourceLocation()); 366 exprs.add(a1); 367 exprs.add(a2); 368 exprs.add(a3); 369 exprs.add(a4); 370 exprs.add(a5); 371 exprs.add(a6); 372 return exprs; 373 } 374 public Exprs makeExprs( 375 Expr a1, 376 Expr a2, 377 Expr a3, 378 Expr a4, 379 Expr a5, 380 Expr a6, 381 Expr a7) { 382 Exprs exprs = new Exprs(getSourceLocation()); 383 exprs.add(a1); 384 exprs.add(a2); 385 exprs.add(a3); 386 exprs.add(a4); 387 exprs.add(a5); 388 exprs.add(a6); 389 exprs.add(a7); 390 return exprs; 391 } 392 393 public TriTestExpr makeTriTest(Expr t, Expr c, Expr a) { 394 return new TriTestExpr(getSourceLocation(), t, c, a); 395 } 396 397 public Stmt makeStmt(Expr expr) { 398 return new ExprStmt(getSourceLocation(), expr); 399 } 400 401 public TriTestExpr makeIfExpr(Expr test, Expr trueValue, Expr falseValue) { 402 return new TriTestExpr(getSourceLocation(), test, trueValue, falseValue); 403 } 404 405 public BinopExpr makeBinop(String op, Expr lhs, Expr rhs) { 406 return BinopExpr.build(getSourceLocation(), op, lhs, rhs); 407 } 408 409 public UnopExpr makeUnop(String op, Expr lhs) { 410 return UnopExpr.build(getSourceLocation(), op, lhs); 411 } 412 413 public FieldAccessExpr makeDynamicGet(FieldDec dec) { 414 return makeGet(makeQualifiedThis(dec.getDeclaringType()), dec); 415 } 416 417 public FieldAccessExpr makeStaticGet(Type type, String name) { 418 return new FieldAccessExpr( 419 getSourceLocation(), 420 makeTypeExpr(type), 421 type.getField(name, null, true), 422 false); 423 } 424 425 public Expr forceCast(Type toType, Expr expr) { 426 CastExpr castExpr = new CastExpr(getSourceLocation(), toType.makeTypeD(), expr); 427 if (!castExpr.isPossibleCast()) { 428 expr.showError("can not be cast to " + toType.getString()); 429 } 430 return makeParen(castExpr); 431 } 432 433 public Expr makeCast(Type toType, Expr expr) { 434 Type exprType = expr.getType(); 435 if (exprType == expr.getTypeManager().anyType) { 436 getCompiler().internalError(expr, "expr is anytype: " + expr); 437 } 438 if (exprType instanceof NullType) { 439 return forceCast(toType, expr); 440 } 441 if (exprType.isEquivalent(toType) || toType.isAssignableFrom(exprType)) { 442 if (expr instanceof CastExpr) { 443 return makeParen(expr); 444 } else { 445 return expr; 446 } 447 } 448 return forceCast(toType, expr); 449 } 450 451 public Expr makeNotInstanceofTest(Expr expr, Type ofType) { 452 if (expr.getType().isSubtypeOf(ofType)) 454 return makeLiteral(true); 455 return makeUnop("!", makeParen(makeInstanceof(expr, ofType))); 456 } 457 458 public Expr makeInstanceof(Expr expr, Type ofType) { 459 return new InstanceofExpr(getSourceLocation(), expr, ofType.makeTypeD()); 461 } 462 463 465 public ThisExpr makeThis(Type thisType) { 467 return new ThisExpr(getSourceLocation(), thisType); 468 } 469 470 public QualifiedThisExpr makeQualifiedThis(TypeD typeD) { 471 return new QualifiedThisExpr(getSourceLocation(), typeD); 472 } 473 public QualifiedThisExpr makeQualifiedThis(Type type) { 474 return makeQualifiedThis(type.makeTypeD()); 475 } 476 477 public TypeExpr makeTypeExpr(TypeD typeD) { 478 return new TypeExpr(getSourceLocation(), typeD); 479 } 480 public TypeExpr makeTypeExpr(Type type) { 481 return makeTypeExpr(type.makeTypeD()); 482 } 483 484 public Expr makeNonNullTest(Expr expr) { 485 return makeBinop("!=", expr, makeNull()); 486 } 487 public Expr makeNullTest(Expr expr) { 488 return makeBinop("==", expr, makeNull()); 489 } 490 491 public BooleanLiteralExpr makeLiteral(boolean b) { 492 return new BooleanLiteralExpr(getSourceLocation(), b); 493 } 494 495 public IntLiteralExpr makeLiteral(int i) { 496 return new IntLiteralExpr(getSourceLocation(), i); 497 } 498 499 public LongLiteralExpr makeLiteral(long l) { 500 return new LongLiteralExpr(getSourceLocation(), l); 501 } 502 503 public FloatLiteralExpr makeLiteral(float f) { 504 return new FloatLiteralExpr(getSourceLocation(), f); 505 } 506 507 public DoubleLiteralExpr makeLiteral(double d) { 508 return new DoubleLiteralExpr(getSourceLocation(), d); 509 } 510 511 public StringLiteralExpr makeLiteral(String s) { 512 return new StringLiteralExpr(getSourceLocation(), s); 513 } 514 515 public Expr makeLiteral(Type type) { 516 return type.getClassExpr(); 517 } 518 519 public NullExpr makeNull() { 520 return new NullExpr(getSourceLocation()); 521 } 522 523 public Exprs makeVars(Formals formals) { 524 return makeVars(formals, 0); 525 } 526 527 public Exprs makeVars(Formals formals, int i) { 528 Exprs es = makeExprs(); 529 for (int len = formals.size(); i < len; i++) { 530 es.add(makeVar(formals.get(i))); 531 } 532 return es; 533 } 534 535 public Expr makeNotFoundExpr() { 536 return makeVar(makeVarDec(getTypeManager().TYPE_NOT_FOUND, "not_found", null)); 537 } 538 539 public VarExpr makeVar(VarDec dec) { 540 return new VarExpr(getSourceLocation(), dec); 541 } 542 543 public Expr makeParen(Expr expr) { 544 return new ParenExpr(getSourceLocation(), expr); 545 } 546 547 public AssignExpr makeSet(AssignableExpr lhs, Expr rhs) { 548 return AssignExpr.build(getSourceLocation(), lhs, "", rhs); 549 } 550 551 public AssignExpr makeSet(AssignableExpr lhs, String op, Expr rhs) { 552 return AssignExpr.build(getSourceLocation(), lhs, op, rhs); 553 } 554 555 public AssignExpr makeSet(VarDec vd, Expr rhs) { 556 return AssignExpr.build(getSourceLocation(), makeVar(vd), "", rhs); 557 } 558 public AssignExpr makeSet(FieldDec fd, Expr rhs) { 559 return makeSet(fd.getField(), rhs); 560 } 561 public AssignExpr makeSet(Field fd, Expr rhs) { 562 return AssignExpr.build(getSourceLocation(), makeGet(fd), "", rhs); 563 } 564 565 public AssignExpr makeSet(Expr i, Field field, Expr rhs) { 566 return AssignExpr.build(getSourceLocation(), makeGet(i, field), "", rhs); 567 } 568 569 public AssignExpr makeSet(Expr i, FieldDec field, Expr rhs) { 570 return makeSet(i, field.getField(), rhs); 571 } 572 public FieldAccessExpr makeGet(Expr i, FieldDec field) { 573 return makeGet(i, field.getField()); 574 } 575 public FieldAccessExpr makeGet(FieldDec field) { 576 return makeGet(field.getField()); 577 } 578 public FieldAccessExpr makeGet(Expr i, Field field) { 579 if (i == null) { 580 field.getFieldDec().showError("INTERNAL: instance null in get"); 581 return makeGet(field); 582 } 583 return new FieldAccessExpr(getSourceLocation(), i, field, false); 584 } 585 586 public PrefixExpr makePrefix(Expr i, FieldDec field, String op) { 587 return new PrefixExpr(getSourceLocation(), makeGet(i, field), op); 588 } 589 public PostfixExpr makePostfix(Expr i, FieldDec field, String op) { 590 return new PostfixExpr(getSourceLocation(), makeGet(i, field), op); 591 } 592 593 public Expr makePrimary(SemanticObject o, NameType referenceType) { 594 NameType declaringType = (NameType) o.getDeclaringType(); 595 return makePrimary(declaringType, referenceType, o.isStatic()); 596 } 597 598 public Expr makePrimary(SemanticObject o, TypeDec referenceDec) { 599 NameType declaringType = (NameType) o.getDeclaringType(); 600 NameType referenceType = (NameType) referenceDec.getType(); 601 return makePrimary(declaringType, referenceType, o.isStatic()); 602 } 603 604 public Expr makePrimary(Dec dec, TypeDec referenceDec) { 605 NameType declaringType = (NameType) dec.getDeclaringType(); 606 NameType referenceType = (NameType) referenceDec.getType(); 607 return makePrimary(declaringType, referenceType, dec.isStatic()); 608 } 609 610 public Expr makePrimary(Dec dec, NameType referenceType) { 611 NameType declaringType = (NameType) dec.getDeclaringType(); 612 return makePrimary(declaringType, referenceType, dec.isStatic()); 613 } 614 615 public Expr makePrimary(NameType declaring, NameType reference, boolean isStatic) { 616 return makePrimary(declaring, reference, isStatic, true); 617 } 618 619 public Expr makePrimary( 620 NameType declaring, 621 NameType reference, 622 boolean isStatic, 623 boolean isThisAvailable) { 624 if (reference.isSubtypeOf(declaring) && isStatic) { 626 return makeTypeExpr(reference); 627 } 628 if (reference.isSubtypeOf(declaring) && isThisAvailable) { 629 return makeThis(reference); 630 } 631 632 NameType curr = reference; 636 while (!curr.isPackageMember()) { 637 curr = curr.getEnclosingType(); 638 if (curr.isSubtypeOf(declaring)) { 639 return isStatic ? makeTypeExpr(curr) : (Expr) makeQualifiedThis(curr); 640 } 641 } 642 throw new RuntimeException ( 643 "No enclosing type for " + declaring + " and " + reference); 644 } 645 646 public Expr makeEnclosingPrimary(NameType declaring, NameType reference) { 647 NameType curr = reference; 648 while (!curr.isPackageMember()) { 649 curr = curr.getEnclosingType(); 650 if (curr.isSubtypeOf(declaring)) { 651 return makeQualifiedThis(curr); 652 } 653 } 654 throw new RuntimeException ( 655 "No enclosing type for " + declaring + " and " + reference); 656 } 657 658 public FieldAccessExpr makeGet(Field field) { 659 return makeGet( 665 field.isStatic() 666 ? (Expr) makeTypeExpr(field.getDeclaringType()) 667 : makeThis(null), 668 field); 669 } 670 671 public Decs makeDecs() { 672 return new Decs(getSourceLocation()); 673 } 674 public TypeDs makeTypeDs() { 675 return new TypeDs(getSourceLocation()); 676 } 677 public TypeDs makeTypeDs(TypeD ty0) { 678 return new TypeDs(getSourceLocation(), ty0); 679 } 680 681 public NewArrayExpr makeArray(Type type, Exprs values) { 682 return new NewArrayExpr( 683 getSourceLocation(), 684 new ResolvedTypeD(getSourceLocation(), type.getArrayType() 685 ), new ArrayInitializer(getSourceLocation(), values), null); 687 } 688 689 public NewArrayExpr makeObjectArray(Exprs values) { 690 Exprs objectExprs = new Exprs(getSourceLocation()); 691 692 for (int i = 0; i < values.size(); i++) { 693 Expr value = values.get(i); 694 objectExprs.add(value.getType().makeObject(value)); 695 } 696 697 return makeArray(getTypeManager().getObjectType(), objectExprs); 698 } 699 700 public Modifiers makeModifiers(int value) { 701 return new Modifiers(getSourceLocation(), value); 702 } 703 704 public FormalDec makeFormal(Type type, String id) { 705 return makeFormal(type, id, false); 706 } 707 public FormalDec makeFinalFormal(Type type, String id) { 708 return makeFormal(type, id, true); 709 } 710 711 public FormalDec makeFormal(Type type, String id, boolean isFinal) { 712 return new FormalDec( 713 getSourceLocation(), 714 makeModifiers(isFinal ? Modifiers.FINAL : 0), 715 type.makeTypeD(), 716 id, 717 null); 718 } 719 720 public Formals makeFormals() { 721 return new Formals(getSourceLocation()); 722 } 723 724 public Formals makeFormals(FormalDec f1) { 725 return new Formals(getSourceLocation(), f1); 726 } 727 728 public Formals makeFormals(FormalDec f1, FormalDec f2) { 729 return new Formals(getSourceLocation(), f1, f2); 730 } 731 public VarDec makeVarDec(Modifiers mods, Type type, String id, Expr init) { 732 return new VarDec(getSourceLocation(), mods, type.makeTypeD(), id, init); 733 } 734 public VarDec makeVarDec(Type type, String id, Expr init, boolean isFinal) { 735 return new VarDec( 736 getSourceLocation(), 737 makeModifiers(isFinal ? Modifiers.FINAL : 0), 738 type.makeTypeD(), 739 id, 740 init); 741 } 742 743 public VarDec makeVarDec(Type type, String id, Expr init) { 744 return new VarDec(getSourceLocation(), type.makeTypeD(), id, init); 745 } 746 public VarDec makeFinalVar(Type type, String id, Expr init) { 747 return new VarDec( 748 getSourceLocation(), 749 makeModifiers(Modifiers.FINAL), 750 type.makeTypeD(), 751 id, 752 init); 753 } 754 755 public ArrayExpr makeArrayRef(Expr arrayExpr, int index) { 756 return new ArrayExpr(getSourceLocation(), arrayExpr, makeLiteral(index)); 757 } 758 759 public FieldDec makeField(Modifiers mods, Type type, String id) { 760 return new FieldDec(getSourceLocation(), mods, type.makeTypeD(), id, null); 761 } 762 763 public FieldDec makeField(Modifiers mods, Type type, String id, Expr init) { 764 return new FieldDec(getSourceLocation(), mods, type.makeTypeD(), id, init); 765 } 766 767 public InitializerDec makeInitializer(Stmts body) { 768 return new InitializerDec( 769 getSourceLocation(), 770 makeModifiers(0), 771 new CodeBody(getSourceLocation(), body, true)); 772 } 773 public InitializerDec makeStaticInitializer(Stmts body) { 774 return new InitializerDec( 775 getSourceLocation(), 776 makeModifiers(Modifiers.STATIC), 777 new CodeBody(getSourceLocation(), body, true)); 778 } 779 780 public MethodDec makeMethod( 781 Modifiers mods, 782 Type retType, 783 String name, 784 Formals formals, 785 BlockStmt body) { 786 MethodDec ret = 787 new MethodDec( 788 getSourceLocation(), 789 mods, 790 retType.makeTypeD(), 791 name, 792 formals, 793 null, 794 body); 795 ret.computeMinimalThrows(); 796 return ret; 797 } 798 799 public CodeBody makeCodeBody(Stmt s) { 800 return new CodeBody(getSourceLocation(), makeStmts(s), true); 801 } 802 803 public CodeBody makeCodeBody(Stmt s0, Stmt s1) { 804 return new CodeBody(getSourceLocation(), makeStmts(s0, s1), true); 805 } 806 807 public IfStmt makeIf(Expr test, Stmt trueStmt, Stmt falseStmt) { 808 return new IfStmt(getSourceLocation(), test, trueStmt, falseStmt); 812 } 813 814 public IfStmt makeIf(Expr test, Stmt trueStmt) { 815 IfStmt s = new IfStmt(getSourceLocation(), test, trueStmt, null); 816 s.setElse((Stmt) s.getAST().makeEmptyStmt()); 817 return s; 818 } 819 820 public TryFinallyStmt makeTryFinally(Stmt block, Stmt finallyBlock) { 821 return new TryFinallyStmt(getSourceLocation(), block, finallyBlock); 822 } 823 824 public TryCatchStmt makeTryCatch( 825 BlockStmt block, 826 FormalDec formal, 827 Stmt catchStmt) { 828 return makeTryCatch( 829 block, 830 new CatchClause(getSourceLocation(), formal, catchStmt)); 831 } 832 833 public TryCatchStmt makeTryCatch(BlockStmt block, CatchClause catchClause) { 834 return new TryCatchStmt( 835 getSourceLocation(), 836 block, 837 new CatchClauses(getSourceLocation(), catchClause)); 838 } 839 840 public WhileStmt makeWhile(Expr test, Stmt body) { 841 return new WhileStmt(getSourceLocation(), test, body); 842 } 843 844 public ReturnStmt makeReturn() { 845 return new ReturnStmt(getSourceLocation()); 846 } 847 848 public ReturnStmt makeReturn(Expr e) { 849 return new ReturnStmt(getSourceLocation(), e); 850 } 851 852 public ThrowStmt makeThrow(Expr e) { 853 return new ThrowStmt(getSourceLocation(), e); 854 } 855 856 public ThrowStmt makeThrow(Type t) { 857 return new ThrowStmt(getSourceLocation(), makeNew(t)); 858 } 859 860 public ThrowStmt makeThrow(Type t, String message) { 861 return new ThrowStmt(getSourceLocation(), makeNew(t, makeLiteral(message))); 862 } 863 864 public CatchClause makeCatch(FormalDec formal, BlockStmt body) { 865 return new CatchClause(getSourceLocation(), formal, body); 866 } 867 868 public LabeledStmt makeLabeled(String label, Stmt stmt) { 869 return new LabeledStmt(getSourceLocation(), label, stmt); 870 } 871 872 public Stmt makeBreak(String label) { 873 return new BreakStmt(getSourceLocation(), label); 874 } 875 876 public Stmt makeContinue(String label) { 877 return new ContinueStmt(getSourceLocation(), label); 878 } 879 880 public BlockStmt makeBlock() { 881 return new BlockStmt(getSourceLocation(), new Stmts(getSourceLocation())); 882 } 883 884 public BlockStmt makeBlock(ASTObject s1) { 885 if (s1 instanceof Stmts) 886 return new BlockStmt(getSourceLocation(), (Stmts) s1); 887 else 888 return new BlockStmt(getSourceLocation(), s1); 889 } 890 891 public BlockStmt makeBlock(ASTObject s1, ASTObject s2) { 892 return new BlockStmt(getSourceLocation(), s1, s2); 893 } 894 895 public BlockStmt makeBlock(ASTObject s1, ASTObject s2, ASTObject s3) { 896 return new BlockStmt(getSourceLocation(), s1, s2, s3); 897 } 898 899 public BlockStmt makeBlock( 900 ASTObject s1, 901 ASTObject s2, 902 ASTObject s3, 903 ASTObject s4) { 904 return new BlockStmt(getSourceLocation(), s1, s2, s3, s4); 905 } 906 907 public BlockStmt makeBlock(Stmts stmts) { 908 return new BlockStmt(getSourceLocation(), stmts); 909 } 910 911 public Stmts makeStmts() { 912 return new Stmts(getSourceLocation()); 913 } 914 915 public Stmts makeStmts(Stmt s1) { 916 return new Stmts(getSourceLocation(), s1); 917 } 918 919 public Stmts makeStmts(Stmt s1, Stmt s2) { 920 return new Stmts(getSourceLocation(), s1, s2); 921 } 922 923 public Stmts makeStmts(Stmt s1, Stmt s2, Stmt s3) { 924 return new Stmts(getSourceLocation(), s1, s2, s3); 925 } 926 927 public EmptyStmt makeEmptyStmt() { 928 return new EmptyStmt(getSourceLocation()); 929 } 930 931 public InterfaceDec makeInnerInterface( 932 TypeDec outerTypeDec, 933 Modifiers modifiers, 934 String name, 935 TypeDs _implements) { 936 InterfaceDec ret = 937 new InterfaceDec( 938 getSourceLocation(), 939 modifiers, 940 name, 941 _implements, 942 new Decs(getSourceLocation())); 943 outerTypeDec.getBody().add(ret); 945 return ret; 946 } 947 948 public InterfaceDec makeInterface( 949 Modifiers modifiers, 950 String name, 951 TypeDs _implements) { 952 InterfaceDec ret = 953 new InterfaceDec( 954 getSourceLocation(), 955 modifiers, 956 name, 957 _implements, 958 new Decs(getSourceLocation())); 959 return ret; 961 } 962 963 966 public ClassDec makeClass( 967 Modifiers modifiers, 968 String name, 969 TypeD superClass, 970 TypeDs _implements) { 971 ClassDec ret = 972 new ClassDec( 973 getSourceLocation(), 974 modifiers, 975 name, 976 superClass, 977 _implements, 978 new Decs(getSourceLocation())); 979 return ret; 981 } 982 983 public AnonymousMethodExpr makeAnonMethod(Type retType, Stmts stmts) { 984 return new AnonymousMethodExpr( 985 getSourceLocation(), 986 makeExprs(), 987 makeMethod(makeModifiers(0), retType, "anon", makeFormals(), makeBlock(stmts))); 988 } 989 } | Popular Tags |