summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/Frontend/PCHReader.cpp144
-rw-r--r--clang/lib/Frontend/PCHWriter.cpp102
2 files changed, 229 insertions, 17 deletions
diff --git a/clang/lib/Frontend/PCHReader.cpp b/clang/lib/Frontend/PCHReader.cpp
index f5f6f5d41c9..909ecdb100c 100644
--- a/clang/lib/Frontend/PCHReader.cpp
+++ b/clang/lib/Frontend/PCHReader.cpp
@@ -16,6 +16,8 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclGroup.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/StmtVisitor.h"
#include "clang/AST/Type.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
@@ -127,7 +129,8 @@ void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
VisitValueDecl(ECD);
- // FIXME: read the initialization expression
+ if (Record[Idx++])
+ ECD->setInitExpr(Reader.ReadExpr());
ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
}
@@ -155,7 +158,8 @@ void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
VisitValueDecl(FD);
FD->setMutable(Record[Idx++]);
- // FIXME: Read the bit width.
+ if (Record[Idx++])
+ FD->setBitWidth(Reader.ReadExpr());
}
void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
@@ -167,6 +171,8 @@ void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
VD->setPreviousDeclaration(
cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+ if (Record[Idx++])
+ VD->setInit(Reader.ReadExpr());
}
void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
@@ -204,6 +210,53 @@ PCHDeclReader::VisitDeclContext(DeclContext *DC) {
return std::make_pair(LexicalOffset, VisibleOffset);
}
+//===----------------------------------------------------------------------===//
+// Statement/expression deserialization
+//===----------------------------------------------------------------------===//
+namespace {
+ class VISIBILITY_HIDDEN PCHStmtReader
+ : public StmtVisitor<PCHStmtReader, void> {
+ PCHReader &Reader;
+ const PCHReader::RecordData &Record;
+ unsigned &Idx;
+
+ public:
+ PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
+ unsigned &Idx)
+ : Reader(Reader), Record(Record), Idx(Idx) { }
+
+ void VisitExpr(Expr *E);
+ void VisitDeclRefExpr(DeclRefExpr *E);
+ void VisitIntegerLiteral(IntegerLiteral *E);
+ void VisitCharacterLiteral(CharacterLiteral *E);
+ };
+}
+
+void PCHStmtReader::VisitExpr(Expr *E) {
+ E->setType(Reader.GetType(Record[Idx++]));
+ E->setTypeDependent(Record[Idx++]);
+ E->setValueDependent(Record[Idx++]);
+}
+
+void PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
+ VisitExpr(E);
+ E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
+ E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
+}
+
+void PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
+ VisitExpr(E);
+ E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
+ E->setValue(Reader.ReadAPInt(Record, Idx));
+}
+
+void PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
+ VisitExpr(E);
+ E->setValue(Record[Idx++]);
+ E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
+ E->setWide(Record[Idx++]);
+}
+
// FIXME: use the diagnostics machinery
static bool Error(const char *Str) {
std::fprintf(stderr, "%s\n", Str);
@@ -755,6 +808,26 @@ PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
return Success;
}
+namespace {
+ /// \brief Helper class that saves the current stream position and
+ /// then restores it when destroyed.
+ struct VISIBILITY_HIDDEN SavedStreamPosition {
+ explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
+ : Stream(Stream), Offset(Stream.GetCurrentBitNo()),
+ EndOfStream(Stream.AtEndOfStream()){ }
+
+ ~SavedStreamPosition() {
+ if (!EndOfStream)
+ Stream.JumpToBit(Offset);
+ }
+
+ private:
+ llvm::BitstreamReader &Stream;
+ uint64_t Offset;
+ bool EndOfStream;
+ };
+}
+
/// \brief Parse the record that corresponds to a LangOptions data
/// structure.
///
@@ -850,6 +923,10 @@ bool PCHReader::ParseLanguageOptions(
/// at the given offset in the bitstream. It is a helper routine for
/// GetType, which deals with reading type IDs.
QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
+ // Keep track of where we are in the stream, then jump back there
+ // after reading this type.
+ SavedStreamPosition SavedPosition(Stream);
+
Stream.JumpToBit(Offset);
RecordData Record;
unsigned Code = Stream.ReadCode();
@@ -918,9 +995,11 @@ QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
}
case pch::TYPE_VARIABLE_ARRAY: {
- // FIXME: implement this
- assert(false && "Unable to de-serialize variable-length array type");
- return QualType();
+ QualType ElementType = GetType(Record[0]);
+ ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
+ unsigned IndexTypeQuals = Record[2];
+ return Context.getVariableArrayType(ElementType, ReadExpr(),
+ ASM, IndexTypeQuals);
}
case pch::TYPE_VECTOR: {
@@ -972,9 +1051,7 @@ QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
case pch::TYPE_TYPEOF_EXPR:
- // FIXME: Deserialize TypeOfExprType
- assert(false && "Cannot de-serialize typeof(expr) from a PCH file");
- return QualType();
+ return Context.getTypeOfExprType(ReadExpr());
case pch::TYPE_TYPEOF: {
if (Record.size() != 1) {
@@ -1032,12 +1109,17 @@ inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
/// \brief Read the declaration at the given offset from the PCH file.
Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
+ // Keep track of where we are in the stream, then jump back there
+ // after reading this declaration.
+ SavedStreamPosition SavedPosition(Stream);
+
Decl *D = 0;
Stream.JumpToBit(Offset);
RecordData Record;
unsigned Code = Stream.ReadCode();
unsigned Idx = 0;
PCHDeclReader Reader(*this, Record, Idx);
+
switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
case pch::DECL_TRANSLATION_UNIT:
assert(Index == 0 && "Translation unit must be at index 0");
@@ -1237,6 +1319,10 @@ bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
uint64_t Offset = DeclContextOffsets[DC].first;
assert(Offset && "DeclContext has no lexical decls in storage");
+ // Keep track of where we are in the stream, then jump back there
+ // after reading this context.
+ SavedStreamPosition SavedPosition(Stream);
+
// Load the record containing all of the declarations lexically in
// this context.
Stream.JumpToBit(Offset);
@@ -1258,6 +1344,10 @@ bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
uint64_t Offset = DeclContextOffsets[DC].second;
assert(Offset && "DeclContext has no visible decls in storage");
+ // Keep track of where we are in the stream, then jump back there
+ // after reading this context.
+ SavedStreamPosition SavedPosition(Stream);
+
// Load the record containing all of the declarations visible in
// this context.
Stream.JumpToBit(Offset);
@@ -1394,6 +1484,44 @@ llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
}
+Expr *PCHReader::ReadExpr() {
+ RecordData Record;
+ unsigned Code = Stream.ReadCode();
+ unsigned Idx = 0;
+ PCHStmtReader Reader(*this, Record, Idx);
+ Stmt::EmptyShell Empty;
+
+ Expr *E = 0;
+ switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
+ case pch::EXPR_NULL:
+ E = 0;
+ break;
+
+ case pch::EXPR_DECL_REF:
+ E = new (Context) DeclRefExpr(Empty);
+ break;
+
+ case pch::EXPR_INTEGER_LITERAL:
+ E = new (Context) IntegerLiteral(Empty);
+ break;
+
+ case pch::EXPR_CHARACTER_LITERAL:
+ E = new (Context) CharacterLiteral(Empty);
+ break;
+
+ default:
+ assert(false && "Unhandled expression kind");
+ break;
+ }
+
+ if (E)
+ Reader.Visit(E);
+
+ assert(Idx == Record.size() && "Invalid deserialization of expression");
+
+ return E;
+}
+
DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
return Diag(SourceLocation(), DiagID);
}
diff --git a/clang/lib/Frontend/PCHWriter.cpp b/clang/lib/Frontend/PCHWriter.cpp
index c7bfa0b74e9..1cf98cea9ca 100644
--- a/clang/lib/Frontend/PCHWriter.cpp
+++ b/clang/lib/Frontend/PCHWriter.cpp
@@ -16,6 +16,8 @@
#include "clang/AST/Decl.h"
#include "clang/AST/DeclContextInternals.h"
#include "clang/AST/DeclVisitor.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/StmtVisitor.h"
#include "clang/AST/Type.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
@@ -122,8 +124,7 @@ void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
VisitArrayType(T);
- // FIXME: Serialize array size expression.
- assert(false && "Cannot serialize variable-length arrays");
+ Writer.AddExpr(T->getSizeExpr());
Code = pch::TYPE_VARIABLE_ARRAY;
}
@@ -163,8 +164,7 @@ void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
}
void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
- // FIXME: serialize the typeof expression
- assert(false && "Cannot serialize typeof(expr)");
+ Writer.AddExpr(T->getUnderlyingExpr());
Code = pch::TYPE_TYPEOF_EXPR;
}
@@ -327,7 +327,9 @@ void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
VisitValueDecl(D);
- // FIXME: Writer.AddExprRef(D->getInitExpr());
+ Record.push_back(D->getInitExpr()? 1 : 0);
+ if (D->getInitExpr())
+ Writer.AddExpr(D->getInitExpr());
Writer.AddAPSInt(D->getInitVal(), Record);
Code = pch::DECL_ENUM_CONSTANT;
}
@@ -354,7 +356,9 @@ void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
VisitValueDecl(D);
Record.push_back(D->isMutable());
- // FIXME: Writer.AddExprRef(D->getBitWidth());
+ Record.push_back(D->getBitWidth()? 1 : 0);
+ if (D->getBitWidth())
+ Writer.AddExpr(D->getBitWidth());
Code = pch::DECL_FIELD;
}
@@ -366,7 +370,9 @@ void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Record.push_back(D->isDeclaredInCondition());
Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
- // FIXME: emit initializer
+ Record.push_back(D->getInit()? 1 : 0);
+ if (D->getInit())
+ Writer.AddExpr(D->getInit());
Code = pch::DECL_VAR;
}
@@ -420,6 +426,57 @@ void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
}
//===----------------------------------------------------------------------===//
+// Statement/expression serialization
+//===----------------------------------------------------------------------===//
+namespace {
+ class VISIBILITY_HIDDEN PCHStmtWriter
+ : public StmtVisitor<PCHStmtWriter, void> {
+
+ PCHWriter &Writer;
+ PCHWriter::RecordData &Record;
+
+ public:
+ pch::StmtCode Code;
+
+ PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
+ : Writer(Writer), Record(Record) { }
+
+ void VisitExpr(Expr *E);
+ void VisitDeclRefExpr(DeclRefExpr *E);
+ void VisitIntegerLiteral(IntegerLiteral *E);
+ void VisitCharacterLiteral(CharacterLiteral *E);
+ };
+}
+
+void PCHStmtWriter::VisitExpr(Expr *E) {
+ Writer.AddTypeRef(E->getType(), Record);
+ Record.push_back(E->isTypeDependent());
+ Record.push_back(E->isValueDependent());
+}
+
+void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
+ VisitExpr(E);
+ Writer.AddDeclRef(E->getDecl(), Record);
+ Writer.AddSourceLocation(E->getLocation(), Record);
+ Code = pch::EXPR_DECL_REF;
+}
+
+void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
+ VisitExpr(E);
+ Writer.AddSourceLocation(E->getLocation(), Record);
+ Writer.AddAPInt(E->getValue(), Record);
+ Code = pch::EXPR_INTEGER_LITERAL;
+}
+
+void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
+ VisitExpr(E);
+ Record.push_back(E->getValue());
+ Writer.AddSourceLocation(E->getLoc(), Record);
+ Record.push_back(E->isWide());
+ Code = pch::EXPR_CHARACTER_LITERAL;
+}
+
+//===----------------------------------------------------------------------===//
// PCHWriter Implementation
//===----------------------------------------------------------------------===//
@@ -513,8 +570,6 @@ static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &S) {
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
- // FIXME: Need an actual encoding for the line directives; maybe
- // this should be an array?
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
return S.EmitAbbrev(Abbrev);
}
@@ -805,6 +860,9 @@ void PCHWriter::WriteType(const Type *T) {
// Emit the serialized record.
S.EmitRecord(W.Code, Record);
+
+ // Flush any expressions that were written as part of this type.
+ FlushExprs();
}
/// \brief Write a block containing all of the types.
@@ -937,6 +995,9 @@ void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
assert(W.Code && "Unhandled declaration kind while generating PCH");
S.EmitRecord(W.Code, Record);
+ // Flush any expressions that were written as part of this declaration.
+ FlushExprs();
+
// Note external declarations so that we can add them to a record
// in the PCH file later.
if (isa<FileScopeAsmDecl>(D))
@@ -1160,3 +1221,26 @@ void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
break;
}
}
+
+/// \brief Flush all of the expressions that have been added to the
+/// queue via AddExpr().
+void PCHWriter::FlushExprs() {
+ RecordData Record;
+ PCHStmtWriter Writer(*this, Record);
+ while (!ExprsToEmit.empty()) {
+ Expr *E = ExprsToEmit.front();
+ ExprsToEmit.pop();
+
+ Record.clear();
+ if (!E) {
+ S.EmitRecord(pch::EXPR_NULL, Record);
+ continue;
+ }
+
+ Writer.Code = pch::EXPR_NULL;
+ Writer.Visit(E);
+ assert(Writer.Code != pch::EXPR_NULL &&
+ "Unhandled expression writing PCH file");
+ S.EmitRecord(Writer.Code, Record);
+ }
+}
OpenPOWER on IntegriCloud