Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Feature call of constant inline expressions #43

Closed
gallandarakhneorg opened this issue Aug 21, 2016 · 6 comments · Fixed by #62
Closed

Feature call of constant inline expressions #43

gallandarakhneorg opened this issue Aug 21, 2016 · 6 comments · Fixed by #62
Labels

Comments

@gallandarakhneorg
Copy link
Contributor

gallandarakhneorg commented Aug 21, 2016

Let a not-static function with an inline constant expression:

class C1 {
    @Inline(value = "1", constantExpression = true)
    def int getValue() {
        return 1
    }
}
class C2 {
   def test {
       var c = new C1
       System.out.println(c.value)
   }
}

The generated Java code for the test function is:

public void test() {
    C1 c = new C1();
    System.out.println(c.1);
}

The Java code has a syntax error because 1 is not a valid member of C1.

@gallandarakhneorg
Copy link
Contributor Author

Solution may be to change the code of FeatureCallCompiler:

    private static boolean isConstantExpression(JvmAnnotationReference reference) {
        for (final JvmAnnotationValue annotationValue: reference.getValues()) {
            if ("constantExpression".equals(annotationValue.getValueName())) {
                return ((JvmBooleanAnnotationValue) annotationValue).getValues().get(0).booleanValue();
            }
        }
        return false;
    }

    @Override
    protected void featureCalltoJavaExpression(final XAbstractFeatureCall call, ITreeAppendable output,
            boolean isExpressionContext) {
        if (call instanceof XAssignment) {
            assignmentToJavaExpression((XAssignment) call, output, isExpressionContext);
        } else {
            if (needMultiAssignment(call)) {
                appendLeftOperand(call, output, isExpressionContext).append(" = "); //$NON-NLS-1$
            }

            ITreeAppendable child = output;
            final JvmAnnotationReference annotationRef = this.expressionHelper.findInlineAnnotation(call);
            if (annotationRef == null || !isConstantExpression(annotationRef)) {
                final boolean hasReceiver = appendReceiver(call, output, isExpressionContext);
                if (hasReceiver) {
                    output.append("."); //$NON-NLS-1$
                    child = appendTypeArguments(call, output);
                }
            }
            appendFeatureCall(call, child);
        }
    }

The idea is to skip the receiver when the inline expression is constant.

May I create a pull request?

@spoenemann
Copy link
Member

May I create a pull request?

Yes.

gallandarakhneorg added a commit to gallandarakhneorg/xtext-extras that referenced this issue Oct 6, 2016
This PR fixes the invalid generated code when constant inline expressions are invoked.

close eclipse#43

Signed-off-by: Stéphane Galland <galland@arakhne.org>
gallandarakhneorg added a commit to gallandarakhneorg/xtext-extras that referenced this issue Oct 6, 2016
This PR fixes the invalid generated code when constant inline
expressions are invoked.

close eclipse#43

Signed-off-by: Stéphane Galland <galland@arakhne.org>
gallandarakhneorg added a commit to gallandarakhneorg/xtext-extras that referenced this issue Oct 7, 2016
This PR fixes the invalid generated code when constant inline
expressions are invoked.

close eclipse#43

Signed-off-by: Stéphane Galland <galland@arakhne.org>
@svenefftinge
Copy link
Member

svenefftinge commented Oct 20, 2016

I don't really get what your intend is here, but @Inline is not meant to be used outside of the xbase lib and is made to work on static methods only.
Could you explain your use case?

@gallandarakhneorg
Copy link
Contributor Author

Dear @svenefftinge

I am using @Inline in two contexts:

  • I am using @Inline in my Java libraries (http://www.arakhne.org/afc) in order to enable better Java code generation when using the library functions from my DSL language.
  • And my DSL compiler is detecting "simple" functions (with only a return statement) for generating the @Inline annotation and attaching it to the generated Java function.

It should be nice to enable the use of @Inline annotation outside xbase lib (indeed it is almost ready to be used), because it is a nice feature for helping the xbase-based generators to generate better Java code.

Nevertheless, even if I'm using @Inline outside its planned scope, I have discovered inconsistency in the meaning of the @Inline parameters from my point of view, as I have explained in this issue.

I have added patches for solving this in my DSL compiler, and it's working fine.

@svenefftinge
Copy link
Member

svenefftinge commented Oct 24, 2016

Thank you. I wanted to understand what the purpose of your use of 'constantExpression' is, since a constant expression by definition can not come from an instance, it doesn't make sense to me to allow it on non-static members.

Also see https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28

@gallandarakhneorg
Copy link
Contributor Author

gallandarakhneorg commented Oct 24, 2016

For an example of @Inline usefull usage for non-static functions:

class X {
   @Inline(value="f($1,2)")
    def int f(int a) {
        return f(a, 2);
     }
    def int f(int a, int b) {
    }
}

gallandarakhneorg added a commit to gallandarakhneorg/xtext-extras that referenced this issue Oct 8, 2017
This PR fixes the invalid generated code when constant inline
expressions are invoked.

close eclipse#43

Signed-off-by: Stéphane Galland <galland@arakhne.org>
gallandarakhneorg added a commit to gallandarakhneorg/xtext-extras that referenced this issue Oct 9, 2017
This PR fixes the invalid generated code when constant inline
expressions are invoked.

close eclipse#43

Signed-off-by: Stéphane Galland <galland@arakhne.org>
@kthoms kthoms closed this as completed in #62 Oct 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants