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 046 private MethodVisitor clInitMethod; 047 048 private ExpressionCodegen clInitCodegen; 049 050 protected ClassBodyCodegen( 051 @NotNull JetClassOrObject aClass, 052 @NotNull ClassContext context, 053 @NotNull ClassBuilder v, 054 @NotNull GenerationState state, 055 @Nullable MemberCodegen parentCodegen 056 ) { 057 super(state, parentCodegen, context, v); 058 descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass); 059 myClass = aClass; 060 this.kind = context.getContextKind(); 061 this.v = v; 062 } 063 064 public void generate() { 065 generateDeclaration(); 066 067 generateClassBody(); 068 069 generateSyntheticParts(); 070 071 generateStaticInitializer(); 072 073 if (state.getClassBuilderMode() == ClassBuilderMode.FULL && isTopLevelOrInnerClass(descriptor)) { 074 generateKotlinAnnotation(); 075 } 076 } 077 078 protected abstract void generateDeclaration(); 079 080 protected abstract void generateKotlinAnnotation(); 081 082 protected void generateSyntheticParts() { 083 } 084 085 private void generateClassBody() { 086 FunctionCodegen functionCodegen = new FunctionCodegen(context, v, state, this); 087 PropertyCodegen propertyCodegen = new PropertyCodegen(context, v, functionCodegen, this); 088 089 if (kind != OwnerKind.TRAIT_IMPL) { 090 //generate nested classes first and only then generate class body. It necessary to access to nested CodegenContexts 091 for (JetDeclaration declaration : myClass.getDeclarations()) { 092 if (shouldProcessFirst(declaration)) { 093 generateDeclaration(propertyCodegen, declaration); 094 } 095 } 096 } 097 098 for (JetDeclaration declaration : myClass.getDeclarations()) { 099 if (!shouldProcessFirst(declaration)) { 100 generateDeclaration(propertyCodegen, declaration); 101 } 102 } 103 104 generatePrimaryConstructorProperties(propertyCodegen, myClass); 105 } 106 107 private static boolean shouldProcessFirst(JetDeclaration declaration) { 108 return !(declaration instanceof JetProperty || declaration instanceof JetNamedFunction); 109 } 110 111 112 protected void generateDeclaration(PropertyCodegen propertyCodegen, JetDeclaration declaration) { 113 if (declaration instanceof JetProperty || declaration instanceof JetNamedFunction) { 114 genFunctionOrProperty(context, (JetTypeParameterListOwner) declaration, v); 115 } 116 else if (declaration instanceof JetClassOrObject) { 117 if (declaration instanceof JetEnumEntry && !enumEntryNeedSubclass(state.getBindingContext(), (JetEnumEntry) declaration)) { 118 return; 119 } 120 121 genClassOrObject(context, (JetClassOrObject) declaration); 122 } 123 else if (declaration instanceof JetClassObject) { 124 genClassOrObject(context, ((JetClassObject) declaration).getObjectDeclaration()); 125 } 126 } 127 128 private void generatePrimaryConstructorProperties(PropertyCodegen propertyCodegen, JetClassOrObject origin) { 129 boolean isAnnotation = origin instanceof JetClass && ((JetClass) origin).isAnnotation(); 130 for (JetParameter p : getPrimaryConstructorParameters()) { 131 if (p.getValOrVarNode() != null) { 132 PropertyDescriptor propertyDescriptor = state.getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, p); 133 if (propertyDescriptor != null) { 134 if (!isAnnotation) { 135 propertyCodegen.generatePrimaryConstructorProperty(p, propertyDescriptor); 136 } 137 else { 138 propertyCodegen.generateConstructorPropertyAsMethodForAnnotationClass(p, propertyDescriptor); 139 } 140 } 141 } 142 } 143 } 144 145 protected @NotNull List<JetParameter> getPrimaryConstructorParameters() { 146 if (myClass instanceof JetClass) { 147 return ((JetClass) myClass).getPrimaryConstructorParameters(); 148 } 149 return Collections.emptyList(); 150 } 151 152 private void generateStaticInitializer() { 153 if (clInitMethod != null) { 154 createOrGetClInitMethod(); 155 156 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) { 157 ExpressionCodegen codegen = createOrGetClInitCodegen(); 158 159 createOrGetClInitMethod().visitInsn(RETURN); 160 FunctionCodegen.endVisit(codegen.v, "static initializer", myClass); 161 } 162 } 163 } 164 165 @NotNull 166 protected MethodVisitor createOrGetClInitMethod() { 167 if (clInitMethod == null) { 168 clInitMethod = v.newMethod(null, ACC_STATIC, "<clinit>", "()V", null, null); 169 } 170 return clInitMethod; 171 } 172 173 @NotNull 174 protected ExpressionCodegen createOrGetClInitCodegen() { 175 assert state.getClassBuilderMode() == ClassBuilderMode.FULL; 176 if (clInitCodegen == null) { 177 MethodVisitor method = createOrGetClInitMethod(); 178 method.visitCode(); 179 SimpleFunctionDescriptorImpl clInit = 180 new SimpleFunctionDescriptorImpl(descriptor, Annotations.EMPTY, 181 Name.special("<clinit>"), 182 CallableMemberDescriptor.Kind.SYNTHESIZED); 183 clInit.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(), 184 Collections.<ValueParameterDescriptor>emptyList(), null, null, Visibilities.PRIVATE); 185 186 clInitCodegen = new ExpressionCodegen(method, new FrameMap(), Type.VOID_TYPE, context.intoFunction(clInit), state, this); 187 } 188 return clInitCodegen; 189 } 190 }