comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/ast/CodeTreeBuilder.java @ 7846:91cc98eae8ee

Refactor guard creation methods are not flexible enough to handle two if guards.
author Christian Humer <christian.humer@gmail.com>
date Tue, 19 Feb 2013 14:29:12 +0100
parents 5e3d1a68664e
children 9aea719e6e98
comparison
equal deleted inserted replaced
7845:0110e781b6fa 7846:91cc98eae8ee
31 31
32 import com.oracle.truffle.codegen.processor.*; 32 import com.oracle.truffle.codegen.processor.*;
33 33
34 public class CodeTreeBuilder { 34 public class CodeTreeBuilder {
35 35
36 private final CodeTreeBuilder parent;
37
36 private BuilderCodeTree currentElement; 38 private BuilderCodeTree currentElement;
37 private final BuilderCodeTree root; 39 private final BuilderCodeTree root;
38 40
39 public CodeTreeBuilder() { 41 private int treeCount;
42
43 public CodeTreeBuilder(CodeTreeBuilder parent) {
40 this.root = new BuilderCodeTree(GROUP, null, null); 44 this.root = new BuilderCodeTree(GROUP, null, null);
41 this.currentElement = root; 45 this.currentElement = root;
42 } 46 this.parent = parent;
43 47 }
44 public CodeTreeBuilder(CodeTree tree) { 48
45 this.root = (BuilderCodeTree) tree; 49 public int getTreeCount() {
46 this.currentElement = root; 50 return treeCount;
51 }
52
53 public boolean isEmpty() {
54 return treeCount == 0;
47 } 55 }
48 56
49 public CodeTreeBuilder statement(String statement) { 57 public CodeTreeBuilder statement(String statement) {
50 return startStatement().string(statement).end(); 58 return startStatement().string(statement).end();
51 } 59 }
53 public CodeTreeBuilder statement(CodeTree statement) { 61 public CodeTreeBuilder statement(CodeTree statement) {
54 return startStatement().tree(statement).end(); 62 return startStatement().tree(statement).end();
55 } 63 }
56 64
57 public static CodeTreeBuilder createBuilder() { 65 public static CodeTreeBuilder createBuilder() {
58 return new CodeTreeBuilder(); 66 return new CodeTreeBuilder(null);
59 } 67 }
60 68
61 public static CodeTree singleString(String s) { 69 public static CodeTree singleString(String s) {
62 return new CodeTreeBuilder().string(s).getTree(); 70 return new CodeTreeBuilder(null).string(s).getTree();
63 } 71 }
64 72
65 private CodeTreeBuilder push(CodeTreeKind kind) { 73 private CodeTreeBuilder push(CodeTreeKind kind) {
66 return push(new BuilderCodeTree(kind, null, null)); 74 return push(new BuilderCodeTree(kind, null, null));
67 } 75 }
87 case GROUP: 95 case GROUP:
88 case INDENT: 96 case INDENT:
89 currentElement = tree; 97 currentElement = tree;
90 break; 98 break;
91 } 99 }
100 treeCount++;
92 return this; 101 return this;
93 } 102 }
94 103
95 private void clearLast(CodeTreeKind kind) { 104 private void clearLast(CodeTreeKind kind) {
96 clearLastRec(kind, currentElement.getEnclosedElements()); 105 if (clearLastRec(kind, currentElement.getEnclosedElements())) {
106 treeCount--;
107 }
97 } 108 }
98 109
99 public CodeTreeBuilder startStatement() { 110 public CodeTreeBuilder startStatement() {
100 startGroup(); 111 startGroup();
101 registerCallBack(new EndCallback() { 112 registerCallBack(new EndCallback() {
356 367
357 public CodeTreeBuilder startIndention() { 368 public CodeTreeBuilder startIndention() {
358 return push(CodeTreeKind.INDENT); 369 return push(CodeTreeKind.INDENT);
359 } 370 }
360 371
372 public CodeTreeBuilder end(int times) {
373 for (int i = 0; i < times; i++) {
374 end();
375 }
376 return this;
377 }
378
361 public CodeTreeBuilder end() { 379 public CodeTreeBuilder end() {
362 BuilderCodeTree tree = currentElement; 380 BuilderCodeTree tree = currentElement;
363 EndCallback callback = tree.getAtEndListener(); 381 EndCallback callback = tree.getAtEndListener();
364 if (callback != null) { 382 if (callback != null) {
365 callback.beforeEnd(); 383 callback.beforeEnd();
370 } 388 }
371 return this; 389 return this;
372 } 390 }
373 391
374 private void toParent() { 392 private void toParent() {
375 Element parent = currentElement.getEnclosingElement(); 393 Element parentElement = currentElement.getEnclosingElement();
376 if (currentElement != root) { 394 if (currentElement != root) {
377 this.currentElement = (BuilderCodeTree) parent; 395 this.currentElement = (BuilderCodeTree) parentElement;
378 } else { 396 } else {
379 this.currentElement = root; 397 this.currentElement = root;
380 } 398 }
381 } 399 }
382 400
431 public CodeTreeBuilder declaration(TypeMirror type, String name) { 449 public CodeTreeBuilder declaration(TypeMirror type, String name) {
432 return declaration(type, name, (CodeTree) null); 450 return declaration(type, name, (CodeTree) null);
433 } 451 }
434 452
435 public CodeTreeBuilder create() { 453 public CodeTreeBuilder create() {
436 return new CodeTreeBuilder(); 454 return new CodeTreeBuilder(null);
437 } 455 }
438 456
439 public CodeTreeBuilder type(TypeMirror type) { 457 public CodeTreeBuilder type(TypeMirror type) {
440 return push(type); 458 return push(type);
441 } 459 }
494 public ExecutableElement findMethod() { 512 public ExecutableElement findMethod() {
495 Element element = currentElement; 513 Element element = currentElement;
496 while (element != null && (element.getKind() != ElementKind.METHOD)) { 514 while (element != null && (element.getKind() != ElementKind.METHOD)) {
497 element = element.getEnclosingElement(); 515 element = element.getEnclosingElement();
498 } 516 }
499 return element != null ? (ExecutableElement) element : null; 517 ExecutableElement found = element != null ? (ExecutableElement) element : null;
518 if (found == null && parent != null) {
519 found = parent.findMethod();
520 }
521 return found;
500 } 522 }
501 523
502 public CodeTreeBuilder returnTrue() { 524 public CodeTreeBuilder returnTrue() {
503 return startReturn().string("true").end(); 525 return startReturn().string("true").end();
504 } 526 }