39package com.jogamp.gluegen.procaddress;
41import static java.util.logging.Level.INFO;
43import com.jogamp.gluegen.JavaConfiguration;
44import com.jogamp.gluegen.cgram.types.AliasedSymbol;
45import com.jogamp.gluegen.cgram.types.FunctionSymbol;
53 private boolean emitProcAddressTable =
false;
54 private boolean forceProcAddressGen4All =
false;
56 private String tableClassPackage;
57 private String tableClassName =
"ProcAddressTable";
58 private String getProcAddressTableExpr;
59 private String localProcAddressCallingConvention4All =
null;
61 private ConvNode procAddressNameConverter;
62 private final Set<String> skipProcAddressGen =
new HashSet<String>();
63 private final List<String> forceProcAddressGen =
new ArrayList<String>();
64 private final Set<String> forceProcAddressGenSet =
new HashSet<String>();
69 private final Map<String, String> localProcAddressCallingConventionMap =
new HashMap<String, String>();
72 protected void dispatch(
final String cmd,
final StringTokenizer tok,
final File file,
final String filename,
final int lineNo)
throws IOException {
73 if (cmd.equalsIgnoreCase(
"EmitProcAddressTable")) {
74 emitProcAddressTable =
readBoolean(
"EmitProcAddressTable", tok, filename, lineNo).booleanValue();
75 }
else if (cmd.equalsIgnoreCase(
"ProcAddressTablePackage")) {
76 tableClassPackage =
readString(
"ProcAddressTablePackage", tok, filename, lineNo);
77 }
else if (cmd.equalsIgnoreCase(
"ProcAddressTableClassName")) {
78 tableClassName =
readString(
"ProcAddressTableClassName", tok, filename, lineNo);
79 }
else if (cmd.equalsIgnoreCase(
"SkipProcAddressGen")) {
80 final String sym =
readString(
"SkipProcAddressGen", tok, filename, lineNo);
81 skipProcAddressGen.add(sym);
82 }
else if (cmd.equalsIgnoreCase(
"ForceProcAddressGen")) {
83 final String funcName =
readString(
"ForceProcAddressGen", tok, filename, lineNo);
84 if (funcName.equals(
"__ALL__")) {
85 forceProcAddressGen4All =
true;
89 }
else if (cmd.equalsIgnoreCase(
"GetProcAddressTableExpr")) {
91 }
else if (cmd.equalsIgnoreCase(
"ProcAddressNameExpr")) {
93 }
else if (cmd.equalsIgnoreCase(
"LocalProcAddressCallingConvention")) {
96 super.dispatch(cmd, tok, file, filename, lineNo);
102 final String restOfLine = tok.nextToken(
"\n\r\f");
103 return restOfLine.trim();
104 }
catch (
final NoSuchElementException e) {
105 throw new RuntimeException(
"Error parsing \"GetProcAddressTableExpr\" command at line " + lineNo
106 +
" in file \"" + filename +
"\"", e);
113 final List<String> tokens =
new ArrayList<String>();
114 final StringTokenizer tok1 =
new StringTokenizer(expr);
115 while (tok1.hasMoreTokens()) {
116 final String sstr = tok1.nextToken();
117 final StringTokenizer tok2 =
new StringTokenizer(sstr,
"$()",
true);
118 while (tok2.hasMoreTokens()) {
119 tokens.add(tok2.nextToken());
124 procAddressNameConverter = makeConverter(tokens.iterator());
125 if (procAddressNameConverter ==
null) {
126 throw new NoSuchElementException(
"Error creating converter from string");
132 String restOfLine = tok.nextToken(
"\n\r\f");
133 restOfLine = restOfLine.trim();
135 }
catch (
final NoSuchElementException e) {
136 throw new RuntimeException(
"Error parsing \"ProcAddressNameExpr\" command at line " + lineNo
137 +
" in file \"" + filename +
"\"", e);
143 final String functionName = tok.nextToken();
144 final String callingConvention = tok.nextToken();
145 if (functionName.equals(
"__ALL__")) {
146 localProcAddressCallingConvention4All = callingConvention;
148 localProcAddressCallingConventionMap.put(functionName, callingConvention);
150 }
catch (
final NoSuchElementException e) {
151 throw new RuntimeException(
"Error parsing \"LocalProcAddressCallingConvention\" command at line " + lineNo
152 +
" in file \"" + filename +
"\"", e);
156 private static ConvNode makeConverter(
final Iterator<String> iter) {
157 final List<ConvNode> result =
new ArrayList<ConvNode>();
159 while (iter.hasNext()) {
160 final String str = iter.next();
161 if (str.equals(
"$")) {
162 final String command = iter.next();
163 final String openParen = iter.next();
164 if (!openParen.equals(
"(")) {
165 throw new NoSuchElementException(
"Expected \"(\"");
167 boolean uppercase =
false;
168 if (command.equalsIgnoreCase(
"UPPERCASE")) {
170 }
else if (!command.equalsIgnoreCase(
"LOWERCASE")) {
171 throw new NoSuchElementException(
"Unknown ProcAddressNameExpr command \"" + command +
"\"");
173 result.add(
new CaseNode(uppercase, makeConverter(iter)));
174 }
else if (str.equals(
")")) {
176 }
else if (str.indexOf(
'{') >= 0) {
177 result.add(
new FormatNode(str));
179 result.add(
new ConstStringNode(str));
182 if (result.isEmpty()) {
184 }
else if (result.size() == 1) {
185 return result.get(0);
187 return new ConcatNode(result);
193 static abstract class ConvNode {
194 abstract String convert(String funcName);
197 static class FormatNode
extends ConvNode {
199 private final MessageFormat msgFmt;
201 FormatNode(
final String fmt) {
202 msgFmt =
new MessageFormat(fmt);
206 String convert(
final String funcName) {
207 final StringBuffer buf =
new StringBuffer();
208 msgFmt.format(
new Object[]{funcName}, buf,
null);
209 return buf.toString();
213 static class ConstStringNode
extends ConvNode {
215 private final String str;
217 ConstStringNode(
final String str) {
222 String convert(
final String funcName) {
227 static class ConcatNode
extends ConvNode {
229 private final List<ConvNode> children;
231 ConcatNode(
final List<ConvNode> children) {
232 this.children = children;
236 String convert(
final String funcName) {
237 final StringBuilder res =
new StringBuilder();
238 for (
final ConvNode node : children) {
239 res.append(node.convert(funcName));
241 return res.toString();
245 static class CaseNode
extends ConvNode {
247 private final boolean upperCase;
248 private final ConvNode child;
250 CaseNode(
final boolean upperCase,
final ConvNode child) {
251 this.upperCase = upperCase;
256 public String convert(
final String funcName) {
258 return child.convert(funcName).toUpperCase();
260 return child.convert(funcName).toLowerCase();
266 return emitProcAddressTable;
270 return tableClassPackage;
274 return tableClassName;
278 if ( skipProcAddressGen.contains( symbol.
getName() ) ||
289 return forceProcAddressGen4All;
293 return forceProcAddressGen;
297 if (getProcAddressTableExpr ==
null) {
298 throw new RuntimeException(
"GetProcAddressTableExpr was not defined in .cfg file");
300 return getProcAddressTableExpr;
303 getProcAddressTableExpr = s;
307 if (procAddressNameConverter ==
null) {
308 throw new RuntimeException(
"ProcAddressNameExpr was not defined in .cfg file");
310 return procAddressNameConverter.convert(funcName);
314 if( forceProcAddressGen4All ) {
315 if(!forceProcAddressGen4AllOnce) {
316 forceProcAddressGen4AllOnce =
true;
322 if ( forceProcAddressGenSet.contains( symbol.
getName() ) ||
331 private static boolean forceProcAddressGen4AllOnce =
false;
334 forceProcAddressGen.add(funcName);
335 forceProcAddressGenSet.add(funcName);
339 localProcAddressCallingConventionMap.put(funcName, callingConvention);
346 final String res = localProcAddressCallingConventionMap.get(symbol.
getName());
354 return localProcAddressCallingConvention4All !=
null;
358 return localProcAddressCallingConvention4All;
Parses and provides access to the contents of .cfg files for the JavaEmitter.
static< K, V > V oneInMap(final Map< K, V > map, final Set< K > symbols)
static< K > boolean oneInSet(final Set< K > set1, final Set< K > set2)
String readString(final String cmd, final StringTokenizer tok, final String filename, final int lineNo)
Boolean readBoolean(final String cmd, final StringTokenizer tok, final String filename, final int lineNo)
Set< String > getAliasedNames()
Return all aliases for this symbol, i.e.
String getName()
Return the current-name, which is the last renamed-name if issued, or the original-name.
Describes a function symbol, which includes the name and type.
ASTLocusTag getASTLocusTag()
Returns this instance's ASTLocusTag, if available, otherwise returns null.
boolean isLocalProcAddressCallingConvention4All()
void setProcAddressNameExpr(final String expr)
String getProcAddressTableExpr()
void addLocalProcAddressCallingConvention(final String funcName, final String callingConvention)
String readGetProcAddressTableExpr(final StringTokenizer tok, final String filename, final int lineNo)
void readLocalProcAddressCallingConvention(final StringTokenizer tok, final String filename, final int lineNo)
boolean forceProcAddressGen(final FunctionSymbol symbol)
boolean emitProcAddressTable()
boolean skipProcAddressGen(final FunctionSymbol symbol)
String getLocalProcAddressCallingConvention(final FunctionSymbol symbol)
List< String > getForceProcAddressGen()
void readProcAddressNameExpr(final StringTokenizer tok, final String filename, final int lineNo)
String convertToFunctionPointerName(final String funcName)
String tableClassPackage()
void dispatch(final String cmd, final StringTokenizer tok, final File file, final String filename, final int lineNo)
void setProcAddressTableExpr(final String s)
boolean isForceProcAddressGen4All()
void addForceProcAddressGen(final String funcName)
String getLocalProcAddressCallingConvention4All()
void log(final Level level, final String msg)
See Logger#log(Level, String).