001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.codegen;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.asm4.MethodVisitor;
022 import org.jetbrains.asm4.Type;
023 import org.jetbrains.jet.codegen.context.ClassContext;
024 import org.jetbrains.jet.codegen.state.GenerationState;
025 import org.jetbrains.jet.lang.descriptors.*;
026 import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
027 import org.jetbrains.jet.lang.descriptors.impl.SimpleFunctionDescriptorImpl;
028 import org.jetbrains.jet.lang.psi.*;
029 import org.jetbrains.jet.lang.resolve.BindingContext;
030 import org.jetbrains.jet.lang.resolve.name.Name;
031
032 import java.util.Collections;
033 import java.util.List;
034
035 import static org.jetbrains.asm4.Opcodes.ACC_STATIC;
036 import static org.jetbrains.asm4.Opcodes.RETURN;
037 import static org.jetbrains.jet.codegen.binding.CodegenBinding.enumEntryNeedSubclass;
038 import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isTopLevelOrInnerClass;
039
040 public abstract class ClassBodyCodegen extends MemberCodegen {
041 protected final JetClassOrObject myClass;
042 protected final OwnerKind kind;
043 protected final ClassDescriptor descriptor;
044 protected final ClassBuilder v;
045 protected final ClassContext context;
046
047 private MethodVisitor clInitMethod;
048
049 private ExpressionCodegen clInitCodegen;
050
051 protected ClassBodyCodegen(
052 @NotNull JetClassOrObject aClass,
053 @NotNull ClassContext context,
054 @NotNull ClassBuilder v,
055 @NotNull GenerationState state,
056 @Nullable MemberCodegen parentCodegen
057 ) {
058 super(state, parentCodegen);
059 descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass);
060 myClass = aClass;
061 this.context = context;
062 this.kind = context.getContextKind();
063 this.v = v;
064 }
065
066 public void generate() {
067 generateDeclaration();
068
069 generateClassBody();
070
071 generateSyntheticParts();
072
073 generateStaticInitializer();
074
075 if (state.getClassBuilderMode() == ClassBuilderMode.FULL && isTopLevelOrInnerClass(descriptor)) {
076 generateKotlinAnnotation();
077 }
078 }
079
080 protected abstract void generateDeclaration();
081
082 protected abstract void generateKotlinAnnotation();
083
084 protected void generateSyntheticParts() {
085 }
086
087 private void generateClassBody() {
088 FunctionCodegen functionCodegen = new FunctionCodegen(context, v, state, this);
089 PropertyCodegen propertyCodegen = new PropertyCodegen(context, v, functionCodegen, this);
090
091 if (kind != OwnerKind.TRAIT_IMPL) {
092 //generate nested classes first and only then generate class body. It necessary to access to nested CodegenContexts
093 for (JetDeclaration declaration : myClass.getDeclarations()) {
094 if (shouldProcessFirst(declaration)) {
095 generateDeclaration(propertyCodegen, declaration);
096 }
097 }
098 }
099
100 for (JetDeclaration declaration : myClass.getDeclarations()) {
101 if (!shouldProcessFirst(declaration)) {
102 generateDeclaration(propertyCodegen, declaration);
103 }
104 }
105
106 generatePrimaryConstructorProperties(propertyCodegen, myClass);
107 }
108
109 private static boolean shouldProcessFirst(JetDeclaration declaration) {
110 return !(declaration instanceof JetProperty || declaration instanceof JetNamedFunction);
111 }
112
113
114 protected void generateDeclaration(PropertyCodegen propertyCodegen, JetDeclaration declaration) {
115 if (declaration instanceof JetProperty || declaration instanceof JetNamedFunction) {
116 genFunctionOrProperty(context, (JetTypeParameterListOwner) declaration, v);
117 }
118 else if (declaration instanceof JetClassOrObject) {
119 if (declaration instanceof JetEnumEntry && !enumEntryNeedSubclass(
120 state.getBindingContext(), (JetEnumEntry) declaration)) {
121 return;
122 }
123
124 genClassOrObject(context, (JetClassOrObject) declaration);
125 }
126 else if (declaration instanceof JetClassObject) {
127 genClassOrObject(context, ((JetClassObject) declaration).getObjectDeclaration());
128 }
129 }
130
131 private void generatePrimaryConstructorProperties(PropertyCodegen propertyCodegen, JetClassOrObject origin) {
132 boolean isAnnotation = origin instanceof JetClass && ((JetClass) origin).isAnnotation();
133 for (JetParameter p : getPrimaryConstructorParameters()) {
134 if (p.getValOrVarNode() != null) {
135 PropertyDescriptor propertyDescriptor = state.getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, p);
136 if (propertyDescriptor != null) {
137 if (!isAnnotation) {
138 propertyCodegen.generatePrimaryConstructorProperty(p, propertyDescriptor);
139 }
140 else {
141 propertyCodegen.generateConstructorPropertyAsMethodForAnnotationClass(p, propertyDescriptor);
142 }
143 }
144 }
145 }
146 }
147
148 protected @NotNull List<JetParameter> getPrimaryConstructorParameters() {
149 if (myClass instanceof JetClass) {
150 return ((JetClass) myClass).getPrimaryConstructorParameters();
151 }
152 return Collections.emptyList();
153 }
154
155 private void generateStaticInitializer() {
156 if (clInitMethod != null) {
157 createOrGetClInitMethod();
158
159 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
160 ExpressionCodegen codegen = createOrGetClInitCodegen();
161
162 createOrGetClInitMethod().visitInsn(RETURN);
163 FunctionCodegen.endVisit(codegen.v, "static initializer", myClass);
164 }
165 }
166 }
167
168 @NotNull
169 protected MethodVisitor createOrGetClInitMethod() {
170 if (clInitMethod == null) {
171 clInitMethod = v.newMethod(null, ACC_STATIC, "<clinit>", "()V", null, null);
172 }
173 return clInitMethod;
174 }
175
176 @NotNull
177 protected ExpressionCodegen createOrGetClInitCodegen() {
178 assert state.getClassBuilderMode() == ClassBuilderMode.FULL;
179 if (clInitCodegen == null) {
180 MethodVisitor method = createOrGetClInitMethod();
181 method.visitCode();
182 SimpleFunctionDescriptorImpl clInit =
183 new SimpleFunctionDescriptorImpl(descriptor, Collections.<AnnotationDescriptor>emptyList(),
184 Name.special("<clinit>"),
185 CallableMemberDescriptor.Kind.SYNTHESIZED);
186 clInit.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
187 Collections.<ValueParameterDescriptor>emptyList(), null, null, Visibilities.PRIVATE);
188
189 clInitCodegen = new ExpressionCodegen(method, new FrameMap(), Type.VOID_TYPE, context.intoFunction(clInit), state, this);
190 }
191 return clInitCodegen;
192 }
193 }