aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2015-03-24 03:26:55 +0100
committerSven Gothel <[email protected]>2015-03-24 03:26:55 +0100
commitb68145fe23170089f797f697163d75eedb3bb319 (patch)
tree622a8794ffd2ffbed872eb72c45bdbaf494b0dc7 /src/main/java/com
parenta2bf42eafb8d6bc270f832c6bd49793465a593d4 (diff)
Expose explicit macro expansiob and remove previously added implicit macro expansion.
- Reverts commit 6d805e3f526b30144649232246d5ffdc04a31ebf and explicitly expose expanded macros to PP getMacros(boolean expand) - PP getMacros(boolean expand) - Returns a deep copy of all macros - May expand them if requested and if they are constants, i.e. non-function like.
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/jogamp/gluegen/jcpp/JCPP.java41
-rw-r--r--src/main/java/com/jogamp/gluegen/jcpp/Macro.java46
-rw-r--r--src/main/java/com/jogamp/gluegen/jcpp/Preprocessor.java46
3 files changed, 89 insertions, 44 deletions
diff --git a/src/main/java/com/jogamp/gluegen/jcpp/JCPP.java b/src/main/java/com/jogamp/gluegen/jcpp/JCPP.java
index 5c1e4ab..91f93db 100644
--- a/src/main/java/com/jogamp/gluegen/jcpp/JCPP.java
+++ b/src/main/java/com/jogamp/gluegen/jcpp/JCPP.java
@@ -1,25 +1,17 @@
/**
* Copyright 2015 JogAmp Community. All rights reserved.
*
- * Redistribution and use in source and binary forms, with or without modification, are
- * permitted provided that the following conditions are met:
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
*
- * 1. Redistributions of source code must retain the above copyright notice, this list of
- * conditions and the following disclaimer.
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * 2. Redistributions in binary form must reproduce the above copyright notice, this list
- * of conditions and the following disclaimer in the documentation and/or other materials
- * provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
@@ -28,6 +20,7 @@
package com.jogamp.gluegen.jcpp;
import java.io.File;
+import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
@@ -96,11 +89,17 @@ public class JCPP implements GenericCPP {
}
@Override
- public List<ConstantDefinition> getConstantDefinitions() {
+ public List<ConstantDefinition> getConstantDefinitions() throws GlueGenException {
final List<ConstantDefinition> constants = new ArrayList<ConstantDefinition>();
- final Map<String, Macro> macroMap = cpp.getMacros();
- final Collection<Macro> macros = macroMap.values();
- for(final Macro macro : macros) {
+ final List<Macro> macros;
+ try {
+ macros = cpp.getMacros(true);
+ } catch (final Throwable t) {
+ throw new GlueGenException(t);
+ }
+ final int count = macros.size();
+ for(int i=0; i<count; i++) {
+ final Macro macro = macros.get(i);
final String name = macro.getName();
if( !GlueGen.__GLUEGEN__.equals(name) ) {
if( !macro.isFunctionLike() ) {
diff --git a/src/main/java/com/jogamp/gluegen/jcpp/Macro.java b/src/main/java/com/jogamp/gluegen/jcpp/Macro.java
index e41273e..3029252 100644
--- a/src/main/java/com/jogamp/gluegen/jcpp/Macro.java
+++ b/src/main/java/com/jogamp/gluegen/jcpp/Macro.java
@@ -29,7 +29,7 @@ import java.util.List;
*/
public class Macro {
- private Source source;
+ private final Source source;
private final String name;
/* It's an explicit decision to keep these around here. We don't
* need to; the argument token type is M_ARG and the value
@@ -37,6 +37,7 @@ public class Macro {
* stringification of the macro, for debugging. */
private List<String> args;
private boolean variadic;
+ private boolean hasPaste;
private List<Token> tokens;
public Macro(final Source source, final String name) {
@@ -44,21 +45,37 @@ public class Macro {
this.name = name;
this.args = null;
this.variadic = false;
+ this.hasPaste = false;
this.tokens = new ArrayList<Token>();
}
+ public Macro(final Macro o) {
+ this(o, o.tokens, true);
+ }
+ public Macro(final Macro o, final List<Token> tokens) {
+ this(o, tokens, false);
+ }
+ private Macro(final Macro o, final List<Token> tokens, final boolean copyTokens) {
+ this.source = o.source;
+ this.name = o.name;
+ if(null != o.args) {
+ this.args = new ArrayList<String>(o.args);
+ } else {
+ this.args = null;
+ }
+ this.variadic = o.variadic;
+ this.hasPaste = o.hasPaste;
+ if(null != tokens) {
+ this.tokens = copyTokens ? new ArrayList<Token>(tokens) : tokens;
+ } else {
+ this.tokens = new ArrayList<Token>();
+ }
+ }
public Macro(final String name) {
this(null, name);
}
/**
- * Sets the Source from which this macro was parsed.
- */
- public void setSource(final Source s) {
- this.source = s;
- }
-
- /**
* Returns the Source from which this macro was parsed.
*
* This method may return null if the macro was not parsed
@@ -78,7 +95,7 @@ public class Macro {
/**
* Sets the arguments to this macro.
*/
- public void setArgs(final List<String> args) {
+ /* pp */ void setArgs(final List<String> args) {
this.args = args;
}
@@ -111,6 +128,13 @@ public class Macro {
}
/**
+ * Returns true if this macro contains a "paste" operator.
+ */
+ public boolean hasPaste() {
+ return hasPaste;
+ }
+
+ /**
* Adds a token to the expansion of this macro.
*/
public void addToken(final Token tok) {
@@ -133,14 +157,12 @@ public class Macro {
* M_PASTE, tok0, M_PASTE, tok1, tok2
*/
this.tokens.add(tokens.size() - 1, tok);
+ this.hasPaste = true;
}
/* pp */ List<Token> getTokens() {
return tokens;
}
- /* pp */ void setTokens(final List<Token> tokens) {
- this.tokens = tokens;
- }
/* Paste tokens are inserted before the first of the two pasted
* tokens, so it's a kind of bytecode notation. This method
diff --git a/src/main/java/com/jogamp/gluegen/jcpp/Preprocessor.java b/src/main/java/com/jogamp/gluegen/jcpp/Preprocessor.java
index 31eca4e..e3825a5 100644
--- a/src/main/java/com/jogamp/gluegen/jcpp/Preprocessor.java
+++ b/src/main/java/com/jogamp/gluegen/jcpp/Preprocessor.java
@@ -360,11 +360,8 @@ public class Preprocessor implements Closeable {
// System.out.println("Macro " + m);
final String name = m.getName();
/* Already handled as a source error in macro(). */
- if ("defined".equals(name))
+ if ("defined".equals(name)) {
throw new LexerException("Cannot redefine name 'defined'");
-
- if ( isActive() && null != source && !source.isExpanding(m) ) {
- m.setTokens( expand( m.getTokens() ) );
}
macros.put(m.getName(), m);
}
@@ -380,11 +377,15 @@ public class Preprocessor implements Closeable {
try {
final Macro m = new Macro(name);
final StringLexerSource s = new StringLexerSource(value);
- for (;;) {
- final Token tok = s.token();
- if (tok.getType() == EOF)
- break;
- m.addToken(tok);
+ try {
+ for (;;) {
+ final Token tok = s.token();
+ if (tok.getType() == EOF)
+ break;
+ m.addToken(tok);
+ }
+ } finally {
+ s.close();
}
addMacro(m);
} catch (final IOException e) {
@@ -468,6 +469,29 @@ public class Preprocessor implements Closeable {
}
/**
+ * Returns a list of {@link Macro}s.
+ * <p>
+ * Implementation returns a new list of copy-ctor {@link Macro}s.
+ * </p>
+ * @param expand if {@code true} and if macro is not {@link Macro#isFunctionLike() function-like},
+ * i.e. a constant, the returned macro will be expanded.
+ * @throws IOException
+ * @throws LexerException
+ */
+ public List<Macro> getMacros(final boolean expand) throws IOException, LexerException {
+ final List<Macro> res = new ArrayList<Macro>();
+ final Collection<Macro> macroList = macros.values();
+ for(final Macro m : macroList) {
+ if( expand && !m.isFunctionLike() ) {
+ res.add( new Macro( m, expand( m.getTokens() ) ) );
+ } else {
+ res.add( new Macro( m ) );
+ }
+ }
+ return res;
+ }
+
+ /**
* Returns the named macro.
*
* While you can modify the returned object, unexpected things
@@ -1091,12 +1115,12 @@ public class Preprocessor implements Closeable {
tok = source_token();
}
- if (getFeature(Feature.DEBUG))
+ if (getFeature(Feature.DEBUG)) {
LOG.debug("Defined macro " + m);
+ }
addMacro(m);
return tok; /* NL or EOF. */
-
}
@Nonnull