void desugarFieldRef(FieldRefPtr x, ModulePtr module) { if (x->expr->exprKind == FIELD_REF || x->expr->exprKind == NAME_REF) { ModulePtr m = dottedImportedModule(x.ptr(), module.ptr()); if (m.ptr()) { x->isDottedModuleName = true; x->desugared = new ObjectExpr(m.ptr()); return; } } ExprListPtr args = new ExprList(x->expr); args->add(new ObjectExpr(x->name.ptr())); CallPtr call = new Call(operator_expr_fieldRef(module->cst), args); call->location = x->location; x->desugared = call.ptr(); }
void convertFreeVars(ExprPtr &x, EnvPtr env, LambdaContext &ctx) { switch (x->exprKind) { case BOOL_LITERAL : case INT_LITERAL : case FLOAT_LITERAL : case CHAR_LITERAL : case STRING_LITERAL : case IDENTIFIER_LITERAL : break; case NAME_REF : { NameRef *y = (NameRef *)x.ptr(); bool isNonLocal = false; bool isGlobal = false; ObjectPtr z = lookupEnvEx(env, y->name, ctx.nonLocalEnv, isNonLocal, isGlobal); if (isNonLocal && !isGlobal) { if ((z->objKind == PVALUE) || (z->objKind == CVALUE)) { TypePtr t = typeOfValue(z); if (isStaticOrTupleOfStatics(t)) { ExprListPtr args = new ExprList(); args->add(new ObjectExpr(t.ptr())); CallPtr call = new Call(operator_expr_typeToRValue(), args); call->location = y->location; x = call.ptr(); } else { addFreeVar(ctx, y->name->str); IdentifierPtr a = new Identifier(ctx.closureDataName); a->location = y->location; NameRefPtr b = new NameRef(a); b->location = y->location; FieldRefPtr c = new FieldRef(b.ptr(), y->name); c->location = y->location; if (ctx.captureByRef) { vector<int> ops; ops.push_back(DEREFERENCE); ExprPtr d = new VariadicOp(ops, new ExprList(c.ptr())); d->location = y->location; x = d.ptr(); } else { x = c.ptr(); } } } else if ((z->objKind == MULTI_PVALUE) || (z->objKind == MULTI_CVALUE)) { vector<TypePtr> types = typesOfValues(z); bool allStatic = true; for (unsigned i = 0; i < types.size(); ++i) { if (!isStaticOrTupleOfStatics(types[i])) { allStatic = false; break; } } if (allStatic) { ExprListPtr args = new ExprList(); for (unsigned i = 0; i < types.size(); ++i) args->add(new ObjectExpr(types[i].ptr())); CallPtr call = new Call(operator_expr_typesToRValues(), args); call->location = y->location; x = call.ptr(); } else { addFreeVar(ctx, y->name->str); IdentifierPtr a = new Identifier(ctx.closureDataName); a->location = y->location; NameRefPtr b = new NameRef(a); b->location = y->location; FieldRefPtr c = new FieldRef(b.ptr(), y->name); c->location = y->location; if (ctx.captureByRef) { ExprPtr f = operator_expr_unpackMultiValuedFreeVarAndDereference(); CallPtr d = new Call(f, new ExprList()); d->parenArgs->add(c.ptr()); x = d.ptr(); } else { ExprPtr f = operator_expr_unpackMultiValuedFreeVar(); CallPtr d = new Call(f, new ExprList()); d->parenArgs->add(c.ptr()); x = d.ptr(); } } } } break; } case TUPLE : { Tuple *y = (Tuple *)x.ptr(); convertFreeVars(y->args, env, ctx); break; } case PAREN : { Paren *y = (Paren *)x.ptr(); convertFreeVars(y->args, env, ctx); break; } case INDEXING : { Indexing *y = (Indexing *)x.ptr(); convertFreeVars(y->expr, env, ctx); convertFreeVars(y->args, env, ctx); break; } case CALL : { Call *y = (Call *)x.ptr(); convertFreeVars(y->expr, env, ctx); convertFreeVars(y->parenArgs, env, ctx); convertFreeVars(y->lambdaArgs, env, ctx); break; } case FIELD_REF : { FieldRef *y = (FieldRef *)x.ptr(); convertFreeVars(y->expr, env, ctx); break; } case STATIC_INDEXING : { StaticIndexing *y = (StaticIndexing *)x.ptr(); convertFreeVars(y->expr, env, ctx); break; } case VARIADIC_OP : { VariadicOp *y = (VariadicOp *)x.ptr(); convertFreeVars(y->exprs, env, ctx); break; } case AND : { And *y = (And *)x.ptr(); convertFreeVars(y->expr1, env, ctx); convertFreeVars(y->expr2, env, ctx); break; } case OR : { Or *y = (Or *)x.ptr(); convertFreeVars(y->expr1, env, ctx); convertFreeVars(y->expr2, env, ctx); break; } case LAMBDA : { Lambda *y = (Lambda *)x.ptr(); EnvPtr env2 = new Env(env); for (unsigned i = 0; i < y->formalArgs.size(); ++i) addLocal(env2, y->formalArgs[i]->name, y->formalArgs[i]->name.ptr()); if (y->formalVarArg.ptr()) addLocal(env2, y->formalVarArg->name, y->formalVarArg->name.ptr()); convertFreeVars(y->body, env2, ctx); break; } case UNPACK : { Unpack *y = (Unpack *)x.ptr(); convertFreeVars(y->expr, env, ctx); break; } case STATIC_EXPR : { StaticExpr *y = (StaticExpr *)x.ptr(); convertFreeVars(y->expr, env, ctx); break; } case DISPATCH_EXPR : { DispatchExpr *y = (DispatchExpr *)x.ptr(); convertFreeVars(y->expr, env, ctx); break; } case EVAL_EXPR : { EvalExpr *eval = (EvalExpr *)x.ptr(); convertFreeVars(eval->args, env, ctx); break; } case FOREIGN_EXPR : case OBJECT_EXPR : case FILE_EXPR : case LINE_EXPR : case COLUMN_EXPR : case ARG_EXPR : break; } }