001    /* Copyright (c) 2005-2008, Torbjorn Ekman
002     *                    2015, Jesper Öqvist <jesper.oqvist@cs.lth.se>
003     * All rights reserved.
004     *
005     * Redistribution and use in source and binary forms, with or without
006     * modification, are permitted provided that the following conditions are met:
007     *
008     * 1. Redistributions of source code must retain the above copyright notice,
009     * this list of conditions and the following disclaimer.
010     *
011     * 2. Redistributions in binary form must reproduce the above copyright notice,
012     * this list of conditions and the following disclaimer in the documentation
013     * and/or other materials provided with the distribution.
014     *
015     * 3. Neither the name of the copyright holder nor the names of its
016     * contributors may be used to endorse or promote products derived from this
017     * software without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
023     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
025     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
026     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
027     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
029     * POSSIBILITY OF SUCH DAMAGE.
030     */
031    
032    aspect SyntacticClassification {
033      // 6.5.1 Syntactic Classification of Name According to Context
034    
035      // The syntactic classification context
036      inh lazy NameType Expr.nameType();
037    
038      /**
039       * The classified name type of a parsed name or expression.
040       *
041       * Method Name is not in this enum because it never occurs in the ASTs
042       * produced by our parser: the parser builds the correct node since it has a
043       * different structure.
044       */
045      enum NameType {
046        NOT_CLASSIFIED,
047        PACKAGE_NAME,
048        TYPE_NAME,
049        PACKAGE_OR_TYPE_NAME,
050        AMBIGUOUS_NAME,
051        EXPRESSION_NAME
052      }
053    
054      /**
055       * Defines the expected kind of name for the left hand side in a qualified
056       * expression.
057       */
058      syn NameType Access.predNameType() {
059        switch (nameType()) {
060          case PACKAGE_NAME:
061            return NameType.PACKAGE_NAME;
062          case TYPE_NAME:
063          case PACKAGE_OR_TYPE_NAME:
064            return NameType.PACKAGE_OR_TYPE_NAME;
065          case AMBIGUOUS_NAME:
066          case EXPRESSION_NAME:
067            return NameType.AMBIGUOUS_NAME;
068          case NOT_CLASSIFIED:
069          default:
070            return NameType.NOT_CLASSIFIED;
071        }
072      }
073    
074      // NB: nameType() should not trigger rewrites: rewriting here does not
075      // provide any extra information needed to compute the correct nameType() and
076      // it can mess up the rewrite order and make the whole name resolution break.
077      // It is important that the left-hand side of a qualified expression is
078      // rewritten first, but triggering rewrites in nameType() will make the
079      // right-hand side be rewritten simultaneously, and complete before the
080      // left-hand side. It is important that the left-hand side is rewritten first
081      // so that lookupType() and qualifiedLookupType() work as intended.
082      // - Jesper 2015
083    
084      // propagate predNameType() to the left hand side of a qualified expression
085      eq AbstractDot.getLeft().nameType() = getRightNoTransform().predNameType();
086      eq AbstractDot.predNameType() = getLeftNoTransform() instanceof Access ?
087        ((Access) getLeftNoTransform()).predNameType() : NameType.NOT_CLASSIFIED;
088    
089      // Equations for the syntactic classification context
090    
091      eq Program.getChild().nameType() = NameType.NOT_CLASSIFIED;
092    
093      // Package name
094    
095      eq PackageAccess.predNameType() = NameType.PACKAGE_NAME;
096      eq CompilationUnit.getImportDecl().nameType() = NameType.PACKAGE_NAME;
097    
098      // Type Name
099      eq SingleTypeImportDecl.getAccess().nameType() = NameType.TYPE_NAME;
100      eq ClassDecl.getSuperClass().nameType() = NameType.TYPE_NAME;
101      eq ClassDecl.getImplements().nameType() = NameType.TYPE_NAME;
102      eq InterfaceDecl.getSuperInterface().nameType() = NameType.TYPE_NAME;
103    
104      eq FieldDecl.getTypeAccess().nameType() = NameType.TYPE_NAME;
105      eq FieldDeclaration.getTypeAccess().nameType() = NameType.TYPE_NAME;
106      eq MethodDecl.getTypeAccess().nameType() = NameType.TYPE_NAME;
107      eq MethodDecl.getParameter().nameType() = NameType.TYPE_NAME;
108      eq MethodDecl.getException().nameType() = NameType.TYPE_NAME;
109      eq ConstructorDecl.getParameter().nameType() = NameType.TYPE_NAME;
110      eq ConstructorDecl.getException().nameType() = NameType.TYPE_NAME;
111      eq VarDeclStmt.getTypeAccess().nameType() = NameType.TYPE_NAME;
112      eq VariableDeclaration.getTypeAccess().nameType() = NameType.TYPE_NAME;
113      eq BasicCatch.getParameter().nameType() = NameType.TYPE_NAME;
114      eq ArrayCreationExpr.getTypeAccess().nameType() = NameType.TYPE_NAME;
115      eq CastExpr.getTypeAccess().nameType() = NameType.TYPE_NAME;
116      eq InstanceOfExpr.getTypeAccess().nameType() = NameType.TYPE_NAME;
117    
118      eq ClassAccess.predNameType() = NameType.TYPE_NAME;
119      eq ThisAccess.predNameType() = NameType.TYPE_NAME;
120      eq SuperAccess.predNameType() = NameType.TYPE_NAME;
121    
122      // Expression Name
123      eq SuperConstructorAccess.predNameType() = NameType.EXPRESSION_NAME;
124      eq ClassInstanceExpr.predNameType() = NameType.EXPRESSION_NAME;
125      eq PostfixExpr.getOperand().nameType() = NameType.EXPRESSION_NAME;
126      eq AssignExpr.getDest().nameType() = NameType.EXPRESSION_NAME;
127      eq ArrayAccess.predNameType() = NameType.EXPRESSION_NAME;
128    
129      // Package Or Type Name
130      eq TypeAccess.predNameType() = NameType.PACKAGE_OR_TYPE_NAME;
131      eq TypeImportOnDemandDecl.getAccess().nameType() = NameType.PACKAGE_OR_TYPE_NAME;
132    
133      // Ambiguous Name
134      eq VarAccess.predNameType() = NameType.AMBIGUOUS_NAME;
135      eq MethodAccess.predNameType() = NameType.AMBIGUOUS_NAME;
136    
137      // Extras
138      eq Block.getStmt().nameType() = NameType.EXPRESSION_NAME;
139      eq ConstructorDecl.getParsedConstructorInvocation().nameType() = NameType.EXPRESSION_NAME;
140      eq ConstructorDecl.getImplicitConstructorInvocation().nameType() = NameType.EXPRESSION_NAME;
141      eq TypeDecl.getBodyDecl().nameType() = NameType.EXPRESSION_NAME;
142    
143      eq MethodAccess.getArg().nameType() = NameType.EXPRESSION_NAME;
144      eq ConstructorAccess.getArg().nameType() = NameType.EXPRESSION_NAME;
145      eq ArrayAccess.getExpr().nameType() = NameType.EXPRESSION_NAME;
146      eq ArrayTypeWithSizeAccess.getExpr().nameType() = NameType.EXPRESSION_NAME;
147    
148      eq ClassInstanceExpr.getAccess().nameType() = NameType.TYPE_NAME;
149      eq ClassInstanceExpr.getTypeDecl().nameType() = NameType.TYPE_NAME;
150      eq ClassInstanceExpr.getArg().nameType() = NameType.EXPRESSION_NAME;
151    
152      eq ConstructorAccess.predNameType() = NameType.AMBIGUOUS_NAME;
153      eq ArrayTypeAccess.predNameType() = NameType.AMBIGUOUS_NAME;
154    }
155