Structure declaration 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_
27 class SemanticAnalysis : public AstVisitor
33 NamedFunction *function_;
42 // Tuple that describes whether a path terminates control flow, and
43 // if it is guaranteed to end in a return.
47 ReturnStatus returns_;
50 ControlStatus(bool terminates, ReturnStatus returns)
51 : terminates_(terminates),
56 bool terminates() const {
59 ReturnStatus returns() const {
64 // Used to keep track of terminating control flow and return statements
65 // inside control structures.
68 ReturnStatus returns_;
69 ControlScope **prevp_;
74 ControlScope(ControlScope **prev)
75 : returns_(Returns_Never),
83 ControlStatus status() const {
84 return ControlStatus(terminates_, returns_);
87 // We allow multiple terminators, but only the first one takes effect.
88 // For example, |break; continue;| is legal but the continue is
90 void returns(ReturnStatus status) {
93 if (status > returns_)
96 void terminate(ReturnStatus status) {
102 void merge(const ControlStatus &left, const ControlStatus &right);
118 LoopScope(LoopScope **prev)
138 unsigned breaks() const {
141 unsigned continues() const {
146 ControlScope *control_;
150 bool canTest(AstNode *node, Handle<Type> type);
151 void typeMismatch(AstNode *node, Handle<Type> actual, Handle<Type> given);
152 void unaryTypeError(AstNode *node, TokenKind token, Handle<Type> type);
153 void binaryTypeError(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right);
154 Type *typeCheckBinary(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right);
156 Type *bindType(const SourcePosition &pos, BoundName *name);
158 // These check if one type is compatible with another type, for various
160 // return (expr) matches function return type
161 // call(a, b) matches expected arg type
162 // a = b matches declared type
164 bool typeCheckArrayAssignment(AstNode *node, Handle<Type> given, Handle<Type> actual, bool forAssign);
165 bool typeCheckAssignment(AstNode *node, Handle<Type> given, Handle<Type> actual, bool forAssign);
166 bool typeCheckParameter(AstNode *node, Handle<Type> given, Handle<Type> actual);
168 Type *visitForLoad(Expression *expression);
169 bool preanalyze(FunctionStatement *node);
170 Type *visitForLValue(Expression *expression);
171 Type *visitAndApplyType(Expression *expression);
172 ControlStatus visitForControl(Statement *statement);
174 void markCallResultUsed(CallExpression *call);
176 bool determineLiveFunctions();
179 SemanticAnalysis(Zone *zone, CompileContext &cc, ParseTree *tree);
183 void visit(NameProxy *name);
184 void visit(Assignment *node);
185 void visit(BinaryExpression *node);
186 void visit(ReturnStatement *node);
187 void visit(ForStatement *node);
188 void visit(BlockStatement *node);
189 void visit(VariableDeclaration *node);
190 void visit(IntegerLiteral *node);
191 void visit(ExpressionStatement *node);
192 void visit(FunctionStatement *node);
193 void visit(CallExpression *node);
194 void visit(IfStatement *node);
195 void visit(IndexExpression *node);
196 void visit(EnumStatement *node);
197 void visit(FloatLiteral *node);
198 void visit(WhileStatement *node);
199 void visit(BreakStatement *node);
200 void visit(ContinueStatement *node);
201 void visit(StringLiteral *node);
202 void visit(IncDecExpression *node);
203 void visit(UnaryExpression *node);
204 void visit(TernaryExpression *node);
205 void visit(BooleanLiteral *node);
206 void visit(StructureStatement *node);
209 const char *GetTypeName(Type *type);
213 #endif // _include_semantic_analysis_h_