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.lang.cfg;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.cfg.pseudocode.PseudoValue;
022    import org.jetbrains.jet.lang.cfg.pseudocode.Pseudocode;
023    import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
024    import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
025    import org.jetbrains.jet.lang.psi.*;
026    import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
027    import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
028    
029    import java.util.List;
030    
031    public interface JetControlFlowBuilder {
032        // Subroutines
033        void enterSubroutine(@NotNull JetElement subroutine);
034    
035        @NotNull
036        Pseudocode exitSubroutine(@NotNull JetElement subroutine);
037    
038        @NotNull
039        JetElement getCurrentSubroutine();
040        @Nullable
041        JetElement getReturnSubroutine();
042    
043        // Lexical scopes
044        void enterLexicalScope(@NotNull JetElement element);
045        void exitLexicalScope(@NotNull JetElement element);
046    
047        // Entry/exit points
048        @NotNull
049        Label getEntryPoint(@NotNull JetElement labelElement);
050        @NotNull
051        Label getExitPoint(@NotNull JetElement labelElement);
052    
053        // Declarations
054        void declareParameter(@NotNull JetParameter parameter);
055        void declareVariable(@NotNull JetVariableDeclaration property);
056        void declareFunction(@NotNull JetElement subroutine, @NotNull Pseudocode pseudocode);
057    
058        // Labels
059        @NotNull
060        Label createUnboundLabel();
061        @NotNull
062        Label createUnboundLabel(@NotNull String name);
063    
064        void bindLabel(@NotNull Label label);
065    
066        // Jumps
067        void jump(@NotNull Label label, @NotNull JetElement element);
068        void jumpOnFalse(@NotNull Label label, @NotNull JetElement element, @Nullable PseudoValue conditionValue);
069        void jumpOnTrue(@NotNull Label label, @NotNull JetElement element, @Nullable PseudoValue conditionValue);
070        void nondeterministicJump(@NotNull Label label, @NotNull JetElement element, @Nullable PseudoValue inputValue); // Maybe, jump to label
071        void nondeterministicJump(@NotNull List<Label> label, @NotNull JetElement element);
072        void jumpToError(@NotNull JetElement element);
073    
074        void returnValue(@NotNull JetReturnExpression returnExpression, @NotNull PseudoValue returnValue, @NotNull JetElement subroutine);
075        void returnNoValue(@NotNull JetReturnExpression returnExpression, @NotNull JetElement subroutine);
076    
077        void throwException(@NotNull JetThrowExpression throwExpression, @NotNull PseudoValue thrownValue);
078    
079        // Loops
080        LoopInfo enterLoop(@NotNull JetExpression expression, @Nullable Label loopExitPoint, @Nullable Label conditionEntryPoint);
081    
082        void exitLoop(@NotNull JetExpression expression);
083        @Nullable
084        JetElement getCurrentLoop();
085    
086        // Try-Finally
087        void enterTryFinally(@NotNull GenerationTrigger trigger);
088        void exitTryFinally();
089    
090        void repeatPseudocode(@NotNull Label startLabel, @NotNull Label finishLabel);
091    
092        // Reading values
093        void mark(@NotNull JetElement element);
094    
095        @Nullable
096        PseudoValue getBoundValue(@Nullable JetElement element);
097        void bindValue(@NotNull PseudoValue value, @NotNull JetElement element);
098    
099        void loadUnit(@NotNull JetExpression expression);
100    
101        @NotNull
102        PseudoValue loadConstant(@NotNull JetExpression expression, @Nullable CompileTimeConstant<?> constant);
103        @NotNull
104        PseudoValue createAnonymousObject(@NotNull JetObjectLiteralExpression expression);
105        @NotNull
106        PseudoValue createFunctionLiteral(@NotNull JetFunctionLiteralExpression expression);
107        @NotNull
108        PseudoValue loadStringTemplate(@NotNull JetStringTemplateExpression expression, @NotNull List<PseudoValue> inputValues);
109    
110        @NotNull
111        PseudoValue magic(
112                @NotNull JetElement instructionElement,
113                @Nullable JetElement valueElement,
114                @NotNull List<PseudoValue> inputValues,
115                boolean synthetic
116        );
117    
118        @NotNull
119        PseudoValue readThis(@NotNull JetExpression expression, @Nullable ReceiverParameterDescriptor parameterDescriptor);
120        @NotNull
121        PseudoValue readVariable(
122                @NotNull JetExpression expression, @Nullable VariableDescriptor variableDescriptor, @Nullable PseudoValue receiverValue
123        );
124    
125        @Nullable
126        PseudoValue call(@NotNull JetExpression expression, @NotNull ResolvedCall<?> resolvedCall, @NotNull List<PseudoValue> inputValues);
127    
128        enum PredefinedOperation {
129            AND,
130            OR,
131            NOT_NULL_ASSERTION
132        }
133        @NotNull
134        PseudoValue predefinedOperation(
135                @NotNull JetExpression expression,
136                @NotNull PredefinedOperation operation,
137                @NotNull List<PseudoValue> inputValues
138        );
139    
140        void compilationError(@NotNull JetElement element, @NotNull String message);
141    
142        void write(@NotNull JetElement assignment, @NotNull JetElement lValue, @NotNull PseudoValue rValue, @Nullable PseudoValue receiverValue);
143        
144        // Other
145        void unsupported(JetElement element);
146    }