Added runtime support for default arguments.
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_
29 class SemanticAnalysis : public AstVisitor
35 NamedFunction *function_;
44 // Tuple that describes whether a path terminates control flow, and
45 // if it is guaranteed to end in a return.
49 ReturnStatus returns_;
52 ControlStatus(bool terminates, ReturnStatus returns)
53 : terminates_(terminates),
58 bool terminates() const {
61 ReturnStatus returns() const {
66 // Used to keep track of terminating control flow and return statements
67 // inside control structures.
70 ReturnStatus returns_;
71 ControlScope **prevp_;
76 ControlScope(ControlScope **prev)
77 : returns_(Returns_Never),
85 ControlStatus status() const {
86 return ControlStatus(terminates_, returns_);
89 // We allow multiple terminators, but only the first one takes effect.
90 // For example, |break; continue;| is legal but the continue is
92 void returns(ReturnStatus status) {
95 if (status > returns_)
98 void terminate(ReturnStatus status) {
104 void merge(const ControlStatus &left, const ControlStatus &right);
120 LoopScope(LoopScope **prev)
140 unsigned breaks() const {
143 unsigned continues() const {
148 ControlScope *control_;
152 bool canTest(AstNode *node, Handle<Type> type);
153 void typeMismatch(const SourcePosition &pos, Handle<Type> actual, Handle<Type> given);
154 void unaryTypeError(AstNode *node, TokenKind token, Handle<Type> type);
155 void binaryTypeError(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right);
156 Type *typeCheckBinary(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right);
158 Type *bindType(const SourcePosition &pos, BoundName *name);
160 // These check if one type is compatible with another type, for various
162 // return (expr) matches function return type
163 // call(a, b) matches expected arg type
164 // a = b matches declared type
166 bool typeCheckArrayAssignment(const SourcePosition &pos, Handle<Type> given, Handle<Type> actual, bool forAssign);
167 bool typeCheckAssignment(const SourcePosition &pos, Handle<Type> given, Handle<Type> actual, bool forAssign);
168 bool typeCheckParameter(AstNode *node, Handle<Type> given, Handle<Type> actual);
170 Type *visitForLoad(Expression *expression);
171 bool preanalyze(FunctionStatement *node);
172 Type *visitForLValue(Expression *expression);
173 Type *visitAndApplyType(Expression *expression);
174 ControlStatus visitForControl(Statement *statement);
176 Box *evaluateDefaultParameter(Handle<Type> type, Parameter *parameter);
177 bool checkArgumentCount(Handle<Type> type, unsigned actual);
179 void markCallResultUsed(CallExpression *call);
181 bool determineLiveFunctions();
184 SemanticAnalysis(Zone *zone, CompileContext &cc, ParseTree *tree);
188 void visit(NameProxy *name);
189 void visit(Assignment *node);
190 void visit(BinaryExpression *node);
191 void visit(ReturnStatement *node);
192 void visit(ForStatement *node);
193 void visit(BlockStatement *node);
194 void visit(VariableDeclaration *node);
195 void visit(IntegerLiteral *node);
196 void visit(ExpressionStatement *node);
197 void visit(FunctionStatement *node);
198 void visit(CallExpression *node);
199 void visit(IfStatement *node);
200 void visit(IndexExpression *node);
201 void visit(EnumStatement *node);
202 void visit(FloatLiteral *node);
203 void visit(WhileStatement *node);
204 void visit(BreakStatement *node);
205 void visit(ContinueStatement *node);
206 void visit(StringLiteral *node);
207 void visit(IncDecExpression *node);
208 void visit(UnaryExpression *node);
209 void visit(TernaryExpression *node);
210 void visit(BooleanLiteral *node);
211 void visit(StructureStatement *node);
212 void visit(FieldExpression *node);
215 const char *GetTypeName(Type *type);
219 #endif // _include_semantic_analysis_h_