View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU Lesser General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU Lesser General Public License for more details.
15  	
16  	You should have received a copy of the GNU Lesser General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
18  */
19  package com.ochafik.lang.jnaerator.parser;
20  
21  import com.ochafik.lang.jnaerator.parser.Declarator.MutableByDeclarator;
22  import com.ochafik.util.string.StringUtils;
23  
24  public class Arg extends Declaration {
25  	String selector;
26  	boolean varArg;
27  	Declarator declarator;
28  	Expression defaultValue;
29  
30  	public Arg(String name, TypeRef type) {
31  		this();
32  		setName(name);
33  		setValueType(type);
34  	}
35  	
36  	public Arg() {
37  	}
38  	
39  	@Override
40  //	@Deprecated
41  	public TypeRef getValueType() {
42  		return super.getValueType();
43  	}
44  	
45  	public TypeRef createMutatedType() {
46  		TypeRef vt = getValueType();
47  		if (vt == null)
48  			return null;
49  		if (getDeclarator() != null) {
50  			MutableByDeclarator type = getDeclarator().mutateType(vt);
51  			if (type instanceof TypeRef)
52  				return (TypeRef)getDeclarator().mutateType(vt);
53  			else {
54  				assert false;
55  				return null;
56  			}
57  		}
58  		return vt;
59  	}
60  	
61  	public Declarator getDeclarator() {
62  		return declarator;
63  	}
64  	public void setDeclarator(Declarator declarator) {
65  		this.declarator = changeValue(this, this.declarator, declarator);
66  	}
67  	public String getName() {
68  		return declarator == null ? null : declarator.resolveName();
69  	}
70  	public void setName(String name) {
71  		if (declarator == null)
72  			setDeclarator(new Declarator.DirectDeclarator(name));
73  		else
74  			declarator.propagateName(name);
75  	}
76  	@Override
77  	public Arg clone() {
78  		return (Arg)super.clone();
79  	}
80  
81  	public Expression getDefaultValue() {
82  		return defaultValue;
83  	}
84  	public void setDefaultValue(Expression defaultValue) {
85  		this.defaultValue = changeValue(this, this.defaultValue, defaultValue);
86  	}
87  	
88  	public static Arg createVarArgs() {
89  		Arg a = new Arg(null, null);
90  		a.varArg = true;
91  		return a;
92  	}
93  	
94  	public String getSelector() {
95  		return selector;
96  	}
97  
98  	public void setSelector(String selector) {
99  		this.selector = selector;
100 	}
101 	
102 	@Override
103 	public boolean replaceChild(Element child, Element by) {
104 		if (child == getDefaultValue()) {
105 			setDefaultValue((Expression) by);
106 			return true;
107 		}
108 		if (child == getDeclarator()) {
109 			setDeclarator((Declarator) by);
110 			return true;
111 		}
112 		return super.replaceChild(child, by);
113 	}
114 	
115 	@Override
116 	public String toString(CharSequence indent) {
117 		//if (isVarArg())
118 		//	return "...";
119 		///else
120 		String ann = "";
121 		if (!getAnnotations().isEmpty())
122 			ann = StringUtils.implode(getAnnotations(), "\n" + indent) + "\n" + indent;
123 		
124 		if (getValueType() == null)
125 			return null;
126 		else if (getValueType() != null) {
127 			if (getName() != null)
128 				return ann + getValueType().variableDeclarationToString(getName(), isVarArg(), indent);
129 			else
130 				return ann + getValueType().toString() + (isVarArg() ? "..." : "");
131 		} else
132 			return "...";
133 	}
134 
135 	public void accept(Visitor visitor) {
136 		visitor.visitArg(this);
137 	}
138 
139 	public boolean isVarArg() {
140 		return varArg;
141 	}
142 	public void setVarArg(boolean v) {
143 		varArg = v;
144 	}
145 }