001    /*
002     * Copyright 2010-2015 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.kotlin.codegen;
018    
019    import com.intellij.util.ArrayUtil;
020    import kotlin.jvm.functions.Function0;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.kotlin.codegen.context.FieldOwnerContext;
023    import org.jetbrains.kotlin.codegen.state.GenerationState;
024    import org.jetbrains.kotlin.psi.JetDeclaration;
025    import org.jetbrains.kotlin.psi.JetFile;
026    import org.jetbrains.kotlin.psi.JetNamedFunction;
027    import org.jetbrains.kotlin.psi.JetProperty;
028    import org.jetbrains.org.objectweb.asm.Type;
029    
030    import static org.jetbrains.kotlin.codegen.AsmUtil.writeKotlinSyntheticClassAnnotation;
031    import static org.jetbrains.kotlin.load.java.JvmAnnotationNames.KotlinSyntheticClass;
032    import static org.jetbrains.org.objectweb.asm.Opcodes.*;
033    
034    public class PackagePartCodegen extends MemberCodegen<JetFile> {
035        private final Type packagePartType;
036    
037        public PackagePartCodegen(
038                @NotNull ClassBuilder v,
039                @NotNull JetFile file,
040                @NotNull Type packagePartType,
041                @NotNull FieldOwnerContext context,
042                @NotNull GenerationState state
043        ) {
044            super(state, null, context, file, v);
045            this.packagePartType = packagePartType;
046        }
047    
048        @Override
049        protected void generateDeclaration() {
050            v.defineClass(element, V1_6,
051                          ACC_PUBLIC | ACC_FINAL,
052                          packagePartType.getInternalName(),
053                          null,
054                          "java/lang/Object",
055                          ArrayUtil.EMPTY_STRING_ARRAY
056            );
057            v.visitSource(element.getName(), null);
058    
059            generatePropertyMetadataArrayFieldIfNeeded(packagePartType);
060        }
061    
062        @Override
063        protected void generateBody() {
064            for (JetDeclaration declaration : element.getDeclarations()) {
065                if (declaration instanceof JetNamedFunction || declaration instanceof JetProperty) {
066                    genFunctionOrProperty(declaration);
067                }
068            }
069    
070            if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
071                generateInitializers(new Function0<ExpressionCodegen>() {
072                    @Override
073                    public ExpressionCodegen invoke() {
074                        return createOrGetClInitCodegen();
075                    }
076                });
077            }
078        }
079    
080        @Override
081        protected void generateKotlinAnnotation() {
082            writeKotlinSyntheticClassAnnotation(v, KotlinSyntheticClass.Kind.PACKAGE_PART);
083        }
084    }