001 /* 002 * Copyright 2010-2014 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.Annotations; 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(state.getBindingContext(), (JetEnumEntry) declaration)) { 120 return; 121 } 122 123 genClassOrObject(context, (JetClassOrObject) declaration); 124 } 125 else if (declaration instanceof JetClassObject) { 126 genClassOrObject(context, ((JetClassObject) declaration).getObjectDeclaration()); 127 } 128 } 129 130 private void generatePrimaryConstructorProperties(PropertyCodegen propertyCodegen, JetClassOrObject origin) { 131 boolean isAnnotation = origin instanceof JetClass && ((JetClass) origin).isAnnotation(); 132 for (JetParameter p : getPrimaryConstructorParameters()) { 133 if (p.getValOrVarNode() != null) { 134 PropertyDescriptor propertyDescriptor = state.getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, p); 135 if (propertyDescriptor != null) { 136 if (!isAnnotation) { 137 propertyCodegen.generatePrimaryConstructorProperty(p, propertyDescriptor); 138 } 139 else { 140 propertyCodegen.generateConstructorPropertyAsMethodForAnnotationClass(p, propertyDescriptor); 141 } 142 } 143 } 144 } 145 } 146 147 protected @NotNull List<JetParameter> getPrimaryConstructorParameters() { 148 if (myClass instanceof JetClass) { 149 return ((JetClass) myClass).getPrimaryConstructorParameters(); 150 } 151 return Collections.emptyList(); 152 } 153 154 private void generateStaticInitializer() { 155 if (clInitMethod != null) { 156 createOrGetClInitMethod(); 157 158 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) { 159 ExpressionCodegen codegen = createOrGetClInitCodegen(); 160 161 createOrGetClInitMethod().visitInsn(RETURN); 162 FunctionCodegen.endVisit(codegen.v, "static initializer", myClass); 163 } 164 } 165 } 166 167 @NotNull 168 protected MethodVisitor createOrGetClInitMethod() { 169 if (clInitMethod == null) { 170 clInitMethod = v.newMethod(null, ACC_STATIC, "<clinit>", "()V", null, null); 171 } 172 return clInitMethod; 173 } 174 175 @NotNull 176 protected ExpressionCodegen createOrGetClInitCodegen() { 177 assert state.getClassBuilderMode() == ClassBuilderMode.FULL; 178 if (clInitCodegen == null) { 179 MethodVisitor method = createOrGetClInitMethod(); 180 method.visitCode(); 181 SimpleFunctionDescriptorImpl clInit = 182 new SimpleFunctionDescriptorImpl(descriptor, Annotations.EMPTY, 183 Name.special("<clinit>"), 184 CallableMemberDescriptor.Kind.SYNTHESIZED); 185 clInit.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(), 186 Collections.<ValueParameterDescriptor>emptyList(), null, null, Visibilities.PRIVATE); 187 188 clInitCodegen = new ExpressionCodegen(method, new FrameMap(), Type.VOID_TYPE, context.intoFunction(clInit), state, this); 189 } 190 return clInitCodegen; 191 } 192 }