From b68145fe23170089f797f697163d75eedb3bb319 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 24 Mar 2015 03:26:55 +0100 Subject: 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. --- src/main/java/com/jogamp/gluegen/jcpp/JCPP.java | 41 ++++++++++--------- src/main/java/com/jogamp/gluegen/jcpp/Macro.java | 46 ++++++++++++++++------ .../java/com/jogamp/gluegen/jcpp/Preprocessor.java | 46 ++++++++++++++++------ 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 getConstantDefinitions() { + public List getConstantDefinitions() throws GlueGenException { final List constants = new ArrayList(); - final Map macroMap = cpp.getMacros(); - final Collection macros = macroMap.values(); - for(final Macro macro : macros) { + final List macros; + try { + macros = cpp.getMacros(true); + } catch (final Throwable t) { + throw new GlueGenException(t); + } + final int count = macros.size(); + for(int i=0; i args; private boolean variadic; + private boolean hasPaste; private List tokens; public Macro(final Source source, final String name) { @@ -44,20 +45,36 @@ public class Macro { this.name = name; this.args = null; this.variadic = false; + this.hasPaste = false; this.tokens = new ArrayList(); } + public Macro(final Macro o) { + this(o, o.tokens, true); + } + public Macro(final Macro o, final List tokens) { + this(o, tokens, false); + } + private Macro(final Macro o, final List tokens, final boolean copyTokens) { + this.source = o.source; + this.name = o.name; + if(null != o.args) { + this.args = new ArrayList(o.args); + } else { + this.args = null; + } + this.variadic = o.variadic; + this.hasPaste = o.hasPaste; + if(null != tokens) { + this.tokens = copyTokens ? new ArrayList(tokens) : tokens; + } else { + this.tokens = new ArrayList(); + } + } 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. * @@ -78,7 +95,7 @@ public class Macro { /** * Sets the arguments to this macro. */ - public void setArgs(final List args) { + /* pp */ void setArgs(final List args) { this.args = args; } @@ -110,6 +127,13 @@ public class Macro { return variadic; } + /** + * Returns true if this macro contains a "paste" operator. + */ + public boolean hasPaste() { + return hasPaste; + } + /** * Adds a token to the expansion of this macro. */ @@ -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 getTokens() { return tokens; } - /* pp */ void setTokens(final List 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) { @@ -467,6 +468,29 @@ public class Preprocessor implements Closeable { return macros; } + /** + * Returns a list of {@link Macro}s. + *

+ * Implementation returns a new list of copy-ctor {@link Macro}s. + *

+ * @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 getMacros(final boolean expand) throws IOException, LexerException { + final List res = new ArrayList(); + final Collection 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. * @@ -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 -- cgit v1.2.3