|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.codehaus.groovy.transform.sc; |
| 20 | + |
| 21 | +import org.codehaus.groovy.ast.ASTNode; |
| 22 | +import org.codehaus.groovy.ast.AnnotatedNode; |
| 23 | +import org.codehaus.groovy.ast.AnnotationNode; |
| 24 | +import org.codehaus.groovy.ast.ClassNode; |
| 25 | +import org.codehaus.groovy.ast.MethodNode; |
| 26 | +import org.codehaus.groovy.ast.expr.Expression; |
| 27 | +import org.codehaus.groovy.classgen.asm.WriterControllerFactory; |
| 28 | +import org.codehaus.groovy.classgen.asm.sc.StaticTypesWriterControllerFactoryImpl; |
| 29 | +import org.codehaus.groovy.control.CompilePhase; |
| 30 | +import org.codehaus.groovy.control.SourceUnit; |
| 31 | +import org.codehaus.groovy.syntax.SyntaxException; |
| 32 | +import org.codehaus.groovy.transform.GroovyASTTransformation; |
| 33 | +import org.codehaus.groovy.transform.StaticTypesTransformation; |
| 34 | +import org.codehaus.groovy.transform.sc.transformers.StaticCompilationTransformer; |
| 35 | +import org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor; |
| 36 | + |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import static org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys.STATIC_COMPILE_NODE; |
| 41 | + |
| 42 | +/** |
| 43 | + * Handles the implementation of the {@link groovy.transform.CompileStatic} transformation. |
| 44 | + */ |
| 45 | +@GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION) |
| 46 | +public class StaticCompileTransformation extends StaticTypesTransformation { |
| 47 | + |
| 48 | + private final StaticTypesWriterControllerFactoryImpl factory = new StaticTypesWriterControllerFactoryImpl(); |
| 49 | + |
| 50 | + @Override |
| 51 | + public void visit(final ASTNode[] nodes, final SourceUnit source) { |
| 52 | + AnnotationNode annotationInformation = (AnnotationNode) nodes[0]; |
| 53 | + AnnotatedNode node = (AnnotatedNode) nodes[1]; |
| 54 | + // GRECLIPSE add -- GROOVY-9707 |
| 55 | + if (node.getNodeMetaData(StaticTypeCheckingVisitor.class)!=null){ |
| 56 | + return; |
| 57 | + } |
| 58 | + // GRECLIPSE end |
| 59 | + StaticTypeCheckingVisitor visitor = null; |
| 60 | + Map<String,Expression> members = annotationInformation.getMembers(); |
| 61 | + Expression extensions = members.get("extensions"); |
| 62 | + if (node instanceof ClassNode) { |
| 63 | + ClassNode classNode = (ClassNode) node; |
| 64 | + visitor = newVisitor(source, classNode); |
| 65 | + visitor.setCompilationUnit(compilationUnit); |
| 66 | + addTypeCheckingExtensions(visitor, extensions); |
| 67 | + classNode.putNodeMetaData(WriterControllerFactory.class, factory); |
| 68 | + node.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node)); |
| 69 | + visitor.initialize(); |
| 70 | + visitor.visitClass(classNode); |
| 71 | + } else if (node instanceof MethodNode) { |
| 72 | + MethodNode methodNode = (MethodNode) node; |
| 73 | + ClassNode declaringClass = methodNode.getDeclaringClass(); |
| 74 | + visitor = newVisitor(source, declaringClass); |
| 75 | + visitor.setCompilationUnit(compilationUnit); |
| 76 | + addTypeCheckingExtensions(visitor, extensions); |
| 77 | + methodNode.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node)); |
| 78 | + if (declaringClass.getNodeMetaData(WriterControllerFactory.class) == null) { |
| 79 | + declaringClass.putNodeMetaData(WriterControllerFactory.class, factory); |
| 80 | + } |
| 81 | + visitor.setMethodsToBeVisited(Collections.singleton(methodNode)); |
| 82 | + visitor.initialize(); |
| 83 | + visitor.visitMethod(methodNode); |
| 84 | + } else { |
| 85 | + source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type", |
| 86 | + node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber())); |
| 87 | + } |
| 88 | + if (visitor != null) { |
| 89 | + visitor.performSecondPass(); |
| 90 | + } |
| 91 | + StaticCompilationTransformer transformer = new StaticCompilationTransformer(source, visitor); |
| 92 | + if (node instanceof ClassNode) { |
| 93 | + transformer.visitClass((ClassNode) node); |
| 94 | + } else if (node instanceof MethodNode) { |
| 95 | + transformer.visitMethod((MethodNode) node); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected StaticTypeCheckingVisitor newVisitor(final SourceUnit unit, final ClassNode node) { |
| 101 | + return new StaticCompilationVisitor(unit, node); |
| 102 | + } |
| 103 | +} |
0 commit comments