Ted Kremenek | 2f1f8cb | 2007-10-25 21:37:16 +0000 | [diff] [blame] | 1 | //===--- DeclSerialization.cpp - Serialization of Decls ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Ted Kremenek and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This files defines methods that implement bitcode serialization for Decls. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
| 15 | #include "clang/AST/Expr.h" |
| 16 | #include "llvm/Bitcode/Serialize.h" |
| 17 | #include "llvm/Bitcode/Deserialize.h" |
| 18 | |
Ted Kremenek | 2f1f8cb | 2007-10-25 21:37:16 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 21 | void Decl::Emit(llvm::Serializer& S) const { |
Ted Kremenek | 8af8fe3 | 2007-11-05 21:38:00 +0000 | [diff] [blame] | 22 | S.EmitInt(getKind()); |
| 23 | |
| 24 | switch (getKind()) { |
| 25 | default: |
| 26 | assert (false && "Not implemented."); |
| 27 | break; |
| 28 | |
| 29 | case BlockVar: |
| 30 | cast<BlockVarDecl>(this)->Emit(S); |
| 31 | break; |
| 32 | |
| 33 | case FileVar: |
| 34 | cast<FileVarDecl>(this)->Emit(S); |
| 35 | break; |
| 36 | |
| 37 | case ParmVar: |
| 38 | cast<ParmVarDecl>(this)->Emit(S); |
| 39 | break; |
| 40 | |
| 41 | case Function: |
| 42 | cast<FunctionDecl>(this)->Emit(S); |
| 43 | break; |
Ted Kremenek | f7bf411 | 2007-11-05 21:49:34 +0000 | [diff] [blame] | 44 | |
| 45 | case Typedef: |
| 46 | cast<TypedefDecl>(this)->Emit(S); |
| 47 | break; |
Ted Kremenek | 8af8fe3 | 2007-11-05 21:38:00 +0000 | [diff] [blame] | 48 | } |
Ted Kremenek | 2f1f8cb | 2007-10-25 21:37:16 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Ted Kremenek | beb7713 | 2007-11-01 22:25:41 +0000 | [diff] [blame] | 51 | Decl* Decl::Materialize(llvm::Deserializer& D) { |
Ted Kremenek | 2ebc89f | 2007-11-06 19:51:47 +0000 | [diff] [blame^] | 52 | Kind k = static_cast<Kind>(D.ReadInt()); |
| 53 | |
| 54 | switch (k) { |
| 55 | default: |
| 56 | assert (false && "Not implemented."); |
| 57 | break; |
| 58 | |
| 59 | case BlockVar: |
| 60 | return BlockVarDecl::Materialize(D); |
| 61 | |
| 62 | case FileVar: |
| 63 | return FileVarDecl::Materialize(D); |
| 64 | |
| 65 | case ParmVar: |
| 66 | return ParmVarDecl::Materialize(D); |
| 67 | |
| 68 | case Function: |
| 69 | return FunctionDecl::Materialize(D); |
| 70 | |
| 71 | case Typedef: |
| 72 | return TypedefDecl::Materialize(D); |
| 73 | } |
Ted Kremenek | 2f1f8cb | 2007-10-25 21:37:16 +0000 | [diff] [blame] | 74 | } |
Ted Kremenek | 0497331 | 2007-11-02 18:05:11 +0000 | [diff] [blame] | 75 | |
| 76 | void NamedDecl::InternalEmit(llvm::Serializer& S) const { |
| 77 | S.EmitPtr(Identifier); |
| 78 | } |
| 79 | |
| 80 | void NamedDecl::InternalRead(llvm::Deserializer& D) { |
| 81 | D.ReadPtr(Identifier); |
| 82 | } |
| 83 | |
| 84 | void ScopedDecl::InternalEmit(llvm::Serializer& S) const { |
| 85 | NamedDecl::InternalEmit(S); |
| 86 | S.EmitPtr(Next); |
| 87 | S.EmitOwnedPtr<Decl>(NextDeclarator); |
| 88 | } |
| 89 | |
| 90 | void ScopedDecl::InternalRead(llvm::Deserializer& D) { |
| 91 | NamedDecl::InternalRead(D); |
| 92 | D.ReadPtr(Next); |
Ted Kremenek | 2ebc89f | 2007-11-06 19:51:47 +0000 | [diff] [blame^] | 93 | NextDeclarator = cast_or_null<ScopedDecl>(D.ReadOwnedPtr<Decl>()); |
Ted Kremenek | 0497331 | 2007-11-02 18:05:11 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void ValueDecl::InternalEmit(llvm::Serializer& S) const { |
| 97 | S.Emit(DeclType); |
| 98 | ScopedDecl::InternalEmit(S); |
| 99 | } |
| 100 | |
| 101 | void ValueDecl::InternalRead(llvm::Deserializer& D) { |
| 102 | D.Read(DeclType); |
| 103 | ScopedDecl::InternalRead(D); |
| 104 | } |
| 105 | |
| 106 | void VarDecl::InternalEmit(llvm::Serializer& S) const { |
| 107 | S.EmitInt(SClass); |
| 108 | S.EmitInt(objcDeclQualifier); |
Ted Kremenek | f7bf411 | 2007-11-05 21:49:34 +0000 | [diff] [blame] | 109 | ValueDecl::InternalEmit(S); |
Ted Kremenek | 0497331 | 2007-11-02 18:05:11 +0000 | [diff] [blame] | 110 | S.EmitOwnedPtr(Init); |
| 111 | } |
| 112 | |
| 113 | void VarDecl::InternalRead(llvm::Deserializer& D) { |
| 114 | SClass = D.ReadInt(); |
| 115 | objcDeclQualifier = static_cast<ObjcDeclQualifier>(D.ReadInt()); |
Ted Kremenek | 2ebc89f | 2007-11-06 19:51:47 +0000 | [diff] [blame^] | 116 | ValueDecl::InternalRead(D); |
Ted Kremenek | 0497331 | 2007-11-02 18:05:11 +0000 | [diff] [blame] | 117 | D.ReadOwnedPtr(Init); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void BlockVarDecl::Emit(llvm::Serializer& S) const { |
| 122 | S.Emit(getLocation()); |
| 123 | VarDecl::InternalEmit(S); |
| 124 | } |
| 125 | |
| 126 | BlockVarDecl* BlockVarDecl::Materialize(llvm::Deserializer& D) { |
| 127 | SourceLocation L = SourceLocation::ReadVal(D); |
| 128 | BlockVarDecl* decl = new BlockVarDecl(L,NULL,QualType(),None,NULL); |
| 129 | decl->VarDecl::InternalRead(D); |
| 130 | return decl; |
| 131 | } |
| 132 | |
| 133 | void FileVarDecl::Emit(llvm::Serializer& S) const { |
| 134 | S.Emit(getLocation()); |
| 135 | VarDecl::InternalEmit(S); |
| 136 | } |
| 137 | |
| 138 | FileVarDecl* FileVarDecl::Materialize(llvm::Deserializer& D) { |
| 139 | SourceLocation L = SourceLocation::ReadVal(D); |
| 140 | FileVarDecl* decl = new FileVarDecl(L,NULL,QualType(),None,NULL); |
| 141 | decl->VarDecl::InternalRead(D); |
| 142 | return decl; |
| 143 | } |
| 144 | |
| 145 | void ParmVarDecl::Emit(llvm::Serializer& S) const { |
| 146 | S.Emit(getLocation()); |
| 147 | VarDecl::InternalEmit(S); |
| 148 | } |
| 149 | |
| 150 | ParmVarDecl* ParmVarDecl::Materialize(llvm::Deserializer& D) { |
| 151 | SourceLocation L = SourceLocation::ReadVal(D); |
| 152 | ParmVarDecl* decl = new ParmVarDecl(L,NULL,QualType(),None,NULL); |
| 153 | decl->VarDecl::InternalRead(D); |
| 154 | return decl; |
| 155 | } |
| 156 | |
| 157 | void FunctionDecl::Emit(llvm::Serializer& S) const { |
| 158 | S.Emit(getLocation()); |
| 159 | S.EmitInt(SClass); |
| 160 | S.EmitBool(IsInline); |
| 161 | |
| 162 | ValueDecl::InternalEmit(S); |
| 163 | |
| 164 | unsigned NumParams = getNumParams(); |
| 165 | S.EmitInt(NumParams); |
| 166 | |
| 167 | for (unsigned i = 0 ; i < NumParams; ++i) |
| 168 | S.EmitOwnedPtr(ParamInfo[i]); |
| 169 | |
| 170 | S.EmitOwnedPtr(Body); |
| 171 | } |
| 172 | |
| 173 | FunctionDecl* FunctionDecl::Materialize(llvm::Deserializer& D) { |
| 174 | SourceLocation L = SourceLocation::ReadVal(D); |
| 175 | StorageClass SClass = static_cast<StorageClass>(D.ReadInt()); |
| 176 | bool IsInline = D.ReadBool(); |
| 177 | |
| 178 | FunctionDecl* decl = new FunctionDecl(L,NULL,QualType(),SClass,IsInline); |
| 179 | |
| 180 | decl->ValueDecl::InternalRead(D); |
| 181 | |
| 182 | unsigned NumParams = D.ReadInt(); |
| 183 | decl->ParamInfo = NumParams ? new ParmVarDecl*[NumParams] : NULL; |
| 184 | |
| 185 | for (unsigned i = 0 ; i < NumParams; ++i) |
| 186 | D.ReadOwnedPtr(decl->ParamInfo[i]); |
| 187 | |
| 188 | D.ReadOwnedPtr(decl->Body); |
| 189 | |
| 190 | return decl; |
| 191 | } |
Ted Kremenek | f7bf411 | 2007-11-05 21:49:34 +0000 | [diff] [blame] | 192 | |
| 193 | void TypedefDecl::Emit(llvm::Serializer& S) const { |
| 194 | S.Emit(getLocation()); |
Ted Kremenek | 2ebc89f | 2007-11-06 19:51:47 +0000 | [diff] [blame^] | 195 | S.Emit(UnderlyingType); |
Ted Kremenek | f7bf411 | 2007-11-05 21:49:34 +0000 | [diff] [blame] | 196 | InternalEmit(S); |
Ted Kremenek | f7bf411 | 2007-11-05 21:49:34 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | TypedefDecl* TypedefDecl::Materialize(llvm::Deserializer& D) { |
| 200 | SourceLocation L = SourceLocation::ReadVal(D); |
| 201 | TypedefDecl* decl = new TypedefDecl(L,NULL,QualType(),NULL); |
Ted Kremenek | f7bf411 | 2007-11-05 21:49:34 +0000 | [diff] [blame] | 202 | D.Read(decl->UnderlyingType); |
Ted Kremenek | 2ebc89f | 2007-11-06 19:51:47 +0000 | [diff] [blame^] | 203 | decl->InternalRead(D); |
Ted Kremenek | f7bf411 | 2007-11-05 21:49:34 +0000 | [diff] [blame] | 204 | return decl; |
| 205 | } |