src/compiler/SemanticAnalysis.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 18 14:53:23 2012 -0800 (2012-11-18)
changeset 212 443d24cfdf2d
parent 208 7c5be05296e3
child 215 b8f650a441a5
permissions -rw-r--r--
Added array support back.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn 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)
    10  * any later version.
    11  * 
    12  * SourcePawn 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.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include <assert.h>
    20 #include "../Zone.h"
    21 #include "../Strings.h"
    22 #include "../NativeTable.h"
    23 #include "../TypeManager.h"
    24 #include "../Modules.h"
    25 #include "../Code.h"
    26 #include "SemanticAnalysis.h"
    27 #include "../Heap-inl.h"
    28 
    29 using namespace ke;
    30 
    31 #define __ emitter_.
    32 
    33 typedef BytecodeEmitter::Label Label;
    34 
    35 class AutoEnterScope
    36 {
    37   public:
    38     AutoEnterScope(Scope **prevp, Scope *scope)
    39       : prevp_(prevp),
    40         prev_(*prevp),
    41         scope_(scope)
    42     {
    43         if (!scope_)
    44             return;
    45         prev_ = *prevp_;
    46         *prevp_ = scope;
    47         assert(prev_ == scope->enclosing());
    48     }
    49     ~AutoEnterScope()
    50     {
    51         if (scope_)
    52             *prevp_ = prev_;
    53     }
    54   private:
    55     Scope **prevp_;
    56     Scope *prev_;
    57     Scope *scope_;
    58 };
    59 
    60 SemanticAnalysis::SemanticAnalysis(Zone *zone, CompileContext &cc, TranslationUnit *unit)
    61   : zone_(zone),
    62     pool_(zone->pool()),
    63     cc_(cc),
    64     unit_(unit),
    65     hir_(NULL),
    66     emitter_(pool_),
    67     module_(NULL),
    68     loop_(NULL)
    69 {
    70 }
    71 
    72 SemanticAnalysis::SemanticAnalysis(SemanticAnalysis *parent, Handle<FunctionType> fun)
    73   : zone_(parent->zone_),
    74     pool_(parent->pool_),
    75     cc_(parent->cc_),
    76     unit_(parent->unit_),
    77     hir_(NULL),
    78     scope_(parent->scope_),
    79     emitter_(pool_),
    80     module_(parent->module_),
    81     fun_(fun),
    82     loop_(NULL)
    83 {
    84 
    85 }
    86 
    87 void
    88 SemanticAnalysis::analyze()
    89 {
    90     scope_ = unit_->globalScope();
    91 
    92     module_ = Module::New(zone_);
    93 
    94     for (size_t i = 0; i < unit_->tree()->statements()->length(); i++) {
    95         Statement *stmt = unit_->tree()->statements()->at(i);
    96         stmt->accept(this);
    97     }
    98 
    99     Local<Struct> globals(zone_, unit_->globalScope()->build(zone_));
   100     if (!globals)
   101         return;
   102     module_->setGlobals(globals);
   103 
   104     Local<Code> code(zone_, __ finish());
   105     if (!code)
   106         return;
   107     code->setModule(module_);
   108     module_->setCode(code);
   109 
   110 #ifndef NDEBUG
   111 //    Disassemble(code, stdout);
   112 #endif
   113 
   114     unit_->module_ = module_;
   115 }
   116 
   117 void
   118 SemanticAnalysis::visitFunctionStatement(FunctionStatement *stmt)
   119 {
   120     FunctionSymbol *sym = stmt->sym();
   121     Local<FunctionType> type(zone_, sym->type());
   122 
   123     assert(scope_->kind() == Scope::GLOBAL);
   124 
   125     if (stmt->token() == TOK_NATIVE) {
   126         // Define the native now.
   127         size_t index;
   128         if (!zone_->nativeTable()->add(stmt->name(), &index))
   129             return;
   130 
   131         Local<Native> native(zone_, Native::New(zone_, type, index));
   132         if (!native)
   133             return;
   134 
   135         __ object(native);
   136         __ setglobal(sym);
   137         __ pop();
   138     }
   139 
   140     // If the function is a forward or native, we don't have to do anything.
   141     if (stmt->token() == TOK_NATIVE || stmt->token() == TOK_FORWARD)
   142         return;
   143 
   144     SemanticAnalysis child(this, type);
   145     Local<Function> funobj(zone_, child.compile(stmt));
   146     if (!funobj)
   147         return;
   148 
   149     __ object(funobj);
   150     __ setglobal(sym);
   151     __ public_();
   152 }
   153 
   154 Function *
   155 SemanticAnalysis::compile(FunctionStatement *stmt)
   156 {
   157     __ setFirstLine(stmt->pos().line);
   158 
   159     AutoEnterScope argScope(&scope_, stmt->argScope());
   160     AutoEnterScope varScope(&scope_, stmt->varScope());
   161 
   162     stmt->body()->accept(this);
   163 
   164     Local<Code> code(zone_, __ finish());
   165     if (!code)
   166         return NULL;
   167     code->setModule(module_);
   168 
   169     Local<Function> fun(zone_, Function::New(zone_, code, fun_, stmt->name(), stmt->pos()));
   170     if (!fun)
   171         return NULL;
   172 
   173 #ifndef NDEBUG
   174 //    Disassemble(code, stdout);
   175 #endif
   176 
   177     return fun;
   178 }
   179 
   180 void 
   181 SemanticAnalysis::visitBlockStatement(BlockStatement *node)
   182 {
   183     AutoEnterScope scope(&scope_, node->scope());
   184 
   185     for (size_t i = 0; i < node->statements()->length(); i++) {
   186         Statement *stmt = node->statements()->at(i);
   187         stmt->accept(this);
   188         assert(__ stackDepth() == 0);
   189     }
   190 }
   191 
   192 void
   193 SemanticAnalysis::visitExpressionStatement(ExpressionStatement *node)
   194 {
   195     // While building HIR, we enter new pool and root scopes, since HIR
   196     // creation is purely temporary.
   197     PoolScope poolScope(zone_->pool());
   198     RootScope rootScope(zone_);
   199 
   200     HIR *hir = eval(node->expression());
   201     if (!hir)
   202         return;
   203 
   204     discharge(hir);
   205     __ pop();
   206 }
   207 
   208 void
   209 SemanticAnalysis::visitIfStatement(IfStatement *node)
   210 {
   211     Label join;
   212 
   213     if (!node->ifFalse()) {
   214         Label success;
   215 
   216         visitForTest(node->condition(), &success, &join, &success);
   217         __ bind(&success);
   218         node->ifTrue()->accept(this);
   219     } else {
   220         Label success, next;
   221 
   222         visitForTest(node->condition(), &success, &next, &success);
   223         __ bind(&success);
   224         node->ifTrue()->accept(this);
   225         __ jump(&join);
   226         __ bind(&next);
   227         node->ifFalse()->accept(this);
   228     }
   229 
   230     __ bind(&join);
   231 }
   232 
   233 void
   234 SemanticAnalysis::visitForStatement(ForStatement *node)
   235 {
   236     AutoEnterScope scope(&scope_, node->scope());
   237     PoolScope poolScope(zone_->pool());
   238     RootScope rootScope(zone_);
   239 
   240     Label join, update;
   241     LoopScope loop(scope_, &join, &update, &loop_);
   242 
   243     if (node->initialization())
   244         node->initialization()->accept(this);
   245 
   246     Label header, test;
   247     if (node->condition())
   248         __ jump(&test);
   249 
   250     __ bind(&header);
   251     node->body()->accept(this);
   252 
   253     __ bind(&update);
   254     if (node->update())
   255         node->update()->accept(this);
   256 
   257     if (node->condition()) {
   258         __ bind(&test);
   259         visitForTest(node->condition(), &header, &join, &join);
   260     } else {
   261         __ jump(&header);
   262     }
   263 
   264     __ bind(&join);
   265 
   266     assert(__ stackDepth() == 0);
   267 }
   268 
   269 void
   270 SemanticAnalysis::visitWhileStatement(WhileStatement *node)
   271 {
   272     PoolScope poolScope(zone_->pool());
   273     RootScope rootScope(zone_);
   274 
   275     Label header, join;
   276 
   277     __ bind(&header);
   278 
   279     LoopScope loop(scope_, &join, &header, &loop_);
   280     if (node->token() == TOK_WHILE) {
   281         Label body;
   282         visitForTest(node->condition(), &body, &join, &body);
   283         node->body()->accept(this);
   284         __ jump(&header);
   285     } else {
   286         node->body()->accept(this);
   287         visitForTest(node->condition(), &header, &join, &join);
   288     }
   289 
   290     __ bind(&join);
   291 
   292     assert(__ stackDepth() == 0);
   293 }
   294 
   295 static inline Heap::Tenure
   296 InferTenure(Symbol *sym)
   297 {
   298     if (sym->scope()->kind() == Scope::GLOBAL)
   299         return Heap::Tenure_Old;
   300     // :TODO: need to use LIFO!
   301     return Heap::Tenure_Default;
   302 }
   303 
   304 void
   305 SemanticAnalysis::visitVariableDeclaration(VariableDeclaration *node)
   306 {
   307     PoolScope poolScope(zone_->pool());
   308     RootScope rootScope(zone_);
   309 
   310     if (node->initialization()) {
   311         HIR *hir = NULL;
   312 
   313         if ((hir = eval(node->initialization())) == NULL)
   314             return;
   315         if ((hir = coerce(hir, node->sym()->type(), Coerce_Assign)) == NULL)
   316             return;
   317 
   318         discharge(hir);
   319     } else {
   320         if (node->sym()->type()->isInt32() ||
   321             node->sym()->type()->isEnum() ||
   322             node->sym()->type()->isChar() ||
   323             node->sym()->type()->isBool())
   324         {
   325             __ int_(0);
   326         } else if (node->sym()->type()->isFloat()) {
   327             __ float_(0);
   328         } else if (node->sym()->type()->isArray()) {
   329             Local<ArrayType> type(zone_, ArrayType::cast(node->sym()->type()));
   330             Local<ArrayMap> map(zone_, ArrayMap::Attach(zone_, type));
   331             if (!map)
   332                 return;
   333 
   334             if (type->isFixedLength()) {
   335                 __ newarray(OP_NEWFIXED, map, InferTenure(node->sym()));
   336             } else if (node->dims()) {
   337                 assert(false);
   338                 __ newarray(OP_NEWSIZED, map, InferTenure(node->sym()));
   339             } else {
   340                 __ newarray(OP_NEWEMPTY, map, InferTenure(node->sym()));
   341             }
   342         }
   343     }
   344 
   345     if (!__ allocate(node->sym()))
   346         return;
   347 
   348     set(node->sym());
   349     __ pop();
   350 }
   351 
   352 void
   353 SemanticAnalysis::visitReturnStatement(ReturnStatement *node)
   354 {
   355     if (!node->expression()) {
   356         if (!fun_->returnType()->isVoid()) {
   357             cc_.reportError(node->pos(), Message_UsedVoidReturn);
   358             return;
   359         }
   360 
   361         __ returnvoid();
   362         return;
   363     }
   364 
   365     PoolScope poolScope(zone_->pool());
   366     RootScope rootScope(zone_);
   367 
   368     HIR *hir = eval(node->expression());
   369     if (!hir)
   370         return;
   371 
   372     Local<Type> returnType(zone_, fun_->returnType());
   373     if ((hir = coerce(hir, returnType, Coerce_Return)) == NULL)
   374         return;
   375 
   376     discharge(hir);
   377     __ return_();
   378 }
   379 
   380 void
   381 SemanticAnalysis::visitBinaryExpression(BinaryExpression *node)
   382 {
   383     HIR *left = eval(node->left());
   384     HIR *right = eval(node->right());
   385     if (!left || !right)
   386         return;
   387 
   388     hir_ = binary(node->token(), left, right);
   389 }
   390 
   391 bool
   392 SemanticAnalysis::checkArgumentCount(Handle<FunctionType> type, unsigned actual)
   393 {
   394     if (type->parameters()->length() == actual)
   395         return true;
   396 
   397     if (actual > type->parameters()->length()) {
   398         if (!type->isNative() || !type->isNativeVariadic())
   399             return false;
   400         return true;
   401     }
   402 
   403     if (!type->defaults())
   404         return false;
   405 
   406     // Missing arguments. See if there's a default argument for each missing
   407     // actual.
   408     for (unsigned i = actual; i < type->parameters()->length(); i++) {
   409         if (!type->defaults()->at(i))
   410             return false;
   411     }
   412 
   413     return true;
   414 }
   415 
   416 void
   417 SemanticAnalysis::visitNameProxy(NameProxy *proxy)
   418 {
   419     Symbol *sym = proxy->sym();
   420     VariableSymbol *var = sym->asVariable();
   421 
   422     Scope *in = sym->scope();
   423     switch (in->kind()) {
   424       case Scope::GLOBAL:
   425         hir_ = new (pool_) HGlobal(sym);
   426         return;
   427 
   428       default:
   429         assert(in->kind() == Scope::BLOCK);
   430         assert(var->storage() == VariableSymbol::Local);
   431         hir_ = new (pool_) HLocal(var);
   432         return;
   433     }
   434 }
   435 
   436 void
   437 SemanticAnalysis::visitIntegerLiteral(IntegerLiteral *lit)
   438 {
   439     Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
   440     hir_ = new (pool_) HInteger(type, lit->value());
   441 }
   442 
   443 void
   444 SemanticAnalysis::visitFloatLiteral(FloatLiteral *node)
   445 {
   446     Local<Type> type(zone_, zone_->types()->getPrimitive(PrimitiveType_Float));
   447     hir_ = new (pool_) HFloat(type, (float)node->value());
   448 }
   449 
   450 void
   451 SemanticAnalysis::visitCallExpression(CallExpression *node)
   452 {
   453     HIR *callee = eval(node->callee());
   454     if (!callee)
   455         return;
   456 
   457     if (!callee->type()->isFunction()) {
   458         cc_.reportError(node->pos(), Message_CalleeNotFunction);
   459         return;
   460     }
   461 
   462     Local<FunctionType> fun(zone_, FunctionType::cast(callee->type()));
   463     
   464     if (!checkArgumentCount(fun, node->arguments()->length())) {
   465         cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
   466         return;
   467     }
   468 
   469     if (fun->isForward()) {
   470         cc_.reportError(node->pos(), Message_ForwardNotImplemented, fun->name()->chars());
   471         return;
   472     }
   473 
   474     HIRList *args = new (pool_) HIRList;
   475     for (unsigned i = 0; i < node->arguments()->length(); i++) {
   476         Expression *expression = node->arguments()->at(i);
   477         HIR *arg = eval(expression);
   478         if (!arg)
   479             return;
   480 
   481         Handle<Type> given = arg->type();
   482         
   483         bool mustComputeReference;
   484         if (i >= fun->parameters()->length()) {
   485             // Special handling for natives. Arguments past the formal must
   486             // always have references computed. However, arrays are already
   487             // references for natives.
   488             assert(fun->isNative() && fun->isNativeVariadic());
   489             mustComputeReference = !given->isReference() &&
   490                                    !given->isArray();
   491         } else {
   492             Local<Type> actual(zone_, fun->parameterAt(i));
   493 
   494             if (expression->isIndexExpression()) {
   495                 // Temporarily disabled.
   496                 assert(!actual->isArray());
   497                 if (actual->isReference()) {
   498                     cc_.reportError(expression->pos(), Message_CannotComputeIndexRef);
   499                     return;
   500                 }
   501             }
   502 
   503             // When we start overloading, coercion will have to happen later.
   504             arg = coerce(arg, actual, Coerce_Arg);
   505             if (!arg)
   506                 return;
   507 
   508             mustComputeReference = actual->isReference() && !given->isReference();
   509         }
   510 
   511         // :TODO: handle mustComputeReference
   512         assert(!mustComputeReference);
   513 
   514         if (!args->append(arg))
   515             return;
   516     }
   517 
   518     hir_ = new (pool_) HCall(fun->returnType(), callee, args);
   519 }
   520 
   521 void
   522 SemanticAnalysis::visitAssignment(Assignment *node)
   523 {
   524     HLValue *lval = lvalue(node->lvalue());
   525     if (!lval)
   526         return;
   527 
   528     HIR *hir = eval(node->expression());
   529     if ((hir = coerce(hir, lval->type(), Coerce_Assign)) == NULL)
   530         return;
   531 
   532     hir_ = new (pool_) HStore(lval, hir);
   533 }
   534 
   535 void
   536 SemanticAnalysis::visitBreakStatement(BreakStatement *node)
   537 {
   538     if (!loop_) {
   539         cc_.reportError(node->pos(), Message_BreakOutsideLoop);
   540         return;
   541     }
   542 
   543     // Note, if we re-introduce LIFO allocs, we need to unwind here.
   544     __ jump(loop_->break_());
   545 }
   546 
   547 void
   548 SemanticAnalysis::visitContinueStatement(ContinueStatement *node)
   549 {
   550     if (!loop_) {
   551         cc_.reportError(node->pos(), Message_ContinueOutsideLoop);
   552         return;
   553     }
   554 
   555     // Note, if we re-introduce LIFO allocs, we need to unwind here.
   556     __ jump(loop_->continue_());
   557 }
   558 
   559 void
   560 SemanticAnalysis::visitIndexExpression(IndexExpression *node)
   561 {
   562     HIR *left = eval(node->left());
   563     if (!left)
   564         return;
   565 
   566     HIR *right = eval(node->right());
   567     if (!right)
   568         return;
   569 
   570     // Hrm... yes, for now, let's coerce here.
   571     Local<Type> i32(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
   572     if ((right = coerce(right, i32, Coerce_Arg)) == NULL)
   573         return;
   574     
   575     // Do not coerce here, there's no reason to yet.
   576     if (!left->type()->isArray()) {
   577         cc_.reportError(node->left()->pos(), Message_IndexBaseMustBeArray);
   578         return;
   579     }
   580 
   581     hir_ = new (pool_) HIndex(ArrayType::cast(left->type())->contained(),
   582                               left,
   583                               right);
   584 }
   585 
   586 Type *
   587 SemanticAnalysis::coercionType(TokenKind token, HIR *left, HIR *right)
   588 {
   589     Handle<Type> l = left->type();
   590     Handle<Type> r = right->type();
   591 
   592     if (token >= TOK_PLUS && token <= TOK_SLASH) {
   593         // We allow: [float|int32] + [float|int32]
   594         if (l->isFloat() && r->isInt32())
   595             return l;
   596         if (l->isInt32() && r->isFloat())
   597             return r;
   598         if (l->isInt32() && r->isInt32())
   599             return l;
   600     }
   601 
   602     assert(l == r);
   603 
   604     return left->type();
   605 }
   606 
   607 HIR *
   608 SemanticAnalysis::coerce(HIR *hir, Handle<Type> to, CoercionKind kind)
   609 {
   610     Handle<Type> from = hir->type();
   611 
   612     if (from->isInt32() && to->isFloat())
   613         return new (pool_) HConvert(to, OP_CVT_I2F, hir);
   614     if (from->isInt32() && to->isBool())
   615         return new (pool_) HConvert(to, OP_CVT_I2B, hir);
   616     
   617     assert(from == to);
   618     
   619     return hir;
   620 }
   621 
   622 void
   623 SemanticAnalysis::set(VariableSymbol *sym)
   624 {
   625     if (sym->scope()->kind() == Scope::GLOBAL) {
   626         __ setglobal(sym);
   627     } else {
   628         assert(sym->storage() == VariableSymbol::Local);
   629         __ setlocal(sym);
   630     }
   631 }
   632 
   633 HIR *
   634 SemanticAnalysis::binary(TokenKind token, HIR *left, HIR *right)
   635 {
   636     Local<Type> coercion(zone_, coercionType(token, left, right));
   637     if (!coercion)
   638         return NULL;
   639     if ((left = coerce(left, coercion, Coerce_Arg)) == NULL)
   640         return NULL;
   641     if ((right = coerce(right, coercion, Coerce_Arg)) == NULL)
   642         return NULL;
   643     
   644     switch (token) {
   645      case TOK_PLUS:
   646       case TOK_MINUS:
   647       case TOK_STAR:
   648       case TOK_SLASH:
   649       case TOK_PERCENT:
   650       case TOK_AMPERSAND:
   651       case TOK_BITOR:
   652       case TOK_BITXOR:
   653       case TOK_SHR:
   654       case TOK_USHR:
   655       case TOK_SHL:
   656         return new (pool_) HBinary(left->type(), token, left, right);
   657 
   658       case TOK_EQUALS:
   659       case TOK_NOTEQUALS:
   660       case TOK_LT:
   661       case TOK_LE:
   662       case TOK_GT:
   663       case TOK_GE:
   664       case TOK_AND:
   665       case TOK_OR:
   666       {
   667         Local<Type> result(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
   668         return new (pool_) HBinary(result, token, left, right);
   669       }
   670 
   671       default:
   672         assert(false);
   673         return NULL;
   674     }
   675 }
   676 
   677 static inline TokenKind
   678 ToCompare(Expression *expr)
   679 {
   680     if (expr->isBinaryExpression()) {
   681         BinaryExpression *b = expr->toBinaryExpression();
   682         if (b->token() >= TOK_EQUALS && b->token() <= TOK_OR)
   683             return b->token();
   684     } else if (expr->isUnaryExpression()) {
   685         UnaryExpression *u = expr->toUnaryExpression();
   686         if (u->token() == TOK_NOT)
   687             return TOK_NOT;
   688     }
   689     return TOK_EOF;
   690 }
   691 
   692 void
   693 SemanticAnalysis::visitForTest(Expression *expr,
   694                                Label *trueBranch,
   695                                Label *falseBranch,
   696                                Label *fallthrough)
   697 {
   698     Local<Type> boolType(zone_, zone_->types()->getPrimitive(PrimitiveType_Bool));
   699 
   700     TokenKind token = ToCompare(expr);
   701     if (token == TOK_EOF || token == TOK_NOT) {
   702         HIR *hir;
   703         if (token == TOK_EOF)
   704             hir = eval(expr);
   705         else
   706             hir = eval(expr->toUnaryExpression()->expression());
   707         if (!hir)
   708             return;
   709         if ((hir = coerce(hir, boolType, Coerce_Assign)) == NULL)
   710             return;
   711 
   712         discharge(hir);
   713 
   714         bool jumpOnTrue = (fallthrough == falseBranch) ^ (token == TOK_NOT);
   715         Opcode op = jumpOnTrue ? OP_JT : OP_JF;
   716         
   717         if (fallthrough == falseBranch)
   718             __ j(op, trueBranch);
   719         else
   720             __ j(op, falseBranch);
   721         return;
   722     }
   723 
   724     BinaryExpression *bin = expr->toBinaryExpression();
   725     if (token == TOK_AND || token == TOK_OR) {
   726         assert(false);
   727         //visitForLogicTest(expr, trueBranch, falseBranch, fallthrough);
   728         return;
   729     }
   730 
   731     HIR *left = eval(bin->left());
   732     HIR *right = eval(bin->right());
   733     if (!left || !right)
   734         return;
   735 
   736     HIR *hir = binary(bin->token(), left, right);
   737     if (!hir)
   738         return;
   739     if ((hir = coerce(hir, boolType, Coerce_Assign)) == NULL)
   740         return;
   741 
   742     discharge(hir);
   743     if (fallthrough == trueBranch)
   744         __ j(OP_JF, falseBranch);
   745     else
   746         __ j(OP_JT, trueBranch);
   747 }
   748 
   749 HLValue *
   750 SemanticAnalysis::lvalue(Expression *expr)
   751 {
   752     HIR *hir = eval(expr);
   753     if (!hir)
   754         return NULL;
   755     
   756     if (!hir->isLValue()) {
   757         cc_.reportError(expr->pos(), Message_ExpectedLValue);
   758         return NULL;
   759     }
   760 
   761     return static_cast<HLValue *>(hir);
   762 }
   763 
   764 HIR *
   765 SemanticAnalysis::eval(Expression *expr)
   766 {
   767     assert(!hir_);
   768     expr->accept(this);
   769 
   770     HIR *hir = hir_;
   771     hir_ = NULL;
   772 
   773     return hir;
   774 }
   775 
   776 void
   777 SemanticAnalysis::discharge(HIR *hir)
   778 {
   779     EmitHIR(zone_, emitter_, hir);
   780 }