aboutsummaryrefslogtreecommitdiff
path: root/lib/Frontend
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2009-04-17 18:18:49 +0000
committerDouglas Gregor <dgregor@apple.com>2009-04-17 18:18:49 +0000
commit1de05feeeafe5b215fe7617594a7076a5192a6e2 (patch)
tree1adf57ca6434d22306af1104eb07a242c797760e /lib/Frontend
parent5abd66ff444d31bd0537d8151b1abf354cfcb1af (diff)
PCH support for labels and goto.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69364 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Frontend')
-rw-r--r--lib/Frontend/PCHReader.cpp65
-rw-r--r--lib/Frontend/PCHWriter.cpp31
2 files changed, 95 insertions, 1 deletions
diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp
index 50378966e1..9597b37082 100644
--- a/lib/Frontend/PCHReader.cpp
+++ b/lib/Frontend/PCHReader.cpp
@@ -249,11 +249,13 @@ namespace {
unsigned VisitSwitchCase(SwitchCase *S);
unsigned VisitCaseStmt(CaseStmt *S);
unsigned VisitDefaultStmt(DefaultStmt *S);
+ unsigned VisitLabelStmt(LabelStmt *S);
unsigned VisitIfStmt(IfStmt *S);
unsigned VisitSwitchStmt(SwitchStmt *S);
unsigned VisitWhileStmt(WhileStmt *S);
unsigned VisitDoStmt(DoStmt *S);
unsigned VisitForStmt(ForStmt *S);
+ unsigned VisitGotoStmt(GotoStmt *S);
unsigned VisitContinueStmt(ContinueStmt *S);
unsigned VisitBreakStmt(BreakStmt *S);
unsigned VisitReturnStmt(ReturnStmt *S);
@@ -336,6 +338,15 @@ unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
return 1;
}
+unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
+ VisitStmt(S);
+ S->setID(Reader.GetIdentifierInfo(Record, Idx));
+ S->setSubStmt(StmtStack.back());
+ S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+ Reader.RecordLabelStmt(S, Record[Idx++]);
+ return 1;
+}
+
unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
VisitStmt(S);
S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
@@ -388,6 +399,14 @@ unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
return 4;
}
+unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
+ VisitStmt(S);
+ Reader.SetLabelOf(S, Record[Idx++]);
+ S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+ S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+ return 0;
+}
+
unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
VisitStmt(S);
S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
@@ -2172,6 +2191,10 @@ Stmt *PCHReader::ReadStmt() {
S = new (Context) DefaultStmt(Empty);
break;
+ case pch::STMT_LABEL:
+ S = new (Context) LabelStmt(Empty);
+ break;
+
case pch::STMT_IF:
S = new (Context) IfStmt(Empty);
break;
@@ -2192,6 +2215,10 @@ Stmt *PCHReader::ReadStmt() {
S = new (Context) ForStmt(Empty);
break;
+ case pch::STMT_GOTO:
+ S = new (Context) GotoStmt(Empty);
+ break;
+
case pch::STMT_CONTINUE:
S = new (Context) ContinueStmt(Empty);
break;
@@ -2342,7 +2369,7 @@ Stmt *PCHReader::ReadStmt() {
}
}
- assert(Idx == Record.size() && "Invalid deserialization of expression");
+ assert(Idx == Record.size() && "Invalid deserialization of statement");
StmtStack.push_back(S);
}
assert(StmtStack.size() == 1 && "Extra expressions on stack!");
@@ -2376,3 +2403,39 @@ SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
return SwitchCaseStmts[ID];
}
+
+/// \brief Record that the given label statement has been
+/// deserialized and has the given ID.
+void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
+ assert(LabelStmts.find(ID) == LabelStmts.end() &&
+ "Deserialized label twice");
+ LabelStmts[ID] = S;
+
+ // If we've already seen any goto statements that point to this
+ // label, resolve them now.
+ typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
+ std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
+ for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
+ Goto->second->setLabel(S);
+ UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
+}
+
+/// \brief Set the label of the given statement to the label
+/// identified by ID.
+///
+/// Depending on the order in which the label and other statements
+/// referencing that label occur, this operation may complete
+/// immediately (updating the statement) or it may queue the
+/// statement to be back-patched later.
+void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
+ std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
+ if (Label != LabelStmts.end()) {
+ // We've already seen this label, so set the label of the goto and
+ // we're done.
+ S->setLabel(Label->second);
+ } else {
+ // We haven't seen this label yet, so add this goto to the set of
+ // unresolved goto statements.
+ UnresolvedGotoStmts.insert(std::make_pair(ID, S));
+ }
+}
diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp
index 4b4e897166..bde859665f 100644
--- a/lib/Frontend/PCHWriter.cpp
+++ b/lib/Frontend/PCHWriter.cpp
@@ -451,11 +451,13 @@ namespace {
void VisitSwitchCase(SwitchCase *S);
void VisitCaseStmt(CaseStmt *S);
void VisitDefaultStmt(DefaultStmt *S);
+ void VisitLabelStmt(LabelStmt *S);
void VisitIfStmt(IfStmt *S);
void VisitSwitchStmt(SwitchStmt *S);
void VisitWhileStmt(WhileStmt *S);
void VisitDoStmt(DoStmt *S);
void VisitForStmt(ForStmt *S);
+ void VisitGotoStmt(GotoStmt *S);
void VisitContinueStmt(ContinueStmt *S);
void VisitBreakStmt(BreakStmt *S);
void VisitReturnStmt(ReturnStmt *S);
@@ -536,6 +538,15 @@ void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
Code = pch::STMT_DEFAULT;
}
+void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
+ VisitStmt(S);
+ Writer.AddIdentifierRef(S->getID(), Record);
+ Writer.WriteSubStmt(S->getSubStmt());
+ Writer.AddSourceLocation(S->getIdentLoc(), Record);
+ Record.push_back(Writer.GetLabelID(S));
+ Code = pch::STMT_LABEL;
+}
+
void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
VisitStmt(S);
Writer.WriteSubStmt(S->getCond());
@@ -582,6 +593,14 @@ void PCHStmtWriter::VisitForStmt(ForStmt *S) {
Code = pch::STMT_FOR;
}
+void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
+ VisitStmt(S);
+ Record.push_back(Writer.GetLabelID(S->getLabel()));
+ Writer.AddSourceLocation(S->getGotoLoc(), Record);
+ Writer.AddSourceLocation(S->getLabelLoc(), Record);
+ Code = pch::STMT_GOTO;
+}
+
void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
VisitStmt(S);
Writer.AddSourceLocation(S->getContinueLoc(), Record);
@@ -1857,3 +1876,15 @@ unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
"SwitchCase hasn't been seen yet");
return SwitchCaseIDs[S];
}
+
+/// \brief Retrieve the ID for the given label statement, which may
+/// or may not have been emitted yet.
+unsigned PCHWriter::GetLabelID(LabelStmt *S) {
+ std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
+ if (Pos != LabelIDs.end())
+ return Pos->second;
+
+ unsigned NextID = LabelIDs.size();
+ LabelIDs[S] = NextID;
+ return NextID;
+}