void GlobalStatement::outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar) { BlockScopePtr scope = getScope(); if (m_exp->getCount() > 1) cg_indentBegin("{\n"); for (int i = 0; i < m_exp->getCount(); i++) { ExpressionPtr exp = (*m_exp)[i]; if (exp->is(Expression::KindOfSimpleVariable)) { SimpleVariablePtr var = dynamic_pointer_cast<SimpleVariable>(exp); const string &name = var->getName(); VariableTablePtr variables = scope->getVariables(); if (variables->needLocalCopy(name)) { cg_printf("%s%s = ref(g->%s);\n", Option::VariablePrefix, name.c_str(), variables->getGlobalVariableName(cg, ar, name).c_str()); } } else if (exp->is(Expression::KindOfDynamicVariable)) { DynamicVariablePtr var = dynamic_pointer_cast<DynamicVariable>(exp); ExpressionPtr exp = var->getSubExpression(); exp->outputCPPBegin(cg, ar); int id = cg.createNewLocalId(shared_from_this()); cg_printf("CStrRef dgv_%d((", id); exp->outputCPP(cg, ar); cg_printf("));\n"); cg_printf("variables->get(dgv_%d) = ref(g->get(dgv_%d));\n", id, id); exp->outputCPPEnd(cg, ar); } else { assert(false); } } if (m_exp->getCount() > 1) cg_indentEnd("}\n"); }
void UnsetStatement::outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar) { if (m_exp->getCount() > 1) cg_indentBegin("{\n"); for (int i = 0; i < m_exp->getCount(); i++) { ExpressionPtr exp = (*m_exp)[i]; exp->outputCPPBegin(cg, ar); exp->outputCPPUnset(cg, ar); cg_printf(";\n"); exp->outputCPPEnd(cg, ar); } if (m_exp->getCount() > 1) cg_indentEnd("}\n"); }
void ClassConstant::outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar) { if (cg.getContext() != CodeGenerator::CppClassConstantsDecl && cg.getContext() != CodeGenerator::CppClassConstantsImpl) { return; } ClassScopePtr scope = getClassScope(); for (int i = 0; i < m_exp->getCount(); i++) { AssignmentExpressionPtr exp = dynamic_pointer_cast<AssignmentExpression>((*m_exp)[i]); ConstantExpressionPtr var = dynamic_pointer_cast<ConstantExpression>(exp->getVariable()); TypePtr type = scope->getConstants()->getFinalType(var->getName()); ExpressionPtr value = exp->getValue(); if (scope->getConstants()->isDynamic(var->getName())) { continue; } switch (cg.getContext()) { case CodeGenerator::CppClassConstantsDecl: cg_printf("extern const "); if (type->is(Type::KindOfString)) { cg_printf("StaticString"); } else { type->outputCPPDecl(cg, ar, getScope()); } cg_printf(" %s%s%s%s;\n", Option::ClassConstantPrefix, scope->getId().c_str(), Option::IdPrefix.c_str(), var->getName().c_str()); break; case CodeGenerator::CppClassConstantsImpl: { bool isString = type->is(Type::KindOfString); bool isVariant = Type::IsMappedToVariant(type); ScalarExpressionPtr scalarExp = dynamic_pointer_cast<ScalarExpression>(value); bool stringForVariant = false; if (isVariant && scalarExp && scalarExp->getActualType() && scalarExp->getActualType()->is(Type::KindOfString)) { cg_printf("static const StaticString %s%s%s%s%sv(LITSTR_INIT(%s));\n", Option::ClassConstantPrefix, scope->getId().c_str(), Option::IdPrefix.c_str(), var->getName().c_str(), Option::IdPrefix.c_str(), scalarExp->getCPPLiteralString().c_str()); stringForVariant = true; } cg_printf("const "); if (isString) { cg_printf("StaticString"); } else { type->outputCPPDecl(cg, ar, getScope()); } value->outputCPPBegin(cg, ar); cg_printf(" %s%s%s%s", Option::ClassConstantPrefix, scope->getId().c_str(), Option::IdPrefix.c_str(), var->getName().c_str()); cg_printf(isString ? "(" : " = "); if (stringForVariant) { cg_printf("%s%s%s%s%sv", Option::ClassConstantPrefix, scope->getId().c_str(), Option::IdPrefix.c_str(), var->getName().c_str(), Option::IdPrefix.c_str()); } else if (isString && scalarExp) { cg_printf("LITSTR_INIT(%s)", scalarExp->getCPPLiteralString().c_str()); } else { value->outputCPP(cg, ar); } cg_printf(isString ? ");\n" : ";\n"); value->outputCPPEnd(cg, ar); break; } default: assert(false); } } }
void ClassVariable::outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar) { // bail out early if possible switch (cg.getContext()) { case CodeGenerator::CppConstructor: case CodeGenerator::CppInitializer: if (m_modifiers->isStatic()) return; break; default: return; } ClassScopePtr scope = getClassScope(); bool derivFromRedec = scope->derivesFromRedeclaring() && !m_modifiers->isPrivate(); for (int i = 0; i < m_declaration->getCount(); i++) { ExpressionPtr exp = (*m_declaration)[i]; SimpleVariablePtr var; TypePtr type; Symbol *sym; ExpressionPtr value; bool initInCtor = false; bool initInInit = false; getCtorAndInitInfo(exp, initInCtor, initInInit, var, type, sym, value); bool isAssign = exp->is(Expression::KindOfAssignmentExpression); bool isValueNull = isAssign ? value->isLiteralNull() : false; switch (cg.getContext()) { case CodeGenerator::CppConstructor: if (initInCtor) { if (!cg.hasInitListFirstElem()) { cg.setInitListFirstElem(); } else { cg_printf(", "); } if (isAssign) { if (isValueNull) { cg_printf("%s%s(Variant::nullInit)", Option::PropertyPrefix, var->getName().c_str()); } else { ASSERT(value); ASSERT(value->is(Expression::KindOfScalarExpression)); cg_printf("%s%s(", Option::PropertyPrefix, var->getName().c_str()); value->outputCPP(cg, ar); cg_printf(")"); } } else { if (type->is(Type::KindOfVariant)) { cg_printf("%s%s(Variant::nullInit)", Option::PropertyPrefix, var->getName().c_str()); } else { const char *initializer = type->getCPPInitializer(); ASSERT(initializer); cg_printf("%s%s(%s)", Option::PropertyPrefix, var->getName().c_str(), initializer); } } } break; case CodeGenerator::CppInitializer: if (initInInit) { if (isAssign) { value->outputCPPBegin(cg, ar); if (derivFromRedec) { cg_printf("%sset(", Option::ObjectPrefix); cg_printString(var->getName(), ar, shared_from_this()); cg_printf(", "); value->outputCPP(cg, ar); cg_printf(")"); } else if (isValueNull) { cg_printf("setNull(%s%s)", Option::PropertyPrefix, var->getName().c_str()); } else { cg_printf("%s%s = ", Option::PropertyPrefix, var->getName().c_str()); value->outputCPP(cg, ar); } cg_printf(";\n"); value->outputCPPEnd(cg, ar); } else { if (derivFromRedec) { cg_printf("%sset(", Option::ObjectPrefix); cg_printString(var->getName(), ar, shared_from_this()); cg_printf(", null_variant);\n"); } else { if (type->is(Type::KindOfVariant)) { cg_printf("setNull(%s%s);\n", Option::PropertyPrefix, var->getName().c_str()); } else { const char *initializer = type->getCPPInitializer(); ASSERT(initializer); cg_printf("%s%s = %s;\n", Option::PropertyPrefix, var->getName().c_str(), initializer); } } } } break; default: break; } } }
void ClassVariable::outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar) { ClassScopePtr scope = ar->getClassScope(); bool derivFromRedec = scope->derivesFromRedeclaring() && !m_modifiers->isPrivate(); for (int i = 0; i < m_declaration->getCount(); i++) { ExpressionPtr exp = (*m_declaration)[i]; SimpleVariablePtr var; TypePtr type; switch (cg.getContext()) { case CodeGenerator::CppConstructor: if (m_modifiers->isStatic()) continue; if (exp->is(Expression::KindOfAssignmentExpression)) { AssignmentExpressionPtr assignment = dynamic_pointer_cast<AssignmentExpression>(exp); var = dynamic_pointer_cast<SimpleVariable>(assignment->getVariable()); ExpressionPtr value = assignment->getValue(); value->outputCPPBegin(cg, ar); if (derivFromRedec) { cg_printf("%sset(\"%s\",-1, ", Option::ObjectPrefix, var->getName().c_str()); value->outputCPP(cg, ar); cg_printf(")"); } else { cg_printf("%s%s = ", Option::PropertyPrefix, var->getName().c_str()); value->outputCPP(cg, ar); } cg_printf(";\n"); value->outputCPPEnd(cg, ar); } else { var = dynamic_pointer_cast<SimpleVariable>(exp); if (derivFromRedec) { cg_printf("%sset(\"%s\",-1, null);\n", Option::ObjectPrefix, var->getName().c_str()); } else { type = scope->getVariables()->getFinalType(var->getName()); const char *initializer = type->getCPPInitializer(); if (initializer) { cg_printf("%s%s = %s;\n", Option::PropertyPrefix, var->getName().c_str(), initializer); } } } break; case CodeGenerator::CppStaticInitializer: { if (!m_modifiers->isStatic()) continue; VariableTablePtr variables = scope->getVariables(); if (exp->is(Expression::KindOfAssignmentExpression)) { AssignmentExpressionPtr assignment = dynamic_pointer_cast<AssignmentExpression>(exp); var = dynamic_pointer_cast<SimpleVariable> (assignment->getVariable()); ExpressionPtr value = assignment->getValue(); if (value->containsDynamicConstant(ar)) continue; cg_printf("g->%s%s%s%s = ", Option::StaticPropertyPrefix, scope->getId(cg).c_str(), Option::IdPrefix.c_str(), var->getName().c_str()); value->outputCPP(cg, ar); } else { var = dynamic_pointer_cast<SimpleVariable>(exp); type = scope->getVariables()->getFinalType(var->getName()); const char *initializer = type->getCPPInitializer(); if (initializer) { cg_printf("g->%s%s%s%s = %s", Option::StaticPropertyPrefix, scope->getId(cg).c_str(), Option::IdPrefix.c_str(), var->getName().c_str(), initializer); } } cg_printf(";\n"); } break; case CodeGenerator::CppLazyStaticInitializer: { if (!m_modifiers->isStatic()) continue; if (!exp->is(Expression::KindOfAssignmentExpression)) continue; VariableTablePtr variables = scope->getVariables(); AssignmentExpressionPtr assignment = dynamic_pointer_cast<AssignmentExpression>(exp); var = dynamic_pointer_cast<SimpleVariable>(assignment->getVariable()); ExpressionPtr value = assignment->getValue(); if (!value->containsDynamicConstant(ar)) continue; value->outputCPPBegin(cg, ar); cg_printf("g->%s%s%s%s = ", Option::StaticPropertyPrefix, scope->getId(cg).c_str(), Option::IdPrefix.c_str(), var->getName().c_str()); value->outputCPP(cg, ar); cg_printf(";\n"); value->outputCPPEnd(cg, ar); } break; default: break; } } }
void ClassConstant::outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar) { bool lazyInit = cg.getContext() == CodeGenerator::CppLazyStaticInitializer; if (cg.getContext() != CodeGenerator::CppClassConstantsDecl && cg.getContext() != CodeGenerator::CppClassConstantsImpl && !lazyInit) { return; } ClassScopePtr scope = getClassScope(); for (int i = 0; i < m_exp->getCount(); i++) { AssignmentExpressionPtr exp = dynamic_pointer_cast<AssignmentExpression>((*m_exp)[i]); ConstantExpressionPtr var = dynamic_pointer_cast<ConstantExpression>(exp->getVariable()); TypePtr type = scope->getConstants()->getFinalType(var->getName()); ExpressionPtr value = exp->getValue(); if (!scope->getConstants()->isDynamic(var->getName()) == lazyInit) { continue; } switch (cg.getContext()) { case CodeGenerator::CppClassConstantsDecl: cg_printf("extern const "); if (type->is(Type::KindOfString)) { cg_printf("StaticString"); } else { type->outputCPPDecl(cg, ar, getScope()); } cg_printf(" %s%s_%s;\n", Option::ClassConstantPrefix, scope->getId(cg).c_str(), var->getName().c_str()); break; case CodeGenerator::CppClassConstantsImpl: { cg_printf("const "); bool isString = type->is(Type::KindOfString); if (isString) { cg_printf("StaticString"); } else { type->outputCPPDecl(cg, ar, getScope()); } value->outputCPPBegin(cg, ar); cg_printf(" %s%s_%s", Option::ClassConstantPrefix, scope->getId(cg).c_str(), var->getName().c_str()); cg_printf(isString ? "(" : " = "); ScalarExpressionPtr scalarExp = dynamic_pointer_cast<ScalarExpression>(value); if (isString && scalarExp) { cg_printf("LITSTR_INIT(%s)", scalarExp->getCPPLiteralString(cg).c_str()); } else { value->outputCPP(cg, ar); } cg_printf(isString ? ");\n" : ";\n"); value->outputCPPEnd(cg, ar); break; } case CodeGenerator::CppLazyStaticInitializer: value->outputCPPBegin(cg, ar); cg_printf("g->%s%s_%s = ", Option::ClassConstantPrefix, scope->getId(cg).c_str(), var->getName().c_str()); value->outputCPP(cg, ar); cg_printf(";\n"); value->outputCPPEnd(cg, ar); break; default: ASSERT(false); } } }