clean code
[cdsspec-compiler.git] / src / edu / uci / eecs / specExtraction / FunctionHeader.java
1 package edu.uci.eecs.specExtraction;
2
3 import java.util.ArrayList;
4
5 /**
6  * <p>
7  * This represents a function declaration header. For example,
8  * "void myFunction(int arg1, bool arg2)" is a function declaration header.
9  * </p>
10  * 
11  * @author Peizhao Ou
12  * 
13  */
14 public class FunctionHeader {
15         private ArrayList<VariableDeclaration> templateList;
16
17         // The return type of the function
18         public final String returnType;
19         // The function name
20         public final QualifiedName funcName;
21         // The argument list (with formal parameter names)
22         public final ArrayList<VariableDeclaration> args;
23
24         // The actually function definition line in plain text.
25         // E.g. "void myFunction(int arg1, bool arg2) {"
26         private String headerLine;
27
28         public FunctionHeader(String returnType, QualifiedName qualifiedName,
29                         ArrayList<VariableDeclaration> args) {
30                 this.templateList = null;
31                 this.returnType = returnType;
32                 this.funcName = qualifiedName;
33                 this.args = args;
34         }
35
36         /**
37          * 
38          * @return Whether the return type is void
39          */
40         public boolean isReturnVoid() {
41                 return returnType.equals("void");
42         }
43
44         public void setTemplateList(ArrayList<VariableDeclaration> templateList) {
45                 this.templateList = templateList;
46         }
47
48         public ArrayList<VariableDeclaration> getTemplateList() {
49                 return this.templateList;
50         }
51
52         public String getTemplateFullStr() {
53                 String templateStr = "";
54                 if (templateList == null)
55                         return templateStr;
56                 VariableDeclaration decl;
57                 decl = templateList.get(0);
58                 templateStr = "<" + decl.type + " " + decl.name;
59                 for (int i = 1; i < templateList.size(); i++) {
60                         decl = templateList.get(i);
61                         templateStr = templateStr + ", " + decl.type + " " + decl.name;
62                 }
63                 templateStr = templateStr + ">";
64                 return templateStr;
65         }
66
67         public String getTemplateArgStr() {
68                 String templateStr = null;
69                 if (templateList.size() == 0)
70                         return templateStr;
71                 templateStr = "<" + templateList.get(0).name;
72                 for (int i = 1; i < templateList.size(); i++) {
73                         templateStr = templateStr + ", " + templateList.get(i);
74                 }
75                 templateStr = templateStr + ">";
76                 return templateStr;
77         }
78
79         public String getFuncStr() {
80                 String res = returnType + " " + funcName.fullName + "(";
81                 if (args.size() >= 1) {
82                         res = res + args.get(0);
83                 }
84                 for (int i = 1; i < args.size(); i++) {
85                         res = res + ", " + args.get(i);
86                 }
87                 res = res + ")";
88                 return res;
89         }
90
91         public String toString() {
92                 String res = returnType + " " + funcName.fullName + "(";
93                 if (args.size() >= 1) {
94                         res = res + args.get(0);
95                 }
96                 for (int i = 1; i < args.size(); i++) {
97                         res = res + ", " + args.get(i);
98                 }
99                 res = res + ")";
100                 return res;
101         }
102
103         public String getRenamedFuncName() {
104                 return funcName.qualifiedName + SpecNaming.WrapperPrefix + "_"
105                                 + funcName.bareName;
106         }
107
108         public FunctionHeader getRenamedHeader(String prefix) {
109                 String newFullName = getRenamedFuncName();
110                 FunctionHeader newHeader = new FunctionHeader(returnType,
111                                 new QualifiedName(newFullName), args);
112                 return newHeader;
113         }
114
115         public FunctionHeader getRenamedHeader() {
116                 return getRenamedHeader(SpecNaming.WrapperPrefix);
117         }
118
119         /**
120          * 
121          * @return The string that represents the renamed function header line. For
122          *         example, we would return
123          *         <code>"bool Wrapper_myFunc(int x, int y)"</code> for the fucntion
124          *         <code>"bool myFunc(int x, int y) {"</code>
125          */
126         public String getRenamedFuncLine() {
127                 String bareName = this.funcName.bareName;
128                 String newName = this.getRenamedFuncName();
129                 return this.headerLine.replaceFirst(bareName, newName);
130         }
131
132         /**
133          * 
134          * @return The string that represents the renamed function call. For
135          *         example, we would return <code>"bool RET = myFunc(x, y)"</code>
136          *         for the fucntion <code>"bool myFunc(int x, int y)"</code>
137          */
138         public String getRenamedCall() {
139                 return getRenamedCall(SpecNaming.WrapperPrefix);
140         }
141
142         /**
143          * 
144          * @return The original plain text line of the function header
145          */
146         public String getHeaderLine() {
147                 return headerLine;
148         }
149         
150         
151         // No support for template right now
152         public String getDeclaration() {
153                 String res = returnType + " " + funcName.fullName + "(";
154                 if (args.size() >= 1) {
155                         res = res + args.get(0).type + " " + args.get(0).name;
156                 }
157                 for (int i = 1; i < args.size(); i++) {
158                         res = res + ", " + args.get(i).type + " " + args.get(i).name;
159                 }
160                 res = res + ")";
161                 return res;
162         }
163
164         public String getRenamedCall(String prefix) {
165                 String res = "";
166                 if (!isReturnVoid()) {
167                         res = res + "bool " + SpecNaming.RET + " = ";
168                 }
169                 res = res + prefix + "_" + funcName.fullName + "(";
170                 if (args.size() >= 1) {
171                         res = res + args.get(0).name;
172                 }
173                 for (int i = 1; i < args.size(); i++) {
174                         res = res + ", " + args.get(i).name;
175                 }
176                 res = res + ")";
177                 return res;
178         }
179
180         public void setHeaderLine(String headerLine) {
181                 this.headerLine = headerLine;
182         }
183 }