1 package gnu.expr; 2 import gnu.bytecode.*; 3 import gnu.kawa.reflect.Invoke; 4 5 public class InlineCalls extends ExpWalker 6 { 7 public static void inlineCalls (Expression exp, Compilation comp) 8 { 9 InlineCalls walker = new InlineCalls(comp); 10 walker.walk(exp); 11 } 12 13 public InlineCalls (Compilation comp) 14 { 15 setContext(comp); 16 } 17 18 protected Expression walkApplyExp(ApplyExp exp) 19 { 20 super.walkApplyExp(exp); 21 return walkApplyOnly(exp); 22 } 23 24 25 public Expression walkApplyOnly(ApplyExp exp) 26 { 27 return exp.func.inline(exp, this, null); 28 } 29 30 protected Expression walkReferenceExp (ReferenceExp exp) 31 { 32 Declaration decl = exp.getBinding(); 33 if (decl != null && decl.getFlag(Declaration.IS_CONSTANT) 34 && decl.field == null) 35 { 36 Expression dval = decl.getValue(); 37 if (dval instanceof QuoteExp && dval != QuoteExp.undefined_exp) 38 return walkQuoteExp((QuoteExp) dval); 39 } 40 return super.walkReferenceExp(exp); 41 } 42 43 protected Expression walkIfExp (IfExp exp) 44 { 45 Expression test = exp.test.walk(this); 46 if (test instanceof ReferenceExp) 47 { 48 Declaration decl = ((ReferenceExp) test).getBinding(); 49 if (decl != null) 50 { 51 Expression value = decl.getValue(); 52 if (value instanceof QuoteExp && value != QuoteExp.undefined_exp) 53 test = value; 54 } 55 } 56 if (test instanceof QuoteExp) 57 { 58 return walk(comp.getLanguage().isTrue(((QuoteExp) test).getValue()) 59 ? exp.then_clause 60 : exp.else_clause == null ? QuoteExp.voidExp 61 : exp.else_clause); 62 } 63 exp.test = test; 64 if (exitValue == null) 65 exp.then_clause = walk(exp.then_clause); 66 if (exitValue == null && exp.else_clause != null) 67 exp.else_clause = walk(exp.else_clause); 68 return exp; 69 } 70 71 protected Expression walkLetExp (LetExp exp) 72 { 73 Declaration decl = exp.firstDecl(); 74 int n = exp.inits.length; 75 for (int i = 0; i < n; i++, decl = decl.nextDecl()) 76 { 77 Expression init0 = exp.inits[i]; 78 Expression init = walk(init0); 79 exp.inits[i] = init; 80 Expression dvalue = decl.value; 81 if (dvalue == init0) 82 { 83 decl.value = dvalue = init; 84 if (! decl.getFlag(Declaration.TYPE_SPECIFIED)) 85 decl.setType(dvalue.getType()); 86 } 87 } 88 89 if (exitValue == null) 90 exp.body = (Expression) walk(exp.body); 91 if (exp.body instanceof ReferenceExp) 92 { 93 ReferenceExp ref = (ReferenceExp) exp.body; 94 Declaration d = ref.getBinding(); 95 if (d.context == exp && ! ref.getDontDereference()) 96 { 97 if ( n == 1) 98 return exp.inits[0]; 99 } 102 } 103 return exp; 104 } 105 106 protected Expression walkLambdaExp (LambdaExp exp) 107 { 108 Declaration firstDecl = exp.firstDecl(); 109 if (firstDecl != null && firstDecl.isThisParameter() 110 && ! exp.isClassMethod() && firstDecl.getType() == null) 111 { 112 firstDecl.setType(comp.mainClass); 113 } 114 return walkScopeExp(exp); 115 } 116 117 135 } 136 | Popular Tags |