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