01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.jjs.ast.js;
17:
18: import com.google.gwt.dev.jjs.ast.Context;
19: import com.google.gwt.dev.jjs.ast.JClassType;
20: import com.google.gwt.dev.jjs.ast.JExpression;
21: import com.google.gwt.dev.jjs.ast.JNode;
22: import com.google.gwt.dev.jjs.ast.JProgram;
23: import com.google.gwt.dev.jjs.ast.JType;
24: import com.google.gwt.dev.jjs.ast.JVisitor;
25:
26: import java.util.ArrayList;
27: import java.util.List;
28:
29: /**
30: * Represents a JS construct that should be emitted as a JSON-style object.
31: */
32: public class JsonObject extends JExpression {
33:
34: /**
35: * An individual property initializer within a JSON object initializer.
36: */
37: public static class JsonPropInit extends JNode {
38:
39: public JExpression labelExpr;
40: public JExpression valueExpr;
41:
42: public JsonPropInit(JProgram program, JExpression labelExpr,
43: JExpression valueExpr) {
44: super (program, null);
45: this .labelExpr = labelExpr;
46: this .valueExpr = valueExpr;
47: }
48:
49: public void traverse(JVisitor visitor, Context ctx) {
50: if (visitor.visit(this , ctx)) {
51: labelExpr = visitor.accept(labelExpr);
52: valueExpr = visitor.accept(valueExpr);
53: }
54: visitor.endVisit(this , ctx);
55: }
56: }
57:
58: public final List<JsonPropInit> propInits = new ArrayList<JsonPropInit>();
59:
60: public JsonObject(JProgram program) {
61: super (program, null);
62: }
63:
64: public JType getType() {
65: // If JavaScriptObject type is not available, just return the Object type
66: JClassType jsoType = program.getJavaScriptObject();
67: return (jsoType != null) ? jsoType : program
68: .getTypeJavaLangObject();
69: }
70:
71: public boolean hasSideEffects() {
72: for (JsonPropInit propInit : propInits) {
73: if (propInit.labelExpr.hasSideEffects()
74: || propInit.valueExpr.hasSideEffects()) {
75: return true;
76: }
77: }
78: return false;
79: }
80:
81: public void traverse(JVisitor visitor, Context ctx) {
82: if (visitor.visit(this, ctx)) {
83: visitor.accept(propInits);
84: }
85: visitor.endVisit(this, ctx);
86: }
87:
88: }
|