Fix linux/mac support.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_semantic_analysis_h_
20 #define _include_semantic_analysis_h_
30 class SemanticAnalysis : public AstVisitor
36 NamedFunction *function_;
45 // Tuple that describes whether a path terminates control flow, and
46 // if it is guaranteed to end in a return.
50 ReturnStatus returns_;
53 ControlStatus(bool terminates, ReturnStatus returns)
54 : terminates_(terminates),
59 bool terminates() const {
62 ReturnStatus returns() const {
67 // Used to keep track of terminating control flow and return statements
68 // inside control structures.
71 ReturnStatus returns_;
72 ControlScope **prevp_;
77 ControlScope(ControlScope **prev)
78 : returns_(Returns_Never),
86 ControlStatus status() const {
87 return ControlStatus(terminates_, returns_);
90 // We allow multiple terminators, but only the first one takes effect.
91 // For example, |break; continue;| is legal but the continue is
93 void returns(ReturnStatus status) {
96 if (status > returns_)
99 void terminate(ReturnStatus status) {
105 void merge(const ControlStatus &left, const ControlStatus &right);
106 void inherit(const ControlStatus &other);
122 LoopScope(LoopScope **prev)
142 unsigned breaks() const {
145 unsigned continues() const {
150 ControlScope *control_;
154 bool canTest(AstNode *node, Handle<Type> type);
155 void typeMismatch(const SourcePosition &pos, Handle<Type> actual, Handle<Type> given);
156 void unaryTypeError(AstNode *node, TokenKind token, Handle<Type> type);
157 void binaryTypeError(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right);
158 Type *typeCheckBinary(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right);
160 Type *bindType(const SourcePosition &pos, BoundName *name);
162 // These check if one type is compatible with another type, for various
164 // return (expr) matches function return type
165 // call(a, b) matches expected arg type
166 // a = b matches declared type
168 bool typeCheckArrayAssignment(const SourcePosition &pos, Handle<Type> given, Handle<Type> actual, bool forAssign);
169 bool typeCheckAssignment(const SourcePosition &pos, Handle<Type> given, Handle<Type> actual, bool forAssign);
170 bool typeCheckParameter(AstNode *node, Handle<Type> given, Handle<Type> actual);
172 Type *visitForLoad(Expression *expression);
173 bool preanalyze(FunctionStatement *node);
174 Type *visitForLValue(Expression *expression);
175 Type *visitAndApplyType(Expression *expression);
176 ControlStatus visitForControl(Statement *statement);
178 bool evaluateDimensions(ExpressionList *exprs, int dims[MAX_ARRAY_DEPTH]);
179 bool inferFixedArrayLiteral(ArrayLiteral *literal,
181 int dims[MAX_ARRAY_DEPTH],
185 // Builds an array type used for parameters, returns, or field entries.
186 Type *buildArrayType(Handle<Type> type, ExpressionList *dimensions, bool isConst);
188 Box *evaluateDefaultParameter(Handle<Type> type, Parameter *parameter);
189 bool checkArgumentCount(Handle<Type> type, unsigned actual);
190 bool buildFunctionArgs(AstNode *node, TokenKind kind, const FunctionSignature &sig,
191 FixedArray **args, FixedArray **defaults);
193 void markCallResultUsed(CallExpression *call);
195 bool determineLiveFunctions();
198 SemanticAnalysis(Zone *zone, CompileContext &cc, ParseTree *tree);
202 void visit(NameProxy *name);
203 void visit(Assignment *node);
204 void visit(BinaryExpression *node);
205 void visit(ReturnStatement *node);
206 void visit(ForStatement *node);
207 void visit(BlockStatement *node);
208 void visit(VariableDeclaration *node);
209 void visit(IntegerLiteral *node);
210 void visit(ExpressionStatement *node);
211 void visit(FunctionStatement *node);
212 void visit(CallExpression *node);
213 void visit(IfStatement *node);
214 void visit(IndexExpression *node);
215 void visit(EnumStatement *node);
216 void visit(FloatLiteral *node);
217 void visit(WhileStatement *node);
218 void visit(BreakStatement *node);
219 void visit(ContinueStatement *node);
220 void visit(StringLiteral *node);
221 void visit(IncDecExpression *node);
222 void visit(UnaryExpression *node);
223 void visit(TernaryExpression *node);
224 void visit(BooleanLiteral *node);
225 void visit(StructureStatement *node);
226 void visit(FieldExpression *node);
227 void visit(SwitchStatement *node);
228 void visit(FunctionTypeStatement *node);
229 void visit(ArrayLiteral *node);
230 void visit(ImportStatement *node);
233 const char *GetTypeName(Type *type);
237 #endif // _include_semantic_analysis_h_