本文整理汇总了C++中CodeGenerator::addLabelId方法的典型用法代码示例。如果您正苦于以下问题:C++ CodeGenerator::addLabelId方法的具体用法?C++ CodeGenerator::addLabelId怎么用?C++ CodeGenerator::addLabelId使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGenerator
的用法示例。
在下文中一共展示了CodeGenerator::addLabelId方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: outputCPPImpl
void SwitchStatement::outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar) {
int labelId = cg.createNewLocalId(shared_from_this());
bool staticCases = true;
if (!m_exp->getType()->isInteger()) {
staticCases = false;
} else if (m_cases) {
bool seenDefault = false;
set<int64> seenNums;
for (int i = 0; i < m_cases->getCount(); i++) {
CaseStatementPtr stmt =
dynamic_pointer_cast<CaseStatement>((*m_cases)[i]);
if (stmt->getCondition()) {
if (!stmt->isLiteralInteger() || seenDefault) {
staticCases = false;
break;
}
// detecting duplicate case value
int64 num = stmt->getLiteralInteger();
if (seenNums.find(num) != seenNums.end()) {
staticCases = false;
break;
}
seenNums.insert(num);
} else {
seenDefault = true;
}
}
}
labelId |= CodeGenerator::InsideSwitch;
if (staticCases) labelId |= CodeGenerator::StaticCases;
cg.pushBreakScope(labelId, false);
labelId &= ~CodeGenerator::BreakScopeBitMask;
cg_indentBegin("{\n");
string var;
int varId = -1;
if (m_exp->preOutputCPP(cg, ar, 0)) {
varId = cg.createNewLocalId(shared_from_this());
var = string(Option::SwitchPrefix) + lexical_cast<string>(varId);
m_exp->getType()->outputCPPDecl(cg, ar, getScope());
cg_printf(" %s;\n", var.c_str());
m_exp->outputCPPBegin(cg, ar);
cg_printf("%s = (", var.c_str());
m_exp->outputCPP(cg, ar);
cg_printf(");\n");
m_exp->outputCPPEnd(cg, ar);
}
if (staticCases) {
cg_printf("switch (");
if (!var.empty()) {
cg_printf("%s", var.c_str());
} else {
m_exp->outputCPP(cg, ar);
}
cg_printf(") {\n");
if (m_cases) m_cases->outputCPP(cg, ar);
cg_printf("}\n");
} else {
if (var.empty()) {
varId = cg.createNewLocalId(shared_from_this());
if (m_exp->hasContext(Expression::LValue) &&
m_exp->is(Expression::KindOfSimpleVariable)) {
var = getScope()->getVariables()->getVariableName(
cg, ar, static_pointer_cast<SimpleVariable>(m_exp)->getName());
} else {
var = string(Option::SwitchPrefix) + lexical_cast<string>(varId);
m_exp->getType()->outputCPPDecl(cg, ar, getScope());
cg_printf(" %s = (", var.c_str());
m_exp->outputCPP(cg, ar);
cg_printf(");\n");
}
}
if (m_cases && m_cases->getCount()) {
CaseStatementPtr defaultCase;
int defaultCaseNum = -1;
for (int i = 0; i < m_cases->getCount(); i++) {
CaseStatementPtr stmt =
dynamic_pointer_cast<CaseStatement>((*m_cases)[i]);
if (stmt->getCondition()) {
stmt->outputCPPAsIf(cg, ar, varId, var.c_str(), i);
} else {
defaultCase = stmt;
defaultCaseNum = i;
}
}
if (defaultCaseNum != -1) {
defaultCase->outputCPPAsIf(cg, ar, varId, var.c_str(), defaultCaseNum);
} else {
cg_printf("goto break%d;\n", labelId);
cg.addLabelId("break", labelId);
}
cg_printf("\n");
for (int i = 0; i < m_cases->getCount(); i++) {
//.........这里部分代码省略.........
示例2: outputCPPImpl
void BreakStatement::outputCPPImpl(CodeGenerator &cg, AnalysisResultPtr ar) {
const std::vector<int> &labelIds = cg.getBreakScopes();
if (labelIds.empty()) {
cg_printf("throw_fatal(\"bad %s\");\n", m_name);
return;
}
int64 depth = getDepth();
if (!depth) {
unsigned size = labelIds.size();
int labelId = 0;
int varId = cg.createNewLocalId(shared_from_this());
cg_printf("int64 %s%d;\n", Option::TempPrefix, varId);
m_exp->outputCPPBegin(cg, ar);
cg_printf("%s%d = (", Option::TempPrefix, varId);
m_exp->outputCPP(cg, ar);
cg_printf(");\n");
m_exp->outputCPPEnd(cg, ar);
if (size > 1) {
cg_printf("switch (");
cg_printf("%s%d", Option::TempPrefix, varId);
cg_printf(") {\n");
for (unsigned int i = 0; i < size; i++) {
labelId = labelIds[i];
labelId &= ~CodeGenerator::BreakScopeBitMask;
cg_printf("case %d: goto %s%d;\n",
int(labelIds.size() - i), m_name, labelId);
cg.addLabelId(m_name, labelId);
}
cg_printf("default:\n");
} else {
labelId = labelIds.back();
labelId &= ~CodeGenerator::BreakScopeBitMask;
cg.addLabelId(m_name, labelId);
}
cg_printf("if (");
cg_printf("%s%d", Option::TempPrefix, varId);
cg_printf("<2) {\n");
cg_printf("goto %s%d;\n", m_name, labelId);
cg_printf("} else {\n");
cg_printf("throw_fatal(\"bad %s\");\n", m_name);
cg_printf("}\n");
if (size > 1) {
cg_printf("}\n");
}
return;
}
assert(depth >= 1);
if (depth > (int64)labelIds.size()) {
cg_printf("throw_fatal(\"bad %s\");\n", m_name);
return;
}
int labelId = labelIds[labelIds.size() - depth];
if (depth > 1 || labelId & CodeGenerator::InsideSwitch) {
if (depth == 1 && labelId & CodeGenerator::StaticCases) {
cg_printf("break;\n"); // continue will turn into break as well
} else {
labelId &= ~CodeGenerator::BreakScopeBitMask;
cg_printf("goto %s%d;\n", m_name, labelId);
cg.addLabelId(m_name, labelId);
}
} else {
cg_printf("%s;\n", m_name);
}
}