KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > UnopExpr


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.ast;
26
27 import org.aspectj.compiler.base.FlowCheckerPass;
28 import org.aspectj.compiler.base.AssignmentCheckerPass;
29 import org.aspectj.compiler.base.JavaCompiler;
30 import org.aspectj.compiler.base.CodeWriter;
31 import org.aspectj.compiler.base.cst.*;
32 import org.aspectj.compiler.base.bcg.CodeBuilder;
33
34 import java.util.*;
35
36
37 /**
38  * @grammar <op> rand1
39  * @property String op
40  * @child Expr rand1
41  *
42  */

43 public abstract class UnopExpr extends Expr {
44
45     protected void bindNamesSelf() {
46         //setType(discoverType());
47
}
48
49     public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) {
50         return tryToFold();
51     }
52
53     private ASTObject tryToFold() {
54         if (getLiftType().isAnyType()) return this;
55         if (rand1 instanceof LiteralExpr) {
56             return halfFold(getLiftType(), (LiteralExpr)rand1).setSource(this);
57         } else {
58             return this;
59         }
60     }
61
62     abstract protected Type getLiftType();
63
64     /** returns a new unfinished ASTObject, that still needs to have
65         {@link ASTObject#setSource} called on it. */

66     abstract protected LiteralExpr halfFold(Type type, LiteralExpr lit);
67
68     public static UnopExpr build(SourceLocation source, String JavaDoc op, Expr rand1) {
69         if (op.equals("+")) {
70             return new PlusOpExpr(source, op, rand1);
71         } else if (op.equals("-")) {
72             return new MinusOpExpr(source, op, rand1);
73         } else if (op.equals("~")) {
74             return new BitNotOpExpr(source, op, rand1);
75         } else if (op.equals("!")) {
76             return new LogNotOpExpr(source, op, rand1);
77         } else {
78             throw new RuntimeException JavaDoc("strange unary op: " + op);
79         }
80     }
81
82     protected Type discoverType() {
83         //??? is this true for all unops?
84
return getRand1().getType();
85     }
86
87     public void unparse(CodeWriter writer) {
88         writer.write(getOp());
89         writer.write(getRand1());
90     }
91
92     public void showOperatorTypeError(Type a) {
93         if (a.isAnyType()) return;
94         showError("operator " + op + " cannot be applied to " + a.getString());
95     }
96
97     // ------------------------------
98
// bcg
99

100     protected void cgEffect(CodeBuilder cb) {
101         rand1.cgEffect(cb);
102     }
103
104     //BEGIN: Generated from @child and @property
105
protected String JavaDoc op;
106     public String JavaDoc getOp() { return op; }
107     public void setOp(String JavaDoc _op) { op = _op; }
108
109     protected Expr rand1;
110     public Expr getRand1() { return rand1; }
111     public void setRand1(Expr _rand1) {
112         if (_rand1 != null) _rand1.setParent(this);
113         rand1 = _rand1;
114     }
115
116     public UnopExpr(SourceLocation location, String JavaDoc _op, Expr _rand1) {
117         super(location);
118         setOp(_op);
119         setRand1(_rand1);
120     }
121     protected UnopExpr(SourceLocation source) {
122         super(source);
123     }
124
125     public ASTObject getChildAt(int childIndex) {
126         switch(childIndex) {
127         case 0: return rand1;
128         default: return super.getChildAt(childIndex);
129         }
130     }
131      public String JavaDoc getChildNameAt(int childIndex) {
132         switch(childIndex) {
133         case 0: return "rand1";
134         default: return super.getChildNameAt(childIndex);
135         }
136     }
137      public void setChildAt(int childIndex, ASTObject child) {
138         switch(childIndex) {
139         case 0: setRand1((Expr)child); return;
140         default: super.setChildAt(childIndex, child); return;
141         }
142     }
143      public int getChildCount() {
144         return 1;
145     }
146
147     public String JavaDoc getDefaultDisplayName() {
148         return "UnopExpr(op: "+op+")";
149     }
150
151     //END: Generated from @child and @property
152
}
153
Popular Tags