Search code examples
c++xtextxtend

How to show an error in the Xtext IDE if a type is referenced before it is declared


We have a custom Xtext-based DSL and generate C++ code from a document written in this DSL via Xtend. Here is a simple example of the DSL:

component CMP
{
    type A
    {
       B member_1;
    }

    type B
    {
        string member_1;
    }
}

Eventually, a header file containing C++ structs (based on type elements) is generated for every component element. Now the following problem occurs: In my DSL A and B can be correctly resolved (even though B is declared later than A). But when we generate C++ code and do something like resource.contents.filter(TypeElement) to iterate through all type elements, they are delivered in the same order as declared in the document. This leads to compiler errors for the resulting C++ header, because B is declared later than A and cannot be resolved by the compiler without a forward declaration.

What I want is to show an error in the IDE in such a case (i.e. if a reference exists to a type which occurs later in the document). Is there some kind of standard validator for such scenarios? If not, what would be the most proper way to deal with this issue?

Thank you in advance!


Solution

  • There is no standard validator for forward references. You'd need to define one one your own based on the values of your cross reference targets. Inject the ILocationInFileProvider into your validator to obtain the region of the referenced instance and compare it with the region of the reference owner. That will do the trick to detect forward references.