1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
//===--- StmtSerialization.cpp - Serialization of Statements --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Ted Kremenek and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the type-specific methods for serializing statements
// and expressions.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/Expr.h"
#include "llvm/Bitcode/Serialize.h"
#include "llvm/Bitcode/Deserialize.h"
using namespace clang;
using llvm::Serializer;
using llvm::Deserializer;
void Stmt::Emit(Serializer& S) const {
S.FlushRecord();
S.EmitInt(getStmtClass());
directEmit(S);
S.FlushRecord();
}
Stmt* Stmt::Materialize(Deserializer& D) {
StmtClass SC = static_cast<StmtClass>(D.ReadInt());
switch (SC) {
default:
assert (false && "Not implemented.");
return NULL;
case ArraySubscriptExprClass:
return ArraySubscriptExpr::directMaterialize(D);
case BinaryOperatorClass:
return BinaryOperator::directMaterialize(D);
case BreakStmtClass:
return BreakStmt::directMaterialize(D);
case CallExprClass:
return CallExpr::directMaterialize(D);
case CaseStmtClass:
return CaseStmt::directMaterialize(D);
case CastExprClass:
return CastExpr::directMaterialize(D);
case CharacterLiteralClass:
return CharacterLiteral::directMaterialize(D);
case CompoundAssignOperatorClass:
return CompoundAssignOperator::directMaterialize(D);
case CompoundStmtClass:
return CompoundStmt::directMaterialize(D);
case ContinueStmtClass:
return ContinueStmt::directMaterialize(D);
case DeclRefExprClass:
return DeclRefExpr::directMaterialize(D);
case DeclStmtClass:
return DeclStmt::directMaterialize(D);
case DefaultStmtClass:
return DefaultStmt::directMaterialize(D);
case DoStmtClass:
return DoStmt::directMaterialize(D);
case FloatingLiteralClass:
return FloatingLiteral::directMaterialize(D);
case ForStmtClass:
return ForStmt::directMaterialize(D);
case GotoStmtClass:
return GotoStmt::directMaterialize(D);
case IfStmtClass:
return IfStmt::directMaterialize(D);
case ImaginaryLiteralClass:
return ImaginaryLiteral::directMaterialize(D);
case ImplicitCastExprClass:
return ImplicitCastExpr::directMaterialize(D);
case IndirectGotoStmtClass:
return IndirectGotoStmt::directMaterialize(D);
case IntegerLiteralClass:
return IntegerLiteral::directMaterialize(D);
case LabelStmtClass:
return LabelStmt::directMaterialize(D);
case NullStmtClass:
return NullStmt::directMaterialize(D);
case ParenExprClass:
return ParenExpr::directMaterialize(D);
case PreDefinedExprClass:
return PreDefinedExpr::directMaterialize(D);
case ReturnStmtClass:
return ReturnStmt::directMaterialize(D);
case StringLiteralClass:
return StringLiteral::directMaterialize(D);
case SwitchStmtClass:
return SwitchStmt::directMaterialize(D);
case UnaryOperatorClass:
return UnaryOperator::directMaterialize(D);
case WhileStmtClass:
return WhileStmt::directMaterialize(D);
}
}
void ArraySubscriptExpr::directEmit(Serializer& S) const {
S.Emit(getType());
S.Emit(RBracketLoc);
S.BatchEmitOwnedPtrs(getLHS(),getRHS());
}
ArraySubscriptExpr* ArraySubscriptExpr::directMaterialize(Deserializer& D) {
QualType t = QualType::ReadVal(D);
SourceLocation L = SourceLocation::ReadVal(D);
Expr *LHS, *RHS;
D.BatchReadOwnedPtrs(LHS,RHS);
return new ArraySubscriptExpr(LHS,RHS,t,L);
}
void BinaryOperator::directEmit(Serializer& S) const {
S.EmitInt(Opc);
S.Emit(OpLoc);;
S.Emit(getType());
S.BatchEmitOwnedPtrs(getLHS(),getRHS());
}
BinaryOperator* BinaryOperator::directMaterialize(Deserializer& D) {
Opcode Opc = static_cast<Opcode>(D.ReadInt());
SourceLocation OpLoc = SourceLocation::ReadVal(D);
QualType Result = QualType::ReadVal(D);
Expr *LHS, *RHS;
D.BatchReadOwnedPtrs(LHS,RHS);
return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
}
void BreakStmt::directEmit(Serializer& S) const {
S.Emit(BreakLoc);
}
BreakStmt* BreakStmt::directMaterialize(Deserializer& D) {
SourceLocation Loc = SourceLocation::ReadVal(D);
return new BreakStmt(Loc);
}
void CallExpr::directEmit(Serializer& S) const {
S.Emit(getType());
S.Emit(RParenLoc);
S.EmitInt(NumArgs);
S.BatchEmitOwnedPtrs(NumArgs+1,SubExprs);
}
CallExpr* CallExpr::directMaterialize(Deserializer& D) {
QualType t = QualType::ReadVal(D);
SourceLocation L = SourceLocation::ReadVal(D);
unsigned NumArgs = D.ReadInt();
Expr** SubExprs = new Expr*[NumArgs+1];
D.BatchReadOwnedPtrs(NumArgs+1,SubExprs);
return new CallExpr(SubExprs,NumArgs,t,L);
}
void CaseStmt::directEmit(Serializer& S) const {
S.Emit(CaseLoc);
S.EmitPtr(getNextSwitchCase());
S.BatchEmitOwnedPtrs(getLHS(),getRHS(),getSubStmt());
}
CaseStmt* CaseStmt::directMaterialize(Deserializer& D) {
SourceLocation CaseLoc = SourceLocation::ReadVal(D);
Expr *LHS, *RHS;
Stmt* SubStmt;
D.BatchReadOwnedPtrs(LHS,RHS,SubStmt);
CaseStmt* stmt = new CaseStmt(LHS,RHS,SubStmt,CaseLoc);
stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
return stmt;
}
void CastExpr::directEmit(Serializer& S) const {
S.Emit(getType());
S.Emit(Loc);
S.EmitOwnedPtr(Op);
}
CastExpr* CastExpr::directMaterialize(Deserializer& D) {
QualType t = QualType::ReadVal(D);
SourceLocation Loc = SourceLocation::ReadVal(D);
Expr* Op = D.ReadOwnedPtr<Expr>();
return new CastExpr(t,Op,Loc);
}
void CharacterLiteral::directEmit(Serializer& S) const {
S.Emit(Value);
S.Emit(Loc);
S.Emit(getType());
}
CharacterLiteral* CharacterLiteral::directMaterialize(Deserializer& D) {
unsigned value = D.ReadInt();
SourceLocation Loc = SourceLocation::ReadVal(D);
QualType T = QualType::ReadVal(D);
return new CharacterLiteral(value,T,Loc);
}
void CompoundAssignOperator::directEmit(Serializer& S) const {
S.Emit(getType());
S.Emit(ComputationType);
S.Emit(getOperatorLoc());
S.EmitInt(getOpcode());
S.BatchEmitOwnedPtrs(getLHS(),getRHS());
}
CompoundAssignOperator*
CompoundAssignOperator::directMaterialize(Deserializer& D) {
QualType t = QualType::ReadVal(D);
QualType c = QualType::ReadVal(D);
SourceLocation L = SourceLocation::ReadVal(D);
Opcode Opc = static_cast<Opcode>(D.ReadInt());
Expr* LHS, *RHS;
D.BatchReadOwnedPtrs(LHS,RHS);
return new CompoundAssignOperator(LHS,RHS,Opc,t,c,L);
}
void CompoundStmt::directEmit(Serializer& S) const {
S.Emit(LBracLoc);
S.Emit(RBracLoc);
S.Emit(Body.size());
for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I)
S.EmitOwnedPtr(*I);
}
CompoundStmt* CompoundStmt::directMaterialize(Deserializer& D) {
SourceLocation LB = SourceLocation::ReadVal(D);
SourceLocation RB = SourceLocation::ReadVal(D);
unsigned size = D.ReadInt();
CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
stmt->Body.reserve(size);
for (unsigned i = 0; i < size; ++i)
stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
return stmt;
}
void ContinueStmt::directEmit(Serializer& S) const {
S.Emit(ContinueLoc);
}
ContinueStmt* ContinueStmt::directMaterialize(Deserializer& D) {
SourceLocation Loc = SourceLocation::ReadVal(D);
return new ContinueStmt(Loc);
}
void DeclStmt::directEmit(Serializer& S) const {
// FIXME: special handling for struct decls.
S.EmitOwnedPtr(getDecl());
}
void DeclRefExpr::directEmit(Serializer& S) const {
S.Emit(Loc);
S.Emit(getType());
S.EmitPtr(getDecl());
}
DeclRefExpr* DeclRefExpr::directMaterialize(Deserializer& D) {
SourceLocation Loc = SourceLocation::ReadVal(D);
QualType T = QualType::ReadVal(D);
DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc);
D.ReadPtr(dr->D,false);
return dr;
}
DeclStmt* DeclStmt::directMaterialize(Deserializer& D) {
ScopedDecl* decl = cast<ScopedDecl>(D.ReadOwnedPtr<Decl>());
return new DeclStmt(decl);
}
void DefaultStmt::directEmit(Serializer& S) const {
S.Emit(DefaultLoc);
S.EmitOwnedPtr(getSubStmt());
S.EmitPtr(getNextSwitchCase());
}
DefaultStmt* DefaultStmt::directMaterialize(Deserializer& D) {
SourceLocation Loc = SourceLocation::ReadVal(D);
Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
DefaultStmt* stmt = new DefaultStmt(Loc,SubStmt);
stmt->setNextSwitchCase(D.ReadPtr<SwitchCase>());
return stmt;
}
void DoStmt::directEmit(Serializer& S) const {
S.Emit(DoLoc);
S.EmitOwnedPtr(getCond());
S.EmitOwnedPtr(getBody());
}
DoStmt* DoStmt::directMaterialize(Deserializer& D) {
SourceLocation DoLoc = SourceLocation::ReadVal(D);
Expr* Cond = D.ReadOwnedPtr<Expr>();
Stmt* Body = D.ReadOwnedPtr<Stmt>();
return new DoStmt(Body,Cond,DoLoc);
}
void FloatingLiteral::directEmit(Serializer& S) const {
S.Emit(Loc);
S.Emit(getType());
S.Emit(Value);
}
FloatingLiteral* FloatingLiteral::directMaterialize(Deserializer& D) {
SourceLocation Loc = SourceLocation::ReadVal(D);
QualType t = QualType::ReadVal(D);
llvm::APFloat Val = llvm::APFloat::ReadVal(D);
FloatingLiteral* expr = new FloatingLiteral(Val,t,Loc);
return expr;
}
void ForStmt::directEmit(Serializer& S) const {
S.Emit(ForLoc);
S.EmitOwnedPtr(getInit());
S.EmitOwnedPtr(getCond());
S.EmitOwnedPtr(getInc());
S.EmitOwnedPtr(getBody());
}
ForStmt* ForStmt::directMaterialize(Deserializer& D) {
SourceLocation ForLoc = SourceLocation::ReadVal(D);
Stmt* Init = D.ReadOwnedPtr<Stmt>();
Expr* Cond = D.ReadOwnedPtr<Expr>();
Expr* Inc = D.ReadOwnedPtr<Expr>();
Stmt* Body = D.ReadOwnedPtr<Stmt>();
return new ForStmt(Init,Cond,Inc,Body,ForLoc);
}
void GotoStmt::directEmit(Serializer& S) const {
S.Emit(GotoLoc);
S.Emit(LabelLoc);
S.EmitPtr(Label);
}
GotoStmt* GotoStmt::directMaterialize(Deserializer& D) {
SourceLocation GotoLoc = SourceLocation::ReadVal(D);
SourceLocation LabelLoc = SourceLocation::ReadVal(D);
GotoStmt* stmt = new GotoStmt(NULL,GotoLoc,LabelLoc);
D.ReadPtr(stmt->Label); // This pointer may be backpatched later.
return stmt;
}
void IfStmt::directEmit(Serializer& S) const {
S.Emit(IfLoc);
S.EmitOwnedPtr(getCond());
S.EmitOwnedPtr(getThen());
S.EmitOwnedPtr(getElse());
}
IfStmt* IfStmt::directMaterialize(Deserializer& D) {
SourceLocation L = SourceLocation::ReadVal(D);
Expr* Cond = D.ReadOwnedPtr<Expr>();
Stmt* Then = D.ReadOwnedPtr<Stmt>();
Stmt* Else = D.ReadOwnedPtr<Stmt>();
return new IfStmt(L,Cond,Then,Else);
}
void ImaginaryLiteral::directEmit(Serializer& S) const {
S.Emit(getType());
S.EmitOwnedPtr(Val);
}
ImaginaryLiteral* ImaginaryLiteral::directMaterialize(Deserializer& D) {
QualType t = QualType::ReadVal(D);
Expr* expr = D.ReadOwnedPtr<Expr>();
assert (isa<FloatingLiteral>(expr) || isa<IntegerLiteral>(expr));
return new ImaginaryLiteral(expr,t);
}
void ImplicitCastExpr::directEmit(Serializer& S) const {
S.Emit(getType());
S.EmitOwnedPtr(Op);
}
ImplicitCastExpr* ImplicitCastExpr::directMaterialize(Deserializer& D) {
QualType t = QualType::ReadVal(D);
Expr* Op = D.ReadOwnedPtr<Expr>();
return new ImplicitCastExpr(t,Op);
}
void IndirectGotoStmt::directEmit(Serializer& S) const {
S.EmitPtr(Target);
}
IndirectGotoStmt* IndirectGotoStmt::directMaterialize(Deserializer& D) {
IndirectGotoStmt* stmt = new IndirectGotoStmt(NULL);
D.ReadPtr(stmt->Target); // The target may be backpatched.
return stmt;
}
void IntegerLiteral::directEmit(Serializer& S) const {
S.Emit(Loc);
S.Emit(getType());
S.Emit(getValue());
}
IntegerLiteral* IntegerLiteral::directMaterialize(Deserializer& D) {
SourceLocation Loc = SourceLocation::ReadVal(D);
QualType T = QualType::ReadVal(D);
// Create a dummy APInt because it is more efficient to deserialize
// it in place with the deserialized IntegerLiteral. (fewer copies)
llvm::APInt temp;
IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc);
D.Read(expr->Value);
return expr;
}
void LabelStmt::directEmit(Serializer& S) const {
S.EmitPtr(Label);
S.Emit(IdentLoc);
S.EmitOwnedPtr(SubStmt);
}
LabelStmt* LabelStmt::directMaterialize(Deserializer& D) {
IdentifierInfo* Label = D.ReadPtr<IdentifierInfo>();
SourceLocation IdentLoc = SourceLocation::ReadVal(D);
Stmt* SubStmt = D.ReadOwnedPtr<Stmt>();
return new LabelStmt(IdentLoc,Label,SubStmt);
}
void NullStmt::directEmit(Serializer& S) const {
S.Emit(SemiLoc);
}
NullStmt* NullStmt::directMaterialize(Deserializer& D) {
SourceLocation SemiLoc = SourceLocation::ReadVal(D);
return new NullStmt(SemiLoc);
}
void ParenExpr::directEmit(Serializer& S) const {
S.Emit(L);
S.Emit(R);
S.EmitOwnedPtr(Val);
}
ParenExpr* ParenExpr::directMaterialize(Deserializer& D) {
SourceLocation L = SourceLocation::ReadVal(D);
SourceLocation R = SourceLocation::ReadVal(D);
Expr* val = D.ReadOwnedPtr<Expr>();
return new ParenExpr(L,R,val);
}
void PreDefinedExpr::directEmit(Serializer& S) const {
S.Emit(Loc);
S.EmitInt(getIdentType());
S.Emit(getType());
}
PreDefinedExpr* PreDefinedExpr::directMaterialize(Deserializer& D) {
SourceLocation Loc = SourceLocation::ReadVal(D);
IdentType it = static_cast<IdentType>(D.ReadInt());
QualType Q = QualType::ReadVal(D);
return new PreDefinedExpr(Loc,Q,it);
}
void ReturnStmt::directEmit(Serializer& S) const {
S.Emit(RetLoc);
S.EmitOwnedPtr(RetExpr);
}
ReturnStmt* ReturnStmt::directMaterialize(Deserializer& D) {
SourceLocation RetLoc = SourceLocation::ReadVal(D);
Expr* RetExpr = D.ReadOwnedPtr<Expr>();
return new ReturnStmt(RetLoc,RetExpr);
}
void StringLiteral::directEmit(Serializer& S) const {
S.Emit(getType());
S.Emit(firstTokLoc);
S.Emit(lastTokLoc);
S.EmitBool(isWide());
S.Emit(getByteLength());
for (unsigned i = 0 ; i < ByteLength; ++i)
S.EmitInt(StrData[i]);
}
StringLiteral* StringLiteral::directMaterialize(Deserializer& D) {
QualType t = QualType::ReadVal(D);
SourceLocation firstTokLoc = SourceLocation::ReadVal(D);
SourceLocation lastTokLoc = SourceLocation::ReadVal(D);
bool isWide = D.ReadBool();
unsigned ByteLength = D.ReadInt();
StringLiteral* sl = new StringLiteral(NULL,0,isWide,t,firstTokLoc,lastTokLoc);
char* StrData = new char[ByteLength];
for (unsigned i = 0; i < ByteLength; ++i)
StrData[i] = (char) D.ReadInt();
sl->ByteLength = ByteLength;
sl->StrData = StrData;
return sl;
}
void SwitchStmt::directEmit(Serializer& S) const {
S.Emit(SwitchLoc);
S.EmitOwnedPtr(getCond());
S.EmitOwnedPtr(getBody());
S.EmitPtr(FirstCase);
}
SwitchStmt* SwitchStmt::directMaterialize(Deserializer& D) {
SourceLocation Loc = SourceLocation::ReadVal(D);
Stmt* Cond = D.ReadOwnedPtr<Stmt>();
Stmt* Body = D.ReadOwnedPtr<Stmt>();
SwitchCase* FirstCase = cast<SwitchCase>(D.ReadPtr<Stmt>());
SwitchStmt* stmt = new SwitchStmt(cast<Expr>(Cond));
stmt->setBody(Body,Loc);
stmt->FirstCase = FirstCase;
return stmt;
}
void UnaryOperator::directEmit(Serializer& S) const {
S.Emit(getType());
S.Emit(Loc);
S.EmitInt(Opc);
S.EmitOwnedPtr(Val);
}
UnaryOperator* UnaryOperator::directMaterialize(Deserializer& D) {
QualType t = QualType::ReadVal(D);
SourceLocation L = SourceLocation::ReadVal(D);
Opcode Opc = static_cast<Opcode>(D.ReadInt());
Expr* Val = D.ReadOwnedPtr<Expr>();
return new UnaryOperator(Val,Opc,t,L);
}
void WhileStmt::directEmit(Serializer& S) const {
S.Emit(WhileLoc);
S.EmitOwnedPtr(getCond());
S.EmitOwnedPtr(getBody());
}
WhileStmt* WhileStmt::directMaterialize(Deserializer& D) {
SourceLocation WhileLoc = SourceLocation::ReadVal(D);
Expr* Cond = D.ReadOwnedPtr<Expr>();
Stmt* Body = D.ReadOwnedPtr<Stmt>();
return new WhileStmt(Cond,Body,WhileLoc);
}
|