Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/AsmParser/LLParser.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- LLParser.cpp - Parser Class ---------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines the parser class for .ll files.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "LLParser.h"
14
#include "llvm/ADT/DenseMap.h"
15
#include "llvm/ADT/None.h"
16
#include "llvm/ADT/Optional.h"
17
#include "llvm/ADT/STLExtras.h"
18
#include "llvm/ADT/SmallPtrSet.h"
19
#include "llvm/AsmParser/SlotMapping.h"
20
#include "llvm/BinaryFormat/Dwarf.h"
21
#include "llvm/IR/Argument.h"
22
#include "llvm/IR/AutoUpgrade.h"
23
#include "llvm/IR/BasicBlock.h"
24
#include "llvm/IR/CallingConv.h"
25
#include "llvm/IR/Comdat.h"
26
#include "llvm/IR/Constants.h"
27
#include "llvm/IR/DebugInfoMetadata.h"
28
#include "llvm/IR/DerivedTypes.h"
29
#include "llvm/IR/Function.h"
30
#include "llvm/IR/GlobalIFunc.h"
31
#include "llvm/IR/GlobalObject.h"
32
#include "llvm/IR/InlineAsm.h"
33
#include "llvm/IR/Instruction.h"
34
#include "llvm/IR/Instructions.h"
35
#include "llvm/IR/Intrinsics.h"
36
#include "llvm/IR/LLVMContext.h"
37
#include "llvm/IR/Metadata.h"
38
#include "llvm/IR/Module.h"
39
#include "llvm/IR/Operator.h"
40
#include "llvm/IR/Type.h"
41
#include "llvm/IR/Value.h"
42
#include "llvm/IR/ValueSymbolTable.h"
43
#include "llvm/Support/Casting.h"
44
#include "llvm/Support/ErrorHandling.h"
45
#include "llvm/Support/MathExtras.h"
46
#include "llvm/Support/SaveAndRestore.h"
47
#include "llvm/Support/raw_ostream.h"
48
#include <algorithm>
49
#include <cassert>
50
#include <cstring>
51
#include <iterator>
52
#include <vector>
53
54
using namespace llvm;
55
56
79
static std::string getTypeString(Type *T) {
57
79
  std::string Result;
58
79
  raw_string_ostream Tmp(Result);
59
79
  Tmp << *T;
60
79
  return Tmp.str();
61
79
}
62
63
/// Run: module ::= toplevelentity*
64
37.9k
bool LLParser::Run() {
65
37.9k
  // Prime the lexer.
66
37.9k
  Lex.Lex();
67
37.9k
68
37.9k
  if (Context.shouldDiscardValueNames())
69
0
    return Error(
70
0
        Lex.getLoc(),
71
0
        "Can't read textual IR with a Context that discards named Values");
72
37.9k
73
37.9k
  return ParseTopLevelEntities() || 
ValidateEndOfModule()37.7k
||
74
37.9k
         
ValidateEndOfIndex()37.7k
;
75
37.9k
}
76
77
bool LLParser::parseStandaloneConstantValue(Constant *&C,
78
2.19k
                                            const SlotMapping *Slots) {
79
2.19k
  restoreParsingState(Slots);
80
2.19k
  Lex.Lex();
81
2.19k
82
2.19k
  Type *Ty = nullptr;
83
2.19k
  if (ParseType(Ty) || 
parseConstantValue(Ty, C)2.19k
)
84
5
    return true;
85
2.19k
  if (Lex.getKind() != lltok::Eof)
86
1
    return Error(Lex.getLoc(), "expected end of string");
87
2.18k
  return false;
88
2.18k
}
89
90
bool LLParser::parseTypeAtBeginning(Type *&Ty, unsigned &Read,
91
22
                                    const SlotMapping *Slots) {
92
22
  restoreParsingState(Slots);
93
22
  Lex.Lex();
94
22
95
22
  Read = 0;
96
22
  SMLoc Start = Lex.getLoc();
97
22
  Ty = nullptr;
98
22
  if (ParseType(Ty))
99
0
    return true;
100
22
  SMLoc End = Lex.getLoc();
101
22
  Read = End.getPointer() - Start.getPointer();
102
22
103
22
  return false;
104
22
}
105
106
2.21k
void LLParser::restoreParsingState(const SlotMapping *Slots) {
107
2.21k
  if (!Slots)
108
43
    return;
109
2.17k
  NumberedVals = Slots->GlobalValues;
110
2.17k
  NumberedMetadata = Slots->MetadataNodes;
111
2.17k
  for (const auto &I : Slots->NamedTypes)
112
55
    NamedTypes.insert(
113
55
        std::make_pair(I.getKey(), std::make_pair(I.second, LocTy())));
114
2.17k
  for (const auto &I : Slots->Types)
115
25
    NumberedTypes.insert(
116
25
        std::make_pair(I.first, std::make_pair(I.second, LocTy())));
117
2.17k
}
118
119
/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
120
/// module.
121
37.7k
bool LLParser::ValidateEndOfModule() {
122
37.7k
  if (!M)
123
0
    return false;
124
37.7k
  // Handle any function attribute group forward references.
125
569k
  
for (const auto &RAG : ForwardRefAttrGroups)37.7k
{
126
569k
    Value *V = RAG.first;
127
569k
    const std::vector<unsigned> &Attrs = RAG.second;
128
569k
    AttrBuilder B;
129
569k
130
569k
    for (const auto &Attr : Attrs)
131
66.3k
      B.merge(NumberedAttrBuilders[Attr]);
132
569k
133
569k
    if (Function *Fn = dyn_cast<Function>(V)) {
134
378k
      AttributeList AS = Fn->getAttributes();
135
378k
      AttrBuilder FnAttrs(AS.getFnAttributes());
136
378k
      AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
137
378k
138
378k
      FnAttrs.merge(B);
139
378k
140
378k
      // If the alignment was parsed as an attribute, move to the alignment
141
378k
      // field.
142
378k
      if (FnAttrs.hasAlignmentAttr()) {
143
0
        Fn->setAlignment(FnAttrs.getAlignment());
144
0
        FnAttrs.removeAttribute(Attribute::Alignment);
145
0
      }
146
378k
147
378k
      AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
148
378k
                            AttributeSet::get(Context, FnAttrs));
149
378k
      Fn->setAttributes(AS);
150
378k
    } else 
if (CallInst *191k
CI191k
= dyn_cast<CallInst>(V)) {
151
188k
      AttributeList AS = CI->getAttributes();
152
188k
      AttrBuilder FnAttrs(AS.getFnAttributes());
153
188k
      AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
154
188k
      FnAttrs.merge(B);
155
188k
      AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
156
188k
                            AttributeSet::get(Context, FnAttrs));
157
188k
      CI->setAttributes(AS);
158
188k
    } else 
if (InvokeInst *2.23k
II2.23k
= dyn_cast<InvokeInst>(V)) {
159
2.17k
      AttributeList AS = II->getAttributes();
160
2.17k
      AttrBuilder FnAttrs(AS.getFnAttributes());
161
2.17k
      AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
162
2.17k
      FnAttrs.merge(B);
163
2.17k
      AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
164
2.17k
                            AttributeSet::get(Context, FnAttrs));
165
2.17k
      II->setAttributes(AS);
166
2.17k
    } else 
if (CallBrInst *64
CBI64
= dyn_cast<CallBrInst>(V)) {
167
26
      AttributeList AS = CBI->getAttributes();
168
26
      AttrBuilder FnAttrs(AS.getFnAttributes());
169
26
      AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
170
26
      FnAttrs.merge(B);
171
26
      AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
172
26
                            AttributeSet::get(Context, FnAttrs));
173
26
      CBI->setAttributes(AS);
174
38
    } else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
175
38
      AttrBuilder Attrs(GV->getAttributes());
176
38
      Attrs.merge(B);
177
38
      GV->setAttributes(AttributeSet::get(Context,Attrs));
178
38
    } else {
179
0
      llvm_unreachable("invalid object with forward attribute group reference");
180
0
    }
181
569k
  }
182
37.7k
183
37.7k
  // If there are entries in ForwardRefBlockAddresses at this point, the
184
37.7k
  // function was never defined.
185
37.7k
  if (!ForwardRefBlockAddresses.empty())
186
0
    return Error(ForwardRefBlockAddresses.begin()->first.Loc,
187
0
                 "expected function name in blockaddress");
188
37.7k
189
37.7k
  for (const auto &NT : NumberedTypes)
190
675
    if (NT.second.second.isValid())
191
0
      return Error(NT.second.second,
192
0
                   "use of undefined type '%" + Twine(NT.first) + "'");
193
37.7k
194
37.7k
  for (StringMap<std::pair<Type*, LocTy> >::iterator I =
195
50.3k
       NamedTypes.begin(), E = NamedTypes.end(); I != E; 
++I12.6k
)
196
12.6k
    if (I->second.second.isValid())
197
2
      return Error(I->second.second,
198
2
                   "use of undefined type named '" + I->getKey() + "'");
199
37.7k
200
37.7k
  
if (37.7k
!ForwardRefComdats.empty()37.7k
)
201
1
    return Error(ForwardRefComdats.begin()->second,
202
1
                 "use of undefined comdat '$" +
203
1
                     ForwardRefComdats.begin()->first + "'");
204
37.7k
205
37.7k
  if (!ForwardRefVals.empty())
206
1
    return Error(ForwardRefVals.begin()->second.second,
207
1
                 "use of undefined value '@" + ForwardRefVals.begin()->first +
208
1
                 "'");
209
37.7k
210
37.7k
  if (!ForwardRefValIDs.empty())
211
0
    return Error(ForwardRefValIDs.begin()->second.second,
212
0
                 "use of undefined value '@" +
213
0
                 Twine(ForwardRefValIDs.begin()->first) + "'");
214
37.7k
215
37.7k
  if (!ForwardRefMDNodes.empty())
216
3
    return Error(ForwardRefMDNodes.begin()->second.second,
217
3
                 "use of undefined metadata '!" +
218
3
                 Twine(ForwardRefMDNodes.begin()->first) + "'");
219
37.7k
220
37.7k
  // Resolve metadata cycles.
221
72.7k
  
for (auto &N : NumberedMetadata)37.7k
{
222
72.7k
    if (N.second && !N.second->isResolved())
223
179
      N.second->resolveCycles();
224
72.7k
  }
225
37.7k
226
37.7k
  for (auto *Inst : InstsWithTBAATag) {
227
4.63k
    MDNode *MD = Inst->getMetadata(LLVMContext::MD_tbaa);
228
4.63k
    assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
229
4.63k
    auto *UpgradedMD = UpgradeTBAANode(*MD);
230
4.63k
    if (MD != UpgradedMD)
231
194
      Inst->setMetadata(LLVMContext::MD_tbaa, UpgradedMD);
232
4.63k
  }
233
37.7k
234
37.7k
  // Look for intrinsic functions and CallInst that need to be upgraded
235
418k
  for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
236
380k
    UpgradeCallsToIntrinsic(&*FI++); // must be post-increment, as we remove
237
37.7k
238
37.7k
  // Some types could be renamed during loading if several modules are
239
37.7k
  // loaded in the same LLVMContext (LTO scenario). In this case we should
240
37.7k
  // remangle intrinsics names as well.
241
413k
  for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) {
242
375k
    Function *F = &*FI++;
243
375k
    if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {
244
2
      F->replaceAllUsesWith(Remangled.getValue());
245
2
      F->eraseFromParent();
246
2
    }
247
375k
  }
248
37.7k
249
37.7k
  if (UpgradeDebugInfo)
250
13.7k
    llvm::UpgradeDebugInfo(*M);
251
37.7k
252
37.7k
  UpgradeModuleFlags(*M);
253
37.7k
  UpgradeSectionAttributes(*M);
254
37.7k
255
37.7k
  if (!Slots)
256
36.6k
    return false;
257
1.13k
  // Initialize the slot mapping.
258
1.13k
  // Because by this point we've parsed and validated everything, we can "steal"
259
1.13k
  // the mapping from LLParser as it doesn't need it anymore.
260
1.13k
  Slots->GlobalValues = std::move(NumberedVals);
261
1.13k
  Slots->MetadataNodes = std::move(NumberedMetadata);
262
1.13k
  for (const auto &I : NamedTypes)
263
91
    Slots->NamedTypes.insert(std::make_pair(I.getKey(), I.second.first));
264
1.13k
  for (const auto &I : NumberedTypes)
265
16
    Slots->Types.insert(std::make_pair(I.first, I.second.first));
266
1.13k
267
1.13k
  return false;
268
1.13k
}
269
270
/// Do final validity and sanity checks at the end of the index.
271
37.7k
bool LLParser::ValidateEndOfIndex() {
272
37.7k
  if (!Index)
273
36.7k
    return false;
274
1.01k
275
1.01k
  if (!ForwardRefValueInfos.empty())
276
0
    return Error(ForwardRefValueInfos.begin()->second.front().second,
277
0
                 "use of undefined summary '^" +
278
0
                     Twine(ForwardRefValueInfos.begin()->first) + "'");
279
1.01k
280
1.01k
  if (!ForwardRefAliasees.empty())
281
0
    return Error(ForwardRefAliasees.begin()->second.front().second,
282
0
                 "use of undefined summary '^" +
283
0
                     Twine(ForwardRefAliasees.begin()->first) + "'");
284
1.01k
285
1.01k
  if (!ForwardRefTypeIds.empty())
286
0
    return Error(ForwardRefTypeIds.begin()->second.front().second,
287
0
                 "use of undefined type id summary '^" +
288
0
                     Twine(ForwardRefTypeIds.begin()->first) + "'");
289
1.01k
290
1.01k
  return false;
291
1.01k
}
292
293
//===----------------------------------------------------------------------===//
294
// Top-Level Entities
295
//===----------------------------------------------------------------------===//
296
297
37.9k
bool LLParser::ParseTopLevelEntities() {
298
37.9k
  // If there is no Module, then parse just the summary index entries.
299
37.9k
  if (!M) {
300
0
    while (true) {
301
0
      switch (Lex.getKind()) {
302
0
      case lltok::Eof:
303
0
        return false;
304
0
      case lltok::SummaryID:
305
0
        if (ParseSummaryEntry())
306
0
          return true;
307
0
        break;
308
0
      case lltok::kw_source_filename:
309
0
        if (ParseSourceFileName())
310
0
          return true;
311
0
        break;
312
0
      default:
313
0
        // Skip everything else
314
0
        Lex.Lex();
315
0
      }
316
0
    }
317
0
  }
318
569k
  
while (37.9k
true) {
319
569k
    switch (Lex.getKind()) {
320
569k
    
default: return TokError("expected top-level entity")3
;
321
569k
    
case lltok::Eof: return false37.7k
;
322
569k
    
case lltok::kw_declare: if (81.3k
ParseDeclare()81.3k
)
return true11
; break;
323
297k
    case lltok::kw_define:  if (ParseDefine()) 
return true91
; break;
324
297k
    
case lltok::kw_module: if (341
ParseModuleAsm()341
)
return true0
; break;
325
14.0k
    case lltok::kw_target:  if (ParseTargetDefinition()) 
return true0
; break;
326
14.0k
    case lltok::kw_source_filename:
327
1.78k
      if (ParseSourceFileName())
328
0
        return true;
329
1.78k
      break;
330
1.78k
    
case lltok::kw_deplibs: if (0
ParseDepLibs()0
)
return true0
; break;
331
675
    case lltok::LocalVarID: if (ParseUnnamedType()) 
return true0
; break;
332
12.6k
    case lltok::LocalVar:   if (ParseNamedType()) 
return true2
; break;
333
12.6k
    
case lltok::GlobalID: if (695
ParseUnnamedGlobal()695
)
return true6
; break;
334
30.8k
    case lltok::GlobalVar:  if (ParseNamedGlobal()) 
return true24
; break;
335
30.8k
    
case lltok::ComdatVar: if (812
parseComdat()812
)
return true1
; break;
336
72.8k
    case lltok::exclaim:    if (ParseStandaloneMetadata()) 
return true54
; break;
337
72.7k
    case lltok::SummaryID:
338
157
      if (ParseSummaryEntry())
339
3
        return true;
340
154
      break;
341
6.83k
    case lltok::MetadataVar:if (ParseNamedMetadata()) 
return true0
; break;
342
10.8k
    case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) 
return true1
; break;
343
10.8k
    
case lltok::kw_uselistorder: if (127
ParseUseListOrder()127
)
return true10
; break;
344
117
    case lltok::kw_uselistorder_bb:
345
12
      if (ParseUseListOrderBB())
346
6
        return true;
347
6
      break;
348
569k
    }
349
569k
  }
350
37.9k
}
351
352
/// toplevelentity
353
///   ::= 'module' 'asm' STRINGCONSTANT
354
341
bool LLParser::ParseModuleAsm() {
355
341
  assert(Lex.getKind() == lltok::kw_module);
356
341
  Lex.Lex();
357
341
358
341
  std::string AsmStr;
359
341
  if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
360
341
      ParseStringConstant(AsmStr)) 
return true0
;
361
341
362
341
  M->appendModuleInlineAsm(AsmStr);
363
341
  return false;
364
341
}
365
366
/// toplevelentity
367
///   ::= 'target' 'triple' '=' STRINGCONSTANT
368
///   ::= 'target' 'datalayout' '=' STRINGCONSTANT
369
14.0k
bool LLParser::ParseTargetDefinition() {
370
14.0k
  assert(Lex.getKind() == lltok::kw_target);
371
14.0k
  std::string Str;
372
14.0k
  switch (Lex.Lex()) {
373
14.0k
  
default: return TokError("unknown target property")0
;
374
14.0k
  case lltok::kw_triple:
375
6.23k
    Lex.Lex();
376
6.23k
    if (ParseToken(lltok::equal, "expected '=' after target triple") ||
377
6.23k
        ParseStringConstant(Str))
378
0
      return true;
379
6.23k
    M->setTargetTriple(Str);
380
6.23k
    return false;
381
7.83k
  case lltok::kw_datalayout:
382
7.83k
    Lex.Lex();
383
7.83k
    if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
384
7.83k
        ParseStringConstant(Str))
385
0
      return true;
386
7.83k
    if (DataLayoutStr.empty())
387
7.83k
      M->setDataLayout(Str);
388
7.83k
    return false;
389
14.0k
  }
390
14.0k
}
391
392
/// toplevelentity
393
///   ::= 'source_filename' '=' STRINGCONSTANT
394
1.78k
bool LLParser::ParseSourceFileName() {
395
1.78k
  assert(Lex.getKind() == lltok::kw_source_filename);
396
1.78k
  Lex.Lex();
397
1.78k
  if (ParseToken(lltok::equal, "expected '=' after source_filename") ||
398
1.78k
      ParseStringConstant(SourceFileName))
399
0
    return true;
400
1.78k
  if (M)
401
1.78k
    M->setSourceFileName(SourceFileName);
402
1.78k
  return false;
403
1.78k
}
404
405
/// toplevelentity
406
///   ::= 'deplibs' '=' '[' ']'
407
///   ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
408
/// FIXME: Remove in 4.0. Currently parse, but ignore.
409
0
bool LLParser::ParseDepLibs() {
410
0
  assert(Lex.getKind() == lltok::kw_deplibs);
411
0
  Lex.Lex();
412
0
  if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
413
0
      ParseToken(lltok::lsquare, "expected '=' after deplibs"))
414
0
    return true;
415
0
416
0
  if (EatIfPresent(lltok::rsquare))
417
0
    return false;
418
0
419
0
  do {
420
0
    std::string Str;
421
0
    if (ParseStringConstant(Str)) return true;
422
0
  } while (EatIfPresent(lltok::comma));
423
0
424
0
  return ParseToken(lltok::rsquare, "expected ']' at end of list");
425
0
}
426
427
/// ParseUnnamedType:
428
///   ::= LocalVarID '=' 'type' type
429
675
bool LLParser::ParseUnnamedType() {
430
675
  LocTy TypeLoc = Lex.getLoc();
431
675
  unsigned TypeID = Lex.getUIntVal();
432
675
  Lex.Lex(); // eat LocalVarID;
433
675
434
675
  if (ParseToken(lltok::equal, "expected '=' after name") ||
435
675
      ParseToken(lltok::kw_type, "expected 'type' after '='"))
436
0
    return true;
437
675
438
675
  Type *Result = nullptr;
439
675
  if (ParseStructDefinition(TypeLoc, "",
440
675
                            NumberedTypes[TypeID], Result)) 
return true0
;
441
675
442
675
  if (!isa<StructType>(Result)) {
443
4
    std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
444
4
    if (Entry.first)
445
0
      return Error(TypeLoc, "non-struct types may not be recursive");
446
4
    Entry.first = Result;
447
4
    Entry.second = SMLoc();
448
4
  }
449
675
450
675
  return false;
451
675
}
452
453
/// toplevelentity
454
///   ::= LocalVar '=' 'type' type
455
12.6k
bool LLParser::ParseNamedType() {
456
12.6k
  std::string Name = Lex.getStrVal();
457
12.6k
  LocTy NameLoc = Lex.getLoc();
458
12.6k
  Lex.Lex();  // eat LocalVar.
459
12.6k
460
12.6k
  if (ParseToken(lltok::equal, "expected '=' after name") ||
461
12.6k
      ParseToken(lltok::kw_type, "expected 'type' after name"))
462
0
    return true;
463
12.6k
464
12.6k
  Type *Result = nullptr;
465
12.6k
  if (ParseStructDefinition(NameLoc, Name,
466
12.6k
                            NamedTypes[Name], Result)) 
return true2
;
467
12.6k
468
12.6k
  if (!isa<StructType>(Result)) {
469
701
    std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
470
701
    if (Entry.first)
471
0
      return Error(NameLoc, "non-struct types may not be recursive");
472
701
    Entry.first = Result;
473
701
    Entry.second = SMLoc();
474
701
  }
475
12.6k
476
12.6k
  return false;
477
12.6k
}
478
479
/// toplevelentity
480
///   ::= 'declare' FunctionHeader
481
81.3k
bool LLParser::ParseDeclare() {
482
81.3k
  assert(Lex.getKind() == lltok::kw_declare);
483
81.3k
  Lex.Lex();
484
81.3k
485
81.3k
  std::vector<std::pair<unsigned, MDNode *>> MDs;
486
81.4k
  while (Lex.getKind() == lltok::MetadataVar) {
487
53
    unsigned MDK;
488
53
    MDNode *N;
489
53
    if (ParseMetadataAttachment(MDK, N))
490
0
      return true;
491
53
    MDs.push_back({MDK, N});
492
53
  }
493
81.3k
494
81.3k
  Function *F;
495
81.3k
  if (ParseFunctionHeader(F, false))
496
11
    return true;
497
81.3k
  for (auto &MD : MDs)
498
53
    F->addMetadata(MD.first, *MD.second);
499
81.3k
  return false;
500
81.3k
}
501
502
/// toplevelentity
503
///   ::= 'define' FunctionHeader (!dbg !56)* '{' ...
504
297k
bool LLParser::ParseDefine() {
505
297k
  assert(Lex.getKind() == lltok::kw_define);
506
297k
  Lex.Lex();
507
297k
508
297k
  Function *F;
509
297k
  return ParseFunctionHeader(F, true) ||
510
297k
         
ParseOptionalFunctionMetadata(*F)297k
||
511
297k
         
ParseFunctionBody(*F)297k
;
512
297k
}
513
514
/// ParseGlobalType
515
///   ::= 'constant'
516
///   ::= 'global'
517
30.5k
bool LLParser::ParseGlobalType(bool &IsConstant) {
518
30.5k
  if (Lex.getKind() == lltok::kw_constant)
519
5.16k
    IsConstant = true;
520
25.3k
  else if (Lex.getKind() == lltok::kw_global)
521
25.3k
    IsConstant = false;
522
0
  else {
523
0
    IsConstant = false;
524
0
    return TokError("expected 'global' or 'constant'");
525
0
  }
526
30.5k
  Lex.Lex();
527
30.5k
  return false;
528
30.5k
}
529
530
bool LLParser::ParseOptionalUnnamedAddr(
531
410k
    GlobalVariable::UnnamedAddr &UnnamedAddr) {
532
410k
  if (EatIfPresent(lltok::kw_unnamed_addr))
533
3.41k
    UnnamedAddr = GlobalValue::UnnamedAddr::Global;
534
406k
  else if (EatIfPresent(lltok::kw_local_unnamed_addr))
535
6.99k
    UnnamedAddr = GlobalValue::UnnamedAddr::Local;
536
399k
  else
537
399k
    UnnamedAddr = GlobalValue::UnnamedAddr::None;
538
410k
  return false;
539
410k
}
540
541
/// ParseUnnamedGlobal:
542
///   OptionalVisibility (ALIAS | IFUNC) ...
543
///   OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
544
///   OptionalDLLStorageClass
545
///                                                     ...   -> global variable
546
///   GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ...
547
///   GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
548
///                OptionalDLLStorageClass
549
///                                                     ...   -> global variable
550
695
bool LLParser::ParseUnnamedGlobal() {
551
695
  unsigned VarID = NumberedVals.size();
552
695
  std::string Name;
553
695
  LocTy NameLoc = Lex.getLoc();
554
695
555
695
  // Handle the GlobalID form.
556
695
  if (Lex.getKind() == lltok::GlobalID) {
557
695
    if (Lex.getUIntVal() != VarID)
558
0
      return Error(Lex.getLoc(), "variable expected to be numbered '%" +
559
0
                   Twine(VarID) + "'");
560
695
    Lex.Lex(); // eat GlobalID;
561
695
562
695
    if (ParseToken(lltok::equal, "expected '=' after name"))
563
0
      return true;
564
695
  }
565
695
566
695
  bool HasLinkage;
567
695
  unsigned Linkage, Visibility, DLLStorageClass;
568
695
  bool DSOLocal;
569
695
  GlobalVariable::ThreadLocalMode TLM;
570
695
  GlobalVariable::UnnamedAddr UnnamedAddr;
571
695
  if (ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
572
695
                           DSOLocal) ||
573
695
      ParseOptionalThreadLocal(TLM) || ParseOptionalUnnamedAddr(UnnamedAddr))
574
0
    return true;
575
695
576
695
  if (Lex.getKind() != lltok::kw_alias && 
Lex.getKind() != lltok::kw_ifunc691
)
577
691
    return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
578
691
                       DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
579
4
580
4
  return parseIndirectSymbol(Name, NameLoc, Linkage, Visibility,
581
4
                             DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
582
4
}
583
584
/// ParseNamedGlobal:
585
///   GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ...
586
///   GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
587
///                 OptionalVisibility OptionalDLLStorageClass
588
///                                                     ...   -> global variable
589
30.8k
bool LLParser::ParseNamedGlobal() {
590
30.8k
  assert(Lex.getKind() == lltok::GlobalVar);
591
30.8k
  LocTy NameLoc = Lex.getLoc();
592
30.8k
  std::string Name = Lex.getStrVal();
593
30.8k
  Lex.Lex();
594
30.8k
595
30.8k
  bool HasLinkage;
596
30.8k
  unsigned Linkage, Visibility, DLLStorageClass;
597
30.8k
  bool DSOLocal;
598
30.8k
  GlobalVariable::ThreadLocalMode TLM;
599
30.8k
  GlobalVariable::UnnamedAddr UnnamedAddr;
600
30.8k
  if (ParseToken(lltok::equal, "expected '=' in global variable") ||
601
30.8k
      ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
602
30.8k
                           DSOLocal) ||
603
30.8k
      
ParseOptionalThreadLocal(TLM)30.8k
||
ParseOptionalUnnamedAddr(UnnamedAddr)30.8k
)
604
1
    return true;
605
30.8k
606
30.8k
  if (Lex.getKind() != lltok::kw_alias && 
Lex.getKind() != lltok::kw_ifunc29.8k
)
607
29.8k
    return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
608
29.8k
                       DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
609
1.04k
610
1.04k
  return parseIndirectSymbol(Name, NameLoc, Linkage, Visibility,
611
1.04k
                             DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
612
1.04k
}
613
614
812
bool LLParser::parseComdat() {
615
812
  assert(Lex.getKind() == lltok::ComdatVar);
616
812
  std::string Name = Lex.getStrVal();
617
812
  LocTy NameLoc = Lex.getLoc();
618
812
  Lex.Lex();
619
812
620
812
  if (ParseToken(lltok::equal, "expected '=' here"))
621
0
    return true;
622
812
623
812
  if (ParseToken(lltok::kw_comdat, "expected comdat keyword"))
624
0
    return TokError("expected comdat type");
625
812
626
812
  Comdat::SelectionKind SK;
627
812
  switch (Lex.getKind()) {
628
812
  default:
629
0
    return TokError("unknown selection kind");
630
812
  case lltok::kw_any:
631
726
    SK = Comdat::Any;
632
726
    break;
633
812
  case lltok::kw_exactmatch:
634
5
    SK = Comdat::ExactMatch;
635
5
    break;
636
812
  case lltok::kw_largest:
637
67
    SK = Comdat::Largest;
638
67
    break;
639
812
  case lltok::kw_noduplicates:
640
7
    SK = Comdat::NoDuplicates;
641
7
    break;
642
812
  case lltok::kw_samesize:
643
7
    SK = Comdat::SameSize;
644
7
    break;
645
812
  }
646
812
  Lex.Lex();
647
812
648
812
  // See if the comdat was forward referenced, if so, use the comdat.
649
812
  Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
650
812
  Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
651
812
  if (I != ComdatSymTab.end() && 
!ForwardRefComdats.erase(Name)2
)
652
1
    return Error(NameLoc, "redefinition of comdat '$" + Name + "'");
653
811
654
811
  Comdat *C;
655
811
  if (I != ComdatSymTab.end())
656
1
    C = &I->second;
657
810
  else
658
810
    C = M->getOrInsertComdat(Name);
659
811
  C->setSelectionKind(SK);
660
811
661
811
  return false;
662
811
}
663
664
// MDString:
665
//   ::= '!' STRINGCONSTANT
666
17.6k
bool LLParser::ParseMDString(MDString *&Result) {
667
17.6k
  std::string Str;
668
17.6k
  if (ParseStringConstant(Str)) 
return true0
;
669
17.6k
  Result = MDString::get(Context, Str);
670
17.6k
  return false;
671
17.6k
}
672
673
// MDNode:
674
//   ::= '!' MDNodeNumber
675
153k
bool LLParser::ParseMDNodeID(MDNode *&Result) {
676
153k
  // !{ ..., !42, ... }
677
153k
  LocTy IDLoc = Lex.getLoc();
678
153k
  unsigned MID = 0;
679
153k
  if (ParseUInt32(MID))
680
0
    return true;
681
153k
682
153k
  // If not a forward reference, just return it now.
683
153k
  if (NumberedMetadata.count(MID)) {
684
83.0k
    Result = NumberedMetadata[MID];
685
83.0k
    return false;
686
83.0k
  }
687
70.7k
688
70.7k
  // Otherwise, create MDNode forward reference.
689
70.7k
  auto &FwdRef = ForwardRefMDNodes[MID];
690
70.7k
  FwdRef = std::make_pair(MDTuple::getTemporary(Context, None), IDLoc);
691
70.7k
692
70.7k
  Result = FwdRef.first.get();
693
70.7k
  NumberedMetadata[MID].reset(Result);
694
70.7k
  return false;
695
70.7k
}
696
697
/// ParseNamedMetadata:
698
///   !foo = !{ !1, !2 }
699
6.83k
bool LLParser::ParseNamedMetadata() {
700
6.83k
  assert(Lex.getKind() == lltok::MetadataVar);
701
6.83k
  std::string Name = Lex.getStrVal();
702
6.83k
  Lex.Lex();
703
6.83k
704
6.83k
  if (ParseToken(lltok::equal, "expected '=' here") ||
705
6.83k
      ParseToken(lltok::exclaim, "Expected '!' here") ||
706
6.83k
      ParseToken(lltok::lbrace, "Expected '{' here"))
707
0
    return true;
708
6.83k
709
6.83k
  NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
710
6.83k
  if (Lex.getKind() != lltok::rbrace)
711
12.2k
    
do 6.82k
{
712
12.2k
      MDNode *N = nullptr;
713
12.2k
      // Parse DIExpressions inline as a special case. They are still MDNodes,
714
12.2k
      // so they can still appear in named metadata. Remove this logic if they
715
12.2k
      // become plain Metadata.
716
12.2k
      if (Lex.getKind() == lltok::MetadataVar &&
717
12.2k
          
Lex.getStrVal() == "DIExpression"50
) {
718
50
        if (ParseDIExpression(N, /*IsDistinct=*/false))
719
0
          return true;
720
12.1k
      } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
721
12.1k
                 ParseMDNodeID(N)) {
722
0
        return true;
723
0
      }
724
12.2k
      NMD->addOperand(N);
725
12.2k
    } while (EatIfPresent(lltok::comma));
726
6.83k
727
6.83k
  return ParseToken(lltok::rbrace, "expected end of metadata node");
728
6.83k
}
729
730
/// ParseStandaloneMetadata:
731
///   !42 = !{...}
732
72.8k
bool LLParser::ParseStandaloneMetadata() {
733
72.8k
  assert(Lex.getKind() == lltok::exclaim);
734
72.8k
  Lex.Lex();
735
72.8k
  unsigned MetadataID = 0;
736
72.8k
737
72.8k
  MDNode *Init;
738
72.8k
  if (ParseUInt32(MetadataID) ||
739
72.8k
      ParseToken(lltok::equal, "expected '=' here"))
740
0
    return true;
741
72.8k
742
72.8k
  // Detect common error, from old metadata syntax.
743
72.8k
  if (Lex.getKind() == lltok::Type)
744
1
    return TokError("unexpected type in metadata definition");
745
72.8k
746
72.8k
  bool IsDistinct = EatIfPresent(lltok::kw_distinct);
747
72.8k
  if (Lex.getKind() == lltok::MetadataVar) {
748
48.4k
    if (ParseSpecializedMDNode(Init, IsDistinct))
749
50
      return true;
750
24.4k
  } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
751
24.4k
             ParseMDTuple(Init, IsDistinct))
752
3
    return true;
753
72.7k
754
72.7k
  // See if this was forward referenced, if so, handle it.
755
72.7k
  auto FI = ForwardRefMDNodes.find(MetadataID);
756
72.7k
  if (FI != ForwardRefMDNodes.end()) {
757
70.7k
    FI->second.first->replaceAllUsesWith(Init);
758
70.7k
    ForwardRefMDNodes.erase(FI);
759
70.7k
760
70.7k
    assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
761
70.7k
  } else {
762
1.98k
    if (NumberedMetadata.count(MetadataID))
763
0
      return TokError("Metadata id is already used");
764
1.98k
    NumberedMetadata[MetadataID].reset(Init);
765
1.98k
  }
766
72.7k
767
72.7k
  return false;
768
72.7k
}
769
770
// Skips a single module summary entry.
771
6
bool LLParser::SkipModuleSummaryEntry() {
772
6
  // Each module summary entry consists of a tag for the entry
773
6
  // type, followed by a colon, then the fields surrounded by nested sets of
774
6
  // parentheses. The "tag:" looks like a Label. Once parsing support is
775
6
  // in place we will look for the tokens corresponding to the expected tags.
776
6
  if (Lex.getKind() != lltok::kw_gv && 
Lex.getKind() != lltok::kw_module4
&&
777
6
      
Lex.getKind() != lltok::kw_typeid1
)
778
1
    return TokError(
779
1
        "Expected 'gv', 'module', or 'typeid' at the start of summary entry");
780
5
  Lex.Lex();
781
5
  if (ParseToken(lltok::colon, "expected ':' at start of summary entry") ||
782
5
      ParseToken(lltok::lparen, "expected '(' at start of summary entry"))
783
1
    return true;
784
4
  // Now walk through the parenthesized entry, until the number of open
785
4
  // parentheses goes back down to 0 (the first '(' was parsed above).
786
4
  unsigned NumOpenParen = 1;
787
94
  do {
788
94
    switch (Lex.getKind()) {
789
94
    case lltok::lparen:
790
6
      NumOpenParen++;
791
6
      break;
792
94
    case lltok::rparen:
793
9
      NumOpenParen--;
794
9
      break;
795
94
    case lltok::Eof:
796
1
      return TokError("found end of file while parsing summary entry");
797
94
    default:
798
78
      // Skip everything in between parentheses.
799
78
      break;
800
93
    }
801
93
    Lex.Lex();
802
93
  } while (NumOpenParen > 0);
803
4
  
return false3
;
804
4
}
805
806
/// SummaryEntry
807
///   ::= SummaryID '=' GVEntry | ModuleEntry | TypeIdEntry
808
157
bool LLParser::ParseSummaryEntry() {
809
157
  assert(Lex.getKind() == lltok::SummaryID);
810
157
  unsigned SummaryID = Lex.getUIntVal();
811
157
812
157
  // For summary entries, colons should be treated as distinct tokens,
813
157
  // not an indication of the end of a label token.
814
157
  Lex.setIgnoreColonInIdentifiers(true);
815
157
816
157
  Lex.Lex();
817
157
  if (ParseToken(lltok::equal, "expected '=' here"))
818
0
    return true;
819
157
820
157
  // If we don't have an index object, skip the summary entry.
821
157
  if (!Index)
822
6
    return SkipModuleSummaryEntry();
823
151
824
151
  bool result = false;
825
151
  switch (Lex.getKind()) {
826
151
  case lltok::kw_gv:
827
116
    result = ParseGVEntry(SummaryID);
828
116
    break;
829
151
  case lltok::kw_module:
830
24
    result = ParseModuleEntry(SummaryID);
831
24
    break;
832
151
  case lltok::kw_typeid:
833
7
    result = ParseTypeIdEntry(SummaryID);
834
7
    break;
835
151
  case lltok::kw_typeidCompatibleVTable:
836
4
    result = ParseTypeIdCompatibleVtableEntry(SummaryID);
837
4
    break;
838
151
  default:
839
0
    result = Error(Lex.getLoc(), "unexpected summary kind");
840
0
    break;
841
151
  }
842
151
  Lex.setIgnoreColonInIdentifiers(false);
843
151
  return result;
844
151
}
845
846
410k
static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
847
410k
  return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
848
410k
         
(GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility6.08k
;
849
410k
}
850
851
// If there was an explicit dso_local, update GV. In the absence of an explicit
852
// dso_local we keep the default value.
853
410k
static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
854
410k
  if (DSOLocal)
855
2.88k
    GV.setDSOLocal(true);
856
410k
}
857
858
/// parseIndirectSymbol:
859
///   ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
860
///                     OptionalVisibility OptionalDLLStorageClass
861
///                     OptionalThreadLocal OptionalUnnamedAddr
862
///                     'alias|ifunc' IndirectSymbol IndirectSymbolAttr*
863
///
864
/// IndirectSymbol
865
///   ::= TypeAndValue
866
///
867
/// IndirectSymbolAttr
868
///   ::= ',' 'partition' StringConstant
869
///
870
/// Everything through OptionalUnnamedAddr has already been parsed.
871
///
872
bool LLParser::parseIndirectSymbol(const std::string &Name, LocTy NameLoc,
873
                                   unsigned L, unsigned Visibility,
874
                                   unsigned DLLStorageClass, bool DSOLocal,
875
                                   GlobalVariable::ThreadLocalMode TLM,
876
1.04k
                                   GlobalVariable::UnnamedAddr UnnamedAddr) {
877
1.04k
  bool IsAlias;
878
1.04k
  if (Lex.getKind() == lltok::kw_alias)
879
1.00k
    IsAlias = true;
880
46
  else if (Lex.getKind() == lltok::kw_ifunc)
881
46
    IsAlias = false;
882
46
  else
883
46
    
llvm_unreachable0
("Not an alias or ifunc!");
884
1.04k
  Lex.Lex();
885
1.04k
886
1.04k
  GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
887
1.04k
888
1.04k
  if(IsAlias && 
!GlobalAlias::isValidLinkage(Linkage)1.00k
)
889
0
    return Error(NameLoc, "invalid linkage type for alias");
890
1.04k
891
1.04k
  if (!isValidVisibilityForLinkage(Visibility, L))
892
4
    return Error(NameLoc,
893
4
                 "symbol with local linkage must have default visibility");
894
1.04k
895
1.04k
  Type *Ty;
896
1.04k
  LocTy ExplicitTypeLoc = Lex.getLoc();
897
1.04k
  if (ParseType(Ty) ||
898
1.04k
      ParseToken(lltok::comma, "expected comma after alias or ifunc's type"))
899
0
    return true;
900
1.04k
901
1.04k
  Constant *Aliasee;
902
1.04k
  LocTy AliaseeLoc = Lex.getLoc();
903
1.04k
  if (Lex.getKind() != lltok::kw_bitcast &&
904
1.04k
      
Lex.getKind() != lltok::kw_getelementptr888
&&
905
1.04k
      
Lex.getKind() != lltok::kw_addrspacecast760
&&
906
1.04k
      
Lex.getKind() != lltok::kw_inttoptr749
) {
907
742
    if (ParseGlobalTypeAndValue(Aliasee))
908
0
      return true;
909
302
  } else {
910
302
    // The bitcast dest type is not present, it is implied by the dest type.
911
302
    ValID ID;
912
302
    if (ParseValID(ID))
913
1
      return true;
914
301
    if (ID.Kind != ValID::t_Constant)
915
0
      return Error(AliaseeLoc, "invalid aliasee");
916
301
    Aliasee = ID.ConstantVal;
917
301
  }
918
1.04k
919
1.04k
  Type *AliaseeType = Aliasee->getType();
920
1.04k
  auto *PTy = dyn_cast<PointerType>(AliaseeType);
921
1.04k
  if (!PTy)
922
0
    return Error(AliaseeLoc, "An alias or ifunc must have pointer type");
923
1.04k
  unsigned AddrSpace = PTy->getAddressSpace();
924
1.04k
925
1.04k
  if (IsAlias && 
Ty != PTy->getElementType()997
)
926
1
    return Error(
927
1
        ExplicitTypeLoc,
928
1
        "explicit pointee type doesn't match operand's pointee type");
929
1.04k
930
1.04k
  if (!IsAlias && 
!PTy->getElementType()->isFunctionTy()46
)
931
0
    return Error(
932
0
        ExplicitTypeLoc,
933
0
        "explicit pointee type should be a function type");
934
1.04k
935
1.04k
  GlobalValue *GVal = nullptr;
936
1.04k
937
1.04k
  // See if the alias was forward referenced, if so, prepare to replace the
938
1.04k
  // forward reference.
939
1.04k
  if (!Name.empty()) {
940
1.03k
    GVal = M->getNamedValue(Name);
941
1.03k
    if (GVal) {
942
67
      if (!ForwardRefVals.erase(Name))
943
1
        return Error(NameLoc, "redefinition of global '@" + Name + "'");
944
4
    }
945
4
  } else {
946
4
    auto I = ForwardRefValIDs.find(NumberedVals.size());
947
4
    if (I != ForwardRefValIDs.end()) {
948
1
      GVal = I->second.first;
949
1
      ForwardRefValIDs.erase(I);
950
1
    }
951
4
  }
952
1.04k
953
1.04k
  // Okay, create the alias but do not insert it into the module yet.
954
1.04k
  std::unique_ptr<GlobalIndirectSymbol> GA;
955
1.04k
  if (IsAlias)
956
995
    GA.reset(GlobalAlias::create(Ty, AddrSpace,
957
995
                                 (GlobalValue::LinkageTypes)Linkage, Name,
958
995
                                 Aliasee, /*Parent*/ nullptr));
959
46
  else
960
46
    GA.reset(GlobalIFunc::create(Ty, AddrSpace,
961
46
                                 (GlobalValue::LinkageTypes)Linkage, Name,
962
46
                                 Aliasee, /*Parent*/ nullptr));
963
1.04k
  GA->setThreadLocalMode(TLM);
964
1.04k
  GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
965
1.04k
  GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
966
1.04k
  GA->setUnnamedAddr(UnnamedAddr);
967
1.04k
  maybeSetDSOLocal(DSOLocal, *GA);
968
1.04k
969
1.04k
  // At this point we've parsed everything except for the IndirectSymbolAttrs.
970
1.04k
  // Now parse them if there are any.
971
1.04k
  while (Lex.getKind() == lltok::comma) {
972
6
    Lex.Lex();
973
6
974
6
    if (Lex.getKind() == lltok::kw_partition) {
975
6
      Lex.Lex();
976
6
      GA->setPartition(Lex.getStrVal());
977
6
      if (ParseToken(lltok::StringConstant, "expected partition string"))
978
0
        return true;
979
0
    } else {
980
0
      return TokError("unknown alias or ifunc property!");
981
0
    }
982
6
  }
983
1.04k
984
1.04k
  if (Name.empty())
985
4
    NumberedVals.push_back(GA.get());
986
1.04k
987
1.04k
  if (GVal) {
988
67
    // Verify that types agree.
989
67
    if (GVal->getType() != GA->getType())
990
0
      return Error(
991
0
          ExplicitTypeLoc,
992
0
          "forward reference and definition of alias have different types");
993
67
994
67
    // If they agree, just RAUW the old value with the alias and remove the
995
67
    // forward ref info.
996
67
    GVal->replaceAllUsesWith(GA.get());
997
67
    GVal->eraseFromParent();
998
67
  }
999
1.04k
1000
1.04k
  // Insert into the module, we know its name won't collide now.
1001
1.04k
  if (IsAlias)
1002
995
    M->getAliasList().push_back(cast<GlobalAlias>(GA.get()));
1003
46
  else
1004
46
    M->getIFuncList().push_back(cast<GlobalIFunc>(GA.get()));
1005
1.04k
  assert(GA->getName() == Name && "Should not be a name conflict!");
1006
1.04k
1007
1.04k
  // The module owns this now
1008
1.04k
  GA.release();
1009
1.04k
1010
1.04k
  return false;
1011
1.04k
}
1012
1013
/// ParseGlobal
1014
///   ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1015
///       OptionalVisibility OptionalDLLStorageClass
1016
///       OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
1017
///       OptionalExternallyInitialized GlobalType Type Const OptionalAttrs
1018
///   ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
1019
///       OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr
1020
///       OptionalAddrSpace OptionalExternallyInitialized GlobalType Type
1021
///       Const OptionalAttrs
1022
///
1023
/// Everything up to and including OptionalUnnamedAddr has been parsed
1024
/// already.
1025
///
1026
bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
1027
                           unsigned Linkage, bool HasLinkage,
1028
                           unsigned Visibility, unsigned DLLStorageClass,
1029
                           bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
1030
30.5k
                           GlobalVariable::UnnamedAddr UnnamedAddr) {
1031
30.5k
  if (!isValidVisibilityForLinkage(Visibility, Linkage))
1032
4
    return Error(NameLoc,
1033
4
                 "symbol with local linkage must have default visibility");
1034
30.5k
1035
30.5k
  unsigned AddrSpace;
1036
30.5k
  bool IsConstant, IsExternallyInitialized;
1037
30.5k
  LocTy IsExternallyInitializedLoc;
1038
30.5k
  LocTy TyLoc;
1039
30.5k
1040
30.5k
  Type *Ty = nullptr;
1041
30.5k
  if (ParseOptionalAddrSpace(AddrSpace) ||
1042
30.5k
      ParseOptionalToken(lltok::kw_externally_initialized,
1043
30.5k
                         IsExternallyInitialized,
1044
30.5k
                         &IsExternallyInitializedLoc) ||
1045
30.5k
      ParseGlobalType(IsConstant) ||
1046
30.5k
      ParseType(Ty, TyLoc))
1047
2
    return true;
1048
30.5k
1049
30.5k
  // If the linkage is specified and is external, then no initializer is
1050
30.5k
  // present.
1051
30.5k
  Constant *Init = nullptr;
1052
30.5k
  if (!HasLinkage ||
1053
30.5k
      !GlobalValue::isValidDeclarationLinkage(
1054
23.4k
          (GlobalValue::LinkageTypes)Linkage)) {
1055
23.4k
    if (ParseGlobalValue(Ty, Init))
1056
12
      return true;
1057
30.5k
  }
1058
30.5k
1059
30.5k
  if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
1060
2
    return Error(TyLoc, "invalid type for global variable");
1061
30.5k
1062
30.5k
  GlobalValue *GVal = nullptr;
1063
30.5k
1064
30.5k
  // See if the global was forward referenced, if so, use the global.
1065
30.5k
  if (!Name.empty()) {
1066
29.8k
    GVal = M->getNamedValue(Name);
1067
29.8k
    if (GVal) {
1068
527
      if (!ForwardRefVals.erase(Name))
1069
1
        return Error(NameLoc, "redefinition of global '@" + Name + "'");
1070
685
    }
1071
685
  } else {
1072
685
    auto I = ForwardRefValIDs.find(NumberedVals.size());
1073
685
    if (I != ForwardRefValIDs.end()) {
1074
22
      GVal = I->second.first;
1075
22
      ForwardRefValIDs.erase(I);
1076
22
    }
1077
685
  }
1078
30.5k
1079
30.5k
  GlobalVariable *GV;
1080
30.5k
  if (!GVal) {
1081
29.9k
    GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
1082
29.9k
                            Name, nullptr, GlobalVariable::NotThreadLocal,
1083
29.9k
                            AddrSpace);
1084
29.9k
  } else {
1085
548
    if (GVal->getValueType() != Ty)
1086
1
      return Error(TyLoc,
1087
1
            "forward reference and definition of global have different types");
1088
547
1089
547
    GV = cast<GlobalVariable>(GVal);
1090
547
1091
547
    // Move the forward-reference to the correct spot in the module.
1092
547
    M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
1093
547
  }
1094
30.5k
1095
30.5k
  
if (30.5k
Name.empty()30.5k
)
1096
685
    NumberedVals.push_back(GV);
1097
30.5k
1098
30.5k
  // Set the parsed properties on the global.
1099
30.5k
  if (Init)
1100
23.4k
    GV->setInitializer(Init);
1101
30.5k
  GV->setConstant(IsConstant);
1102
30.5k
  GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
1103
30.5k
  maybeSetDSOLocal(DSOLocal, *GV);
1104
30.5k
  GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
1105
30.5k
  GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
1106
30.5k
  GV->setExternallyInitialized(IsExternallyInitialized);
1107
30.5k
  GV->setThreadLocalMode(TLM);
1108
30.5k
  GV->setUnnamedAddr(UnnamedAddr);
1109
30.5k
1110
30.5k
  // Parse attributes on the global.
1111
50.5k
  while (Lex.getKind() == lltok::comma) {
1112
20.0k
    Lex.Lex();
1113
20.0k
1114
20.0k
    if (Lex.getKind() == lltok::kw_section) {
1115
632
      Lex.Lex();
1116
632
      GV->setSection(Lex.getStrVal());
1117
632
      if (ParseToken(lltok::StringConstant, "expected global section string"))
1118
0
        return true;
1119
19.4k
    } else if (Lex.getKind() == lltok::kw_partition) {
1120
3
      Lex.Lex();
1121
3
      GV->setPartition(Lex.getStrVal());
1122
3
      if (ParseToken(lltok::StringConstant, "expected partition string"))
1123
0
        return true;
1124
19.3k
    } else if (Lex.getKind() == lltok::kw_align) {
1125
17.1k
      unsigned Alignment;
1126
17.1k
      if (ParseOptionalAlignment(Alignment)) 
return true0
;
1127
17.1k
      GV->setAlignment(Alignment);
1128
17.1k
    } else 
if (2.24k
Lex.getKind() == lltok::MetadataVar2.24k
) {
1129
1.73k
      if (ParseGlobalObjectMetadataAttachment(*GV))
1130
0
        return true;
1131
502
    } else {
1132
502
      Comdat *C;
1133
502
      if (parseOptionalComdat(Name, C))
1134
0
        return true;
1135
502
      if (C)
1136
502
        GV->setComdat(C);
1137
0
      else
1138
0
        return TokError("unknown global variable property!");
1139
502
    }
1140
20.0k
  }
1141
30.5k
1142
30.5k
  AttrBuilder Attrs;
1143
30.5k
  LocTy BuiltinLoc;
1144
30.5k
  std::vector<unsigned> FwdRefAttrGrps;
1145
30.5k
  if (ParseFnAttributeValuePairs(Attrs, FwdRefAttrGrps, false, BuiltinLoc))
1146
0
    return true;
1147
30.5k
  if (Attrs.hasAttributes() || 
!FwdRefAttrGrps.empty()30.5k
) {
1148
38
    GV->setAttributes(AttributeSet::get(Context, Attrs));
1149
38
    ForwardRefAttrGroups[GV] = FwdRefAttrGrps;
1150
38
  }
1151
30.5k
1152
30.5k
  return false;
1153
30.5k
}
1154
1155
/// ParseUnnamedAttrGrp
1156
///   ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
1157
10.8k
bool LLParser::ParseUnnamedAttrGrp() {
1158
10.8k
  assert(Lex.getKind() == lltok::kw_attributes);
1159
10.8k
  LocTy AttrGrpLoc = Lex.getLoc();
1160
10.8k
  Lex.Lex();
1161
10.8k
1162
10.8k
  if (Lex.getKind() != lltok::AttrGrpID)
1163
1
    return TokError("expected attribute group id");
1164
10.8k
1165
10.8k
  unsigned VarID = Lex.getUIntVal();
1166
10.8k
  std::vector<unsigned> unused;
1167
10.8k
  LocTy BuiltinLoc;
1168
10.8k
  Lex.Lex();
1169
10.8k
1170
10.8k
  if (ParseToken(lltok::equal, "expected '=' here") ||
1171
10.8k
      ParseToken(lltok::lbrace, "expected '{' here") ||
1172
10.8k
      ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
1173
10.8k
                                 BuiltinLoc) ||
1174
10.8k
      ParseToken(lltok::rbrace, "expected end of attribute group"))
1175
0
    return true;
1176
10.8k
1177
10.8k
  if (!NumberedAttrBuilders[VarID].hasAttributes())
1178
0
    return Error(AttrGrpLoc, "attribute group has no attributes");
1179
10.8k
1180
10.8k
  return false;
1181
10.8k
}
1182
1183
/// ParseFnAttributeValuePairs
1184
///   ::= <attr> | <attr> '=' <value>
1185
bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
1186
                                          std::vector<unsigned> &FwdRefAttrGrps,
1187
611k
                                          bool inAttrGrp, LocTy &BuiltinLoc) {
1188
611k
  bool HaveError = false;
1189
611k
1190
611k
  B.clear();
1191
611k
1192
838k
  while (true) {
1193
838k
    lltok::Kind Token = Lex.getKind();
1194
838k
    if (Token == lltok::kw_builtin)
1195
62
      BuiltinLoc = Lex.getLoc();
1196
838k
    switch (Token) {
1197
838k
    default:
1198
600k
      if (!inAttrGrp) return HaveError;
1199
0
      return Error(Lex.getLoc(), "unterminated attribute group");
1200
10.8k
    case lltok::rbrace:
1201
10.8k
      // Finished.
1202
10.8k
      return false;
1203
0
1204
66.3k
    case lltok::AttrGrpID: {
1205
66.3k
      // Allow a function to reference an attribute group:
1206
66.3k
      //
1207
66.3k
      //   define void @foo() #1 { ... }
1208
66.3k
      if (inAttrGrp)
1209
0
        HaveError |=
1210
0
          Error(Lex.getLoc(),
1211
0
              "cannot have an attribute group reference in an attribute group");
1212
66.3k
1213
66.3k
      unsigned AttrGrpNum = Lex.getUIntVal();
1214
66.3k
      if (inAttrGrp) 
break0
;
1215
66.3k
1216
66.3k
      // Save the reference to the attribute group. We'll fill it in later.
1217
66.3k
      FwdRefAttrGrps.push_back(AttrGrpNum);
1218
66.3k
      break;
1219
66.3k
    }
1220
66.3k
    // Target-dependent attributes:
1221
66.3k
    case lltok::StringConstant: {
1222
21.9k
      if (ParseStringAttribute(B))
1223
0
        return true;
1224
21.9k
      continue;
1225
21.9k
    }
1226
21.9k
1227
21.9k
    // Target-independent attributes:
1228
21.9k
    case lltok::kw_align: {
1229
699
      // As a hack, we allow function alignment to be initially parsed as an
1230
699
      // attribute on a function declaration/definition or added to an attribute
1231
699
      // group and later moved to the alignment field.
1232
699
      unsigned Alignment;
1233
699
      if (inAttrGrp) {
1234
1
        Lex.Lex();
1235
1
        if (ParseToken(lltok::equal, "expected '=' here") ||
1236
1
            ParseUInt32(Alignment))
1237
0
          return true;
1238
698
      } else {
1239
698
        if (ParseOptionalAlignment(Alignment))
1240
0
          return true;
1241
699
      }
1242
699
      B.addAlignmentAttr(Alignment);
1243
699
      continue;
1244
699
    }
1245
699
    case lltok::kw_alignstack: {
1246
50
      unsigned Alignment;
1247
50
      if (inAttrGrp) {
1248
16
        Lex.Lex();
1249
16
        if (ParseToken(lltok::equal, "expected '=' here") ||
1250
16
            ParseUInt32(Alignment))
1251
0
          return true;
1252
34
      } else {
1253
34
        if (ParseOptionalStackAlignment(Alignment))
1254
0
          return true;
1255
50
      }
1256
50
      B.addStackAlignmentAttr(Alignment);
1257
50
      continue;
1258
50
    }
1259
50
    case lltok::kw_allocsize: {
1260
25
      unsigned ElemSizeArg;
1261
25
      Optional<unsigned> NumElemsArg;
1262
25
      // inAttrGrp doesn't matter; we only support allocsize(a[, b])
1263
25
      if (parseAllocSizeArguments(ElemSizeArg, NumElemsArg))
1264
1
        return true;
1265
24
      B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1266
24
      continue;
1267
24
    }
1268
268
    case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
1269
434
    case lltok::kw_argmemonly: B.addAttribute(Attribute::ArgMemOnly); break;
1270
62
    case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
1271
69
    case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
1272
310
    case lltok::kw_convergent: B.addAttribute(Attribute::Convergent); break;
1273
81
    case lltok::kw_inaccessiblememonly:
1274
81
      B.addAttribute(Attribute::InaccessibleMemOnly); break;
1275
24
    case lltok::kw_inaccessiblemem_or_argmemonly:
1276
19
      B.addAttribute(Attribute::InaccessibleMemOrArgMemOnly); break;
1277
183
    case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
1278
24
    
case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break9
;
1279
814
    case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
1280
29
    case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
1281
140
    case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
1282
66
    case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
1283
24
    
case lltok::kw_nofree: B.addAttribute(Attribute::NoFree); break10
;
1284
57
    case lltok::kw_noimplicitfloat:
1285
57
      B.addAttribute(Attribute::NoImplicitFloat); break;
1286
1.43k
    case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
1287
44
    case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
1288
219
    case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
1289
600
    case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
1290
24
    
case lltok::kw_nosync: B.addAttribute(Attribute::NoSync); break8
;
1291
24
    
case lltok::kw_nocf_check: B.addAttribute(Attribute::NoCfCheck); break2
;
1292
445
    case lltok::kw_norecurse: B.addAttribute(Attribute::NoRecurse); break;
1293
95.1k
    case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
1294
24
    case lltok::kw_optforfuzzing:
1295
1
      B.addAttribute(Attribute::OptForFuzzing); break;
1296
235
    case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
1297
1.96k
    case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
1298
19.2k
    case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1299
2.42k
    case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
1300
91
    case lltok::kw_returns_twice:
1301
91
      B.addAttribute(Attribute::ReturnsTwice); break;
1302
509
    case lltok::kw_speculatable: B.addAttribute(Attribute::Speculatable); break;
1303
5.94k
    case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
1304
78
    case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
1305
90
    case lltok::kw_sspstrong:
1306
90
      B.addAttribute(Attribute::StackProtectStrong); break;
1307
199
    case lltok::kw_safestack: B.addAttribute(Attribute::SafeStack); break;
1308
24
    case lltok::kw_shadowcallstack:
1309
11
      B.addAttribute(Attribute::ShadowCallStack); break;
1310
382
    case lltok::kw_sanitize_address:
1311
382
      B.addAttribute(Attribute::SanitizeAddress); break;
1312
210
    case lltok::kw_sanitize_hwaddress:
1313
210
      B.addAttribute(Attribute::SanitizeHWAddress); break;
1314
24
    case lltok::kw_sanitize_memtag:
1315
19
      B.addAttribute(Attribute::SanitizeMemTag); break;
1316
73
    case lltok::kw_sanitize_thread:
1317
73
      B.addAttribute(Attribute::SanitizeThread); break;
1318
708
    case lltok::kw_sanitize_memory:
1319
708
      B.addAttribute(Attribute::SanitizeMemory); break;
1320
59
    case lltok::kw_speculative_load_hardening:
1321
59
      B.addAttribute(Attribute::SpeculativeLoadHardening);
1322
59
      break;
1323
24
    
case lltok::kw_strictfp: B.addAttribute(Attribute::StrictFP); break6
;
1324
5.37k
    case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
1325
24
    
case lltok::kw_willreturn: B.addAttribute(Attribute::WillReturn); break11
;
1326
47
    case lltok::kw_writeonly: B.addAttribute(Attribute::WriteOnly); break;
1327
24
1328
24
    // Error handling.
1329
24
    case lltok::kw_inreg:
1330
0
    case lltok::kw_signext:
1331
0
    case lltok::kw_zeroext:
1332
0
      HaveError |=
1333
0
        Error(Lex.getLoc(),
1334
0
              "invalid use of attribute on a function");
1335
0
      break;
1336
1
    case lltok::kw_byval:
1337
1
    case lltok::kw_dereferenceable:
1338
1
    case lltok::kw_dereferenceable_or_null:
1339
1
    case lltok::kw_inalloca:
1340
1
    case lltok::kw_nest:
1341
1
    case lltok::kw_noalias:
1342
1
    case lltok::kw_nocapture:
1343
1
    case lltok::kw_nonnull:
1344
1
    case lltok::kw_returned:
1345
1
    case lltok::kw_sret:
1346
1
    case lltok::kw_swifterror:
1347
1
    case lltok::kw_swiftself:
1348
1
    case lltok::kw_immarg:
1349
1
      HaveError |=
1350
1
        Error(Lex.getLoc(),
1351
1
              "invalid use of parameter-only attribute on a function");
1352
1
      break;
1353
204k
    }
1354
204k
1355
204k
    Lex.Lex();
1356
204k
  }
1357
611k
}
1358
1359
//===----------------------------------------------------------------------===//
1360
// GlobalValue Reference/Resolution Routines.
1361
//===----------------------------------------------------------------------===//
1362
1363
static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy,
1364
51.2k
                                              const std::string &Name) {
1365
51.2k
  if (auto *FT = dyn_cast<FunctionType>(PTy->getElementType()))
1366
50.6k
    return Function::Create(FT, GlobalValue::ExternalWeakLinkage,
1367
50.6k
                            PTy->getAddressSpace(), Name, M);
1368
581
  else
1369
581
    return new GlobalVariable(*M, PTy->getElementType(), false,
1370
581
                              GlobalValue::ExternalWeakLinkage, nullptr, Name,
1371
581
                              nullptr, GlobalVariable::NotThreadLocal,
1372
581
                              PTy->getAddressSpace());
1373
51.2k
}
1374
1375
Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
1376
2.29M
                                        Value *Val, bool IsCall) {
1377
2.29M
  if (Val->getType() == Ty)
1378
2.29M
    return Val;
1379
15
  // For calls we also accept variables in the program address space.
1380
15
  Type *SuggestedTy = Ty;
1381
15
  if (IsCall && 
isa<PointerType>(Ty)8
) {
1382
8
    Type *TyInProgAS = cast<PointerType>(Ty)->getElementType()->getPointerTo(
1383
8
        M->getDataLayout().getProgramAddressSpace());
1384
8
    SuggestedTy = TyInProgAS;
1385
8
    if (Val->getType() == TyInProgAS)
1386
0
      return Val;
1387
15
  }
1388
15
  if (Ty->isLabelTy())
1389
0
    Error(Loc, "'" + Name + "' is not a basic block");
1390
15
  else
1391
15
    Error(Loc, "'" + Name + "' defined with type '" +
1392
15
                   getTypeString(Val->getType()) + "' but expected '" +
1393
15
                   getTypeString(SuggestedTy) + "'");
1394
15
  return nullptr;
1395
15
}
1396
1397
/// GetGlobalVal - Get a value with the specified name or ID, creating a
1398
/// forward reference record if needed.  This can return null if the value
1399
/// exists but does not have the right type.
1400
GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
1401
297k
                                    LocTy Loc, bool IsCall) {
1402
297k
  PointerType *PTy = dyn_cast<PointerType>(Ty);
1403
297k
  if (!PTy) {
1404
0
    Error(Loc, "global variable reference must have pointer type");
1405
0
    return nullptr;
1406
0
  }
1407
297k
1408
297k
  // Look this name up in the normal function symbol table.
1409
297k
  GlobalValue *Val =
1410
297k
    cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
1411
297k
1412
297k
  // If this is a forward reference for the value, see if we already created a
1413
297k
  // forward ref record.
1414
297k
  if (!Val) {
1415
51.1k
    auto I = ForwardRefVals.find(Name);
1416
51.1k
    if (I != ForwardRefVals.end())
1417
0
      Val = I->second.first;
1418
51.1k
  }
1419
297k
1420
297k
  // If we have the value in the symbol table or fwd-ref table, return it.
1421
297k
  if (Val)
1422
246k
    return cast_or_null<GlobalValue>(
1423
246k
        checkValidVariableType(Loc, "@" + Name, Ty, Val, IsCall));
1424
51.1k
1425
51.1k
  // Otherwise, create a new forward reference for this value and remember it.
1426
51.1k
  GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, Name);
1427
51.1k
  ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1428
51.1k
  return FwdVal;
1429
51.1k
}
1430
1431
GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc,
1432
665
                                    bool IsCall) {
1433
665
  PointerType *PTy = dyn_cast<PointerType>(Ty);
1434
665
  if (!PTy) {
1435
0
    Error(Loc, "global variable reference must have pointer type");
1436
0
    return nullptr;
1437
0
  }
1438
665
1439
665
  GlobalValue *Val = ID < NumberedVals.size() ? 
NumberedVals[ID]613
:
nullptr52
;
1440
665
1441
665
  // If this is a forward reference for the value, see if we already created a
1442
665
  // forward ref record.
1443
665
  if (!Val) {
1444
52
    auto I = ForwardRefValIDs.find(ID);
1445
52
    if (I != ForwardRefValIDs.end())
1446
8
      Val = I->second.first;
1447
52
  }
1448
665
1449
665
  // If we have the value in the symbol table or fwd-ref table, return it.
1450
665
  if (Val)
1451
621
    return cast_or_null<GlobalValue>(
1452
621
        checkValidVariableType(Loc, "@" + Twine(ID), Ty, Val, IsCall));
1453
44
1454
44
  // Otherwise, create a new forward reference for this value and remember it.
1455
44
  GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, "");
1456
44
  ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1457
44
  return FwdVal;
1458
44
}
1459
1460
//===----------------------------------------------------------------------===//
1461
// Comdat Reference/Resolution Routines.
1462
//===----------------------------------------------------------------------===//
1463
1464
906
Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1465
906
  // Look this name up in the comdat symbol table.
1466
906
  Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1467
906
  Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1468
906
  if (I != ComdatSymTab.end())
1469
904
    return &I->second;
1470
2
1471
2
  // Otherwise, create a new forward reference for this value and remember it.
1472
2
  Comdat *C = M->getOrInsertComdat(Name);
1473
2
  ForwardRefComdats[Name] = Loc;
1474
2
  return C;
1475
2
}
1476
1477
//===----------------------------------------------------------------------===//
1478
// Helper Routines.
1479
//===----------------------------------------------------------------------===//
1480
1481
/// ParseToken - If the current token has the specified kind, eat it and return
1482
/// success.  Otherwise, emit the specified error and return failure.
1483
8.10M
bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1484
8.10M
  if (Lex.getKind() != T)
1485
6
    return TokError(ErrMsg);
1486
8.10M
  Lex.Lex();
1487
8.10M
  return false;
1488
8.10M
}
1489
1490
/// ParseStringConstant
1491
///   ::= StringConstant
1492
105k
bool LLParser::ParseStringConstant(std::string &Result) {
1493
105k
  if (Lex.getKind() != lltok::StringConstant)
1494
0
    return TokError("expected string constant");
1495
105k
  Result = Lex.getStrVal();
1496
105k
  Lex.Lex();
1497
105k
  return false;
1498
105k
}
1499
1500
/// ParseUInt32
1501
///   ::= uint32
1502
669k
bool LLParser::ParseUInt32(uint32_t &Val) {
1503
669k
  if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1504
0
    return TokError("expected integer");
1505
669k
  uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1506
669k
  if (Val64 != unsigned(Val64))
1507
0
    return TokError("expected 32-bit integer (too large)");
1508
669k
  Val = Val64;
1509
669k
  Lex.Lex();
1510
669k
  return false;
1511
669k
}
1512
1513
/// ParseUInt64
1514
///   ::= uint64
1515
895
bool LLParser::ParseUInt64(uint64_t &Val) {
1516
895
  if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1517
0
    return TokError("expected integer");
1518
895
  Val = Lex.getAPSIntVal().getLimitedValue();
1519
895
  Lex.Lex();
1520
895
  return false;
1521
895
}
1522
1523
/// ParseTLSModel
1524
///   := 'localdynamic'
1525
///   := 'initialexec'
1526
///   := 'localexec'
1527
187
bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1528
187
  switch (Lex.getKind()) {
1529
187
    default:
1530
0
      return TokError("expected localdynamic, initialexec or localexec");
1531
187
    case lltok::kw_localdynamic:
1532
59
      TLM = GlobalVariable::LocalDynamicTLSModel;
1533
59
      break;
1534
187
    case lltok::kw_initialexec:
1535
65
      TLM = GlobalVariable::InitialExecTLSModel;
1536
65
      break;
1537
187
    case lltok::kw_localexec:
1538
63
      TLM = GlobalVariable::LocalExecTLSModel;
1539
63
      break;
1540
187
  }
1541
187
1542
187
  Lex.Lex();
1543
187
  return false;
1544
187
}
1545
1546
/// ParseOptionalThreadLocal
1547
///   := /*empty*/
1548
///   := 'thread_local'
1549
///   := 'thread_local' '(' tlsmodel ')'
1550
31.5k
bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1551
31.5k
  TLM = GlobalVariable::NotThreadLocal;
1552
31.5k
  if (!EatIfPresent(lltok::kw_thread_local))
1553
30.6k
    return false;
1554
946
1555
946
  TLM = GlobalVariable::GeneralDynamicTLSModel;
1556
946
  if (Lex.getKind() == lltok::lparen) {
1557
187
    Lex.Lex();
1558
187
    return ParseTLSModel(TLM) ||
1559
187
      ParseToken(lltok::rparen, "expected ')' after thread local model");
1560
187
  }
1561
759
  return false;
1562
759
}
1563
1564
/// ParseOptionalAddrSpace
1565
///   := /*empty*/
1566
///   := 'addrspace' '(' uint32 ')'
1567
716k
bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) {
1568
716k
  AddrSpace = DefaultAS;
1569
716k
  if (!EatIfPresent(lltok::kw_addrspace))
1570
599k
    return false;
1571
116k
  return ParseToken(lltok::lparen, "expected '(' in address space") ||
1572
116k
         ParseUInt32(AddrSpace) ||
1573
116k
         ParseToken(lltok::rparen, "expected ')' in address space");
1574
116k
}
1575
1576
/// ParseStringAttribute
1577
///   := StringConstant
1578
///   := StringConstant '=' StringConstant
1579
22.0k
bool LLParser::ParseStringAttribute(AttrBuilder &B) {
1580
22.0k
  std::string Attr = Lex.getStrVal();
1581
22.0k
  Lex.Lex();
1582
22.0k
  std::string Val;
1583
22.0k
  if (EatIfPresent(lltok::equal) && 
ParseStringConstant(Val)20.7k
)
1584
0
    return true;
1585
22.0k
  B.addAttribute(Attr, Val);
1586
22.0k
  return false;
1587
22.0k
}
1588
1589
/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1590
1.04M
bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1591
1.04M
  bool HaveError = false;
1592
1.04M
1593
1.04M
  B.clear();
1594
1.04M
1595
1.10M
  while (true) {
1596
1.10M
    lltok::Kind Token = Lex.getKind();
1597
1.10M
    switch (Token) {
1598
1.10M
    default:  // End of attributes.
1599
1.04M
      return HaveError;
1600
1.10M
    case lltok::StringConstant: {
1601
14
      if (ParseStringAttribute(B))
1602
0
        return true;
1603
14
      continue;
1604
14
    }
1605
6.07k
    case lltok::kw_align: {
1606
6.07k
      unsigned Alignment;
1607
6.07k
      if (ParseOptionalAlignment(Alignment))
1608
1
        return true;
1609
6.07k
      B.addAlignmentAttr(Alignment);
1610
6.07k
      continue;
1611
6.07k
    }
1612
6.07k
    case lltok::kw_byval: {
1613
1.77k
      Type *Ty;
1614
1.77k
      if (ParseByValWithOptionalType(Ty))
1615
1
        return true;
1616
1.77k
      B.addByValAttr(Ty);
1617
1.77k
      continue;
1618
1.77k
    }
1619
1.77k
    case lltok::kw_dereferenceable: {
1620
615
      uint64_t Bytes;
1621
615
      if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
1622
0
        return true;
1623
615
      B.addDereferenceableAttr(Bytes);
1624
615
      continue;
1625
615
    }
1626
615
    case lltok::kw_dereferenceable_or_null: {
1627
54
      uint64_t Bytes;
1628
54
      if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1629
0
        return true;
1630
54
      B.addDereferenceableOrNullAttr(Bytes);
1631
54
      continue;
1632
54
    }
1633
139
    case lltok::kw_inalloca:        B.addAttribute(Attribute::InAlloca); break;
1634
5.82k
    case lltok::kw_inreg:           B.addAttribute(Attribute::InReg); break;
1635
95
    case lltok::kw_nest:            B.addAttribute(Attribute::Nest); break;
1636
5.51k
    case lltok::kw_noalias:         B.addAttribute(Attribute::NoAlias); break;
1637
17.4k
    case lltok::kw_nocapture:       B.addAttribute(Attribute::NoCapture); break;
1638
2.04k
    case lltok::kw_nonnull:         B.addAttribute(Attribute::NonNull); break;
1639
342
    case lltok::kw_readnone:        B.addAttribute(Attribute::ReadNone); break;
1640
5.56k
    case lltok::kw_readonly:        B.addAttribute(Attribute::ReadOnly); break;
1641
333
    case lltok::kw_returned:        B.addAttribute(Attribute::Returned); break;
1642
10.1k
    case lltok::kw_signext:         B.addAttribute(Attribute::SExt); break;
1643
1.45k
    case lltok::kw_sret:            B.addAttribute(Attribute::StructRet); break;
1644
363
    case lltok::kw_swifterror:      B.addAttribute(Attribute::SwiftError); break;
1645
210
    case lltok::kw_swiftself:       B.addAttribute(Attribute::SwiftSelf); break;
1646
322
    case lltok::kw_writeonly:       B.addAttribute(Attribute::WriteOnly); break;
1647
7.77k
    case lltok::kw_zeroext:         B.addAttribute(Attribute::ZExt); break;
1648
413
    case lltok::kw_immarg:          B.addAttribute(Attribute::ImmArg); break;
1649
54
1650
54
    case lltok::kw_alignstack:
1651
1
    case lltok::kw_alwaysinline:
1652
1
    case lltok::kw_argmemonly:
1653
1
    case lltok::kw_builtin:
1654
1
    case lltok::kw_inlinehint:
1655
1
    case lltok::kw_jumptable:
1656
1
    case lltok::kw_minsize:
1657
1
    case lltok::kw_naked:
1658
1
    case lltok::kw_nobuiltin:
1659
1
    case lltok::kw_noduplicate:
1660
1
    case lltok::kw_noimplicitfloat:
1661
1
    case lltok::kw_noinline:
1662
1
    case lltok::kw_nonlazybind:
1663
1
    case lltok::kw_noredzone:
1664
1
    case lltok::kw_noreturn:
1665
1
    case lltok::kw_nocf_check:
1666
1
    case lltok::kw_nounwind:
1667
1
    case lltok::kw_optforfuzzing:
1668
1
    case lltok::kw_optnone:
1669
1
    case lltok::kw_optsize:
1670
1
    case lltok::kw_returns_twice:
1671
1
    case lltok::kw_sanitize_address:
1672
1
    case lltok::kw_sanitize_hwaddress:
1673
1
    case lltok::kw_sanitize_memtag:
1674
1
    case lltok::kw_sanitize_memory:
1675
1
    case lltok::kw_sanitize_thread:
1676
1
    case lltok::kw_speculative_load_hardening:
1677
1
    case lltok::kw_ssp:
1678
1
    case lltok::kw_sspreq:
1679
1
    case lltok::kw_sspstrong:
1680
1
    case lltok::kw_safestack:
1681
1
    case lltok::kw_shadowcallstack:
1682
1
    case lltok::kw_strictfp:
1683
1
    case lltok::kw_uwtable:
1684
1
      HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1685
1
      break;
1686
57.9k
    }
1687
57.9k
1688
57.9k
    Lex.Lex();
1689
57.9k
  }
1690
1.04M
}
1691
1692
/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1693
569k
bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1694
569k
  bool HaveError = false;
1695
569k
1696
569k
  B.clear();
1697
569k
1698
580k
  while (true) {
1699
580k
    lltok::Kind Token = Lex.getKind();
1700
580k
    switch (Token) {
1701
580k
    default:  // End of attributes.
1702
569k
      return HaveError;
1703
580k
    case lltok::StringConstant: {
1704
16
      if (ParseStringAttribute(B))
1705
0
        return true;
1706
16
      continue;
1707
16
    }
1708
80
    case lltok::kw_dereferenceable: {
1709
80
      uint64_t Bytes;
1710
80
      if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
1711
0
        return true;
1712
80
      B.addDereferenceableAttr(Bytes);
1713
80
      continue;
1714
80
    }
1715
80
    case lltok::kw_dereferenceable_or_null: {
1716
14
      uint64_t Bytes;
1717
14
      if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1718
0
        return true;
1719
14
      B.addDereferenceableOrNullAttr(Bytes);
1720
14
      continue;
1721
14
    }
1722
14
    case lltok::kw_align: {
1723
5
      unsigned Alignment;
1724
5
      if (ParseOptionalAlignment(Alignment))
1725
0
        return true;
1726
5
      B.addAlignmentAttr(Alignment);
1727
5
      continue;
1728
5
    }
1729
88
    case lltok::kw_inreg:           B.addAttribute(Attribute::InReg); break;
1730
577
    case lltok::kw_noalias:         B.addAttribute(Attribute::NoAlias); break;
1731
80
    case lltok::kw_nonnull:         B.addAttribute(Attribute::NonNull); break;
1732
5.23k
    case lltok::kw_signext:         B.addAttribute(Attribute::SExt); break;
1733
4.63k
    case lltok::kw_zeroext:         B.addAttribute(Attribute::ZExt); break;
1734
5
1735
5
    // Error handling.
1736
5
    case lltok::kw_byval:
1737
2
    case lltok::kw_inalloca:
1738
2
    case lltok::kw_nest:
1739
2
    case lltok::kw_nocapture:
1740
2
    case lltok::kw_returned:
1741
2
    case lltok::kw_sret:
1742
2
    case lltok::kw_swifterror:
1743
2
    case lltok::kw_swiftself:
1744
2
    case lltok::kw_immarg:
1745
2
      HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
1746
2
      break;
1747
2
1748
2
    case lltok::kw_alignstack:
1749
1
    case lltok::kw_alwaysinline:
1750
1
    case lltok::kw_argmemonly:
1751
1
    case lltok::kw_builtin:
1752
1
    case lltok::kw_cold:
1753
1
    case lltok::kw_inlinehint:
1754
1
    case lltok::kw_jumptable:
1755
1
    case lltok::kw_minsize:
1756
1
    case lltok::kw_naked:
1757
1
    case lltok::kw_nobuiltin:
1758
1
    case lltok::kw_noduplicate:
1759
1
    case lltok::kw_noimplicitfloat:
1760
1
    case lltok::kw_noinline:
1761
1
    case lltok::kw_nonlazybind:
1762
1
    case lltok::kw_noredzone:
1763
1
    case lltok::kw_noreturn:
1764
1
    case lltok::kw_nocf_check:
1765
1
    case lltok::kw_nounwind:
1766
1
    case lltok::kw_optforfuzzing:
1767
1
    case lltok::kw_optnone:
1768
1
    case lltok::kw_optsize:
1769
1
    case lltok::kw_returns_twice:
1770
1
    case lltok::kw_sanitize_address:
1771
1
    case lltok::kw_sanitize_hwaddress:
1772
1
    case lltok::kw_sanitize_memtag:
1773
1
    case lltok::kw_sanitize_memory:
1774
1
    case lltok::kw_sanitize_thread:
1775
1
    case lltok::kw_speculative_load_hardening:
1776
1
    case lltok::kw_ssp:
1777
1
    case lltok::kw_sspreq:
1778
1
    case lltok::kw_sspstrong:
1779
1
    case lltok::kw_safestack:
1780
1
    case lltok::kw_shadowcallstack:
1781
1
    case lltok::kw_strictfp:
1782
1
    case lltok::kw_uwtable:
1783
1
      HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1784
1
      break;
1785
1
1786
1
    case lltok::kw_readnone:
1787
0
    case lltok::kw_readonly:
1788
0
      HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
1789
580k
    }
1790
580k
1791
580k
    Lex.Lex();
1792
10.6k
  }
1793
569k
}
1794
1795
410k
static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) {
1796
410k
  HasLinkage = true;
1797
410k
  switch (Kind) {
1798
410k
  default:
1799
388k
    HasLinkage = false;
1800
388k
    return GlobalValue::ExternalLinkage;
1801
410k
  case lltok::kw_private:
1802
2.10k
    return GlobalValue::PrivateLinkage;
1803
410k
  case lltok::kw_internal:
1804
3.98k
    return GlobalValue::InternalLinkage;
1805
410k
  case lltok::kw_weak:
1806
696
    return GlobalValue::WeakAnyLinkage;
1807
410k
  case lltok::kw_weak_odr:
1808
324
    return GlobalValue::WeakODRLinkage;
1809
410k
  case lltok::kw_linkonce:
1810
137
    return GlobalValue::LinkOnceAnyLinkage;
1811
410k
  case lltok::kw_linkonce_odr:
1812
1.02k
    return GlobalValue::LinkOnceODRLinkage;
1813
410k
  case lltok::kw_available_externally:
1814
142
    return GlobalValue::AvailableExternallyLinkage;
1815
410k
  case lltok::kw_appending:
1816
372
    return GlobalValue::AppendingLinkage;
1817
410k
  case lltok::kw_common:
1818
6.16k
    return GlobalValue::CommonLinkage;
1819
410k
  case lltok::kw_extern_weak:
1820
143
    return GlobalValue::ExternalWeakLinkage;
1821
410k
  case lltok::kw_external:
1822
7.08k
    return GlobalValue::ExternalLinkage;
1823
410k
  }
1824
410k
}
1825
1826
/// ParseOptionalLinkage
1827
///   ::= /*empty*/
1828
///   ::= 'private'
1829
///   ::= 'internal'
1830
///   ::= 'weak'
1831
///   ::= 'weak_odr'
1832
///   ::= 'linkonce'
1833
///   ::= 'linkonce_odr'
1834
///   ::= 'available_externally'
1835
///   ::= 'appending'
1836
///   ::= 'common'
1837
///   ::= 'extern_weak'
1838
///   ::= 'external'
1839
bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage,
1840
                                    unsigned &Visibility,
1841
                                    unsigned &DLLStorageClass,
1842
410k
                                    bool &DSOLocal) {
1843
410k
  Res = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
1844
410k
  if (HasLinkage)
1845
22.0k
    Lex.Lex();
1846
410k
  ParseOptionalDSOLocal(DSOLocal);
1847
410k
  ParseOptionalVisibility(Visibility);
1848
410k
  ParseOptionalDLLStorageClass(DLLStorageClass);
1849
410k
1850
410k
  if (DSOLocal && 
DLLStorageClass == GlobalValue::DLLImportStorageClass2.89k
) {
1851
1
    return Error(Lex.getLoc(), "dso_location and DLL-StorageClass mismatch");
1852
1
  }
1853
410k
1854
410k
  return false;
1855
410k
}
1856
1857
410k
void LLParser::ParseOptionalDSOLocal(bool &DSOLocal) {
1858
410k
  switch (Lex.getKind()) {
1859
410k
  default:
1860
407k
    DSOLocal = false;
1861
407k
    break;
1862
410k
  case lltok::kw_dso_local:
1863
2.89k
    DSOLocal = true;
1864
2.89k
    Lex.Lex();
1865
2.89k
    break;
1866
410k
  case lltok::kw_dso_preemptable:
1867
150
    DSOLocal = false;
1868
150
    Lex.Lex();
1869
150
    break;
1870
410k
  }
1871
410k
}
1872
1873
/// ParseOptionalVisibility
1874
///   ::= /*empty*/
1875
///   ::= 'default'
1876
///   ::= 'hidden'
1877
///   ::= 'protected'
1878
///
1879
410k
void LLParser::ParseOptionalVisibility(unsigned &Res) {
1880
410k
  switch (Lex.getKind()) {
1881
410k
  default:
1882
408k
    Res = GlobalValue::DefaultVisibility;
1883
408k
    return;
1884
410k
  case lltok::kw_default:
1885
20
    Res = GlobalValue::DefaultVisibility;
1886
20
    break;
1887
410k
  case lltok::kw_hidden:
1888
1.50k
    Res = GlobalValue::HiddenVisibility;
1889
1.50k
    break;
1890
410k
  case lltok::kw_protected:
1891
112
    Res = GlobalValue::ProtectedVisibility;
1892
112
    break;
1893
1.63k
  }
1894
1.63k
  Lex.Lex();
1895
1.63k
}
1896
1897
/// ParseOptionalDLLStorageClass
1898
///   ::= /*empty*/
1899
///   ::= 'dllimport'
1900
///   ::= 'dllexport'
1901
///
1902
410k
void LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1903
410k
  switch (Lex.getKind()) {
1904
410k
  default:
1905
409k
    Res = GlobalValue::DefaultStorageClass;
1906
409k
    return;
1907
410k
  case lltok::kw_dllimport:
1908
146
    Res = GlobalValue::DLLImportStorageClass;
1909
146
    break;
1910
410k
  case lltok::kw_dllexport:
1911
323
    Res = GlobalValue::DLLExportStorageClass;
1912
323
    break;
1913
469
  }
1914
469
  Lex.Lex();
1915
469
}
1916
1917
/// ParseOptionalCallingConv
1918
///   ::= /*empty*/
1919
///   ::= 'ccc'
1920
///   ::= 'fastcc'
1921
///   ::= 'intel_ocl_bicc'
1922
///   ::= 'coldcc'
1923
///   ::= 'x86_stdcallcc'
1924
///   ::= 'x86_fastcallcc'
1925
///   ::= 'x86_thiscallcc'
1926
///   ::= 'x86_vectorcallcc'
1927
///   ::= 'arm_apcscc'
1928
///   ::= 'arm_aapcscc'
1929
///   ::= 'arm_aapcs_vfpcc'
1930
///   ::= 'aarch64_vector_pcs'
1931
///   ::= 'msp430_intrcc'
1932
///   ::= 'avr_intrcc'
1933
///   ::= 'avr_signalcc'
1934
///   ::= 'ptx_kernel'
1935
///   ::= 'ptx_device'
1936
///   ::= 'spir_func'
1937
///   ::= 'spir_kernel'
1938
///   ::= 'x86_64_sysvcc'
1939
///   ::= 'win64cc'
1940
///   ::= 'webkit_jscc'
1941
///   ::= 'anyregcc'
1942
///   ::= 'preserve_mostcc'
1943
///   ::= 'preserve_allcc'
1944
///   ::= 'ghccc'
1945
///   ::= 'swiftcc'
1946
///   ::= 'x86_intrcc'
1947
///   ::= 'hhvmcc'
1948
///   ::= 'hhvm_ccc'
1949
///   ::= 'cxx_fast_tlscc'
1950
///   ::= 'amdgpu_vs'
1951
///   ::= 'amdgpu_ls'
1952
///   ::= 'amdgpu_hs'
1953
///   ::= 'amdgpu_es'
1954
///   ::= 'amdgpu_gs'
1955
///   ::= 'amdgpu_ps'
1956
///   ::= 'amdgpu_cs'
1957
///   ::= 'amdgpu_kernel'
1958
///   ::= 'cc' UINT
1959
///
1960
569k
bool LLParser::ParseOptionalCallingConv(unsigned &CC) {
1961
569k
  switch (Lex.getKind()) {
1962
569k
  
default: CC = CallingConv::C; return false535k
;
1963
569k
  
case lltok::kw_ccc: CC = CallingConv::C; break18
;
1964
569k
  
case lltok::kw_fastcc: CC = CallingConv::Fast; break1.97k
;
1965
569k
  
case lltok::kw_coldcc: CC = CallingConv::Cold; break125
;
1966
569k
  
case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break88
;
1967
569k
  
case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break108
;
1968
569k
  
case lltok::kw_x86_regcallcc: CC = CallingConv::X86_RegCall; break200
;
1969
569k
  
case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break348
;
1970
569k
  
case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break71
;
1971
569k
  
case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break40
;
1972
569k
  
case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break800
;
1973
569k
  
case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break1.47k
;
1974
569k
  
case lltok::kw_aarch64_vector_pcs:CC = CallingConv::AArch64_VectorCall; break17
;
1975
569k
  
case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break14
;
1976
569k
  
case lltok::kw_avr_intrcc: CC = CallingConv::AVR_INTR; break3
;
1977
569k
  
case lltok::kw_avr_signalcc: CC = CallingConv::AVR_SIGNAL; break3
;
1978
569k
  
case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break21
;
1979
569k
  
case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break185
;
1980
569k
  
case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break23
;
1981
569k
  
case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break73
;
1982
569k
  
case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break92
;
1983
569k
  
case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break11
;
1984
569k
  
case lltok::kw_win64cc: CC = CallingConv::Win64; break46
;
1985
569k
  
case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break33
;
1986
569k
  
case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break84
;
1987
569k
  
case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break15
;
1988
569k
  
case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break11
;
1989
569k
  
case lltok::kw_ghccc: CC = CallingConv::GHC; break58
;
1990
569k
  
case lltok::kw_swiftcc: CC = CallingConv::Swift; break471
;
1991
569k
  
case lltok::kw_x86_intrcc: CC = CallingConv::X86_INTR; break42
;
1992
569k
  
case lltok::kw_hhvmcc: CC = CallingConv::HHVM; break27
;
1993
569k
  
case lltok::kw_hhvm_ccc: CC = CallingConv::HHVM_C; break16
;
1994
569k
  
case lltok::kw_cxx_fast_tlscc: CC = CallingConv::CXX_FAST_TLS; break108
;
1995
569k
  
case lltok::kw_amdgpu_vs: CC = CallingConv::AMDGPU_VS; break236
;
1996
569k
  
case lltok::kw_amdgpu_ls: CC = CallingConv::AMDGPU_LS; break8
;
1997
569k
  
case lltok::kw_amdgpu_hs: CC = CallingConv::AMDGPU_HS; break30
;
1998
569k
  
case lltok::kw_amdgpu_es: CC = CallingConv::AMDGPU_ES; break8
;
1999
569k
  
case lltok::kw_amdgpu_gs: CC = CallingConv::AMDGPU_GS; break133
;
2000
569k
  
case lltok::kw_amdgpu_ps: CC = CallingConv::AMDGPU_PS; break2.81k
;
2001
569k
  
case lltok::kw_amdgpu_cs: CC = CallingConv::AMDGPU_CS; break93
;
2002
569k
  
case lltok::kw_amdgpu_kernel: CC = CallingConv::AMDGPU_KERNEL; break24.5k
;
2003
569k
  case lltok::kw_cc: {
2004
112
      Lex.Lex();
2005
112
      return ParseUInt32(CC);
2006
34.4k
    }
2007
34.4k
  }
2008
34.4k
2009
34.4k
  Lex.Lex();
2010
34.4k
  return false;
2011
34.4k
}
2012
2013
/// ParseMetadataAttachment
2014
///   ::= !dbg !42
2015
42.1k
bool LLParser::ParseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
2016
42.1k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
2017
42.1k
2018
42.1k
  std::string Name = Lex.getStrVal();
2019
42.1k
  Kind = M->getMDKindID(Name);
2020
42.1k
  Lex.Lex();
2021
42.1k
2022
42.1k
  return ParseMDNode(MD);
2023
42.1k
}
2024
2025
/// ParseInstructionMetadata
2026
///   ::= !dbg !42 (',' !dbg !57)*
2027
33.4k
bool LLParser::ParseInstructionMetadata(Instruction &Inst) {
2028
35.1k
  do {
2029
35.1k
    if (Lex.getKind() != lltok::MetadataVar)
2030
2
      return TokError("expected metadata after comma");
2031
35.1k
2032
35.1k
    unsigned MDK;
2033
35.1k
    MDNode *N;
2034
35.1k
    if (ParseMetadataAttachment(MDK, N))
2035
2
      return true;
2036
35.1k
2037
35.1k
    Inst.setMetadata(MDK, N);
2038
35.1k
    if (MDK == LLVMContext::MD_tbaa)
2039
4.63k
      InstsWithTBAATag.push_back(&Inst);
2040
35.1k
2041
35.1k
    // If this is the end of the list, we're done.
2042
35.1k
  } while (EatIfPresent(lltok::comma));
2043
33.4k
  
return false33.4k
;
2044
33.4k
}
2045
2046
/// ParseGlobalObjectMetadataAttachment
2047
///   ::= !dbg !57
2048
6.99k
bool LLParser::ParseGlobalObjectMetadataAttachment(GlobalObject &GO) {
2049
6.99k
  unsigned MDK;
2050
6.99k
  MDNode *N;
2051
6.99k
  if (ParseMetadataAttachment(MDK, N))
2052
0
    return true;
2053
6.99k
2054
6.99k
  GO.addMetadata(MDK, *N);
2055
6.99k
  return false;
2056
6.99k
}
2057
2058
/// ParseOptionalFunctionMetadata
2059
///   ::= (!dbg !57)*
2060
297k
bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
2061
302k
  while (Lex.getKind() == lltok::MetadataVar)
2062
5.25k
    if (ParseGlobalObjectMetadataAttachment(F))
2063
0
      return true;
2064
297k
  return false;
2065
297k
}
2066
2067
/// ParseOptionalAlignment
2068
///   ::= /* empty */
2069
///   ::= 'align' 4
2070
684k
bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
2071
684k
  Alignment = 0;
2072
684k
  if (!EatIfPresent(lltok::kw_align))
2073
378k
    return false;
2074
305k
  LocTy AlignLoc = Lex.getLoc();
2075
305k
  if (ParseUInt32(Alignment)) 
return true0
;
2076
305k
  if (!isPowerOf2_32(Alignment))
2077
1
    return Error(AlignLoc, "alignment is not a power of two");
2078
305k
  if (Alignment > Value::MaximumAlignment)
2079
3
    return Error(AlignLoc, "huge alignments are not supported yet");
2080
305k
  return false;
2081
305k
}
2082
2083
/// ParseOptionalDerefAttrBytes
2084
///   ::= /* empty */
2085
///   ::= AttrKind '(' 4 ')'
2086
///
2087
/// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'.
2088
bool LLParser::ParseOptionalDerefAttrBytes(lltok::Kind AttrKind,
2089
763
                                           uint64_t &Bytes) {
2090
763
  assert((AttrKind == lltok::kw_dereferenceable ||
2091
763
          AttrKind == lltok::kw_dereferenceable_or_null) &&
2092
763
         "contract!");
2093
763
2094
763
  Bytes = 0;
2095
763
  if (!EatIfPresent(AttrKind))
2096
0
    return false;
2097
763
  LocTy ParenLoc = Lex.getLoc();
2098
763
  if (!EatIfPresent(lltok::lparen))
2099
0
    return Error(ParenLoc, "expected '('");
2100
763
  LocTy DerefLoc = Lex.getLoc();
2101
763
  if (ParseUInt64(Bytes)) 
return true0
;
2102
763
  ParenLoc = Lex.getLoc();
2103
763
  if (!EatIfPresent(lltok::rparen))
2104
0
    return Error(ParenLoc, "expected ')'");
2105
763
  if (!Bytes)
2106
0
    return Error(DerefLoc, "dereferenceable bytes must be non-zero");
2107
763
  return false;
2108
763
}
2109
2110
/// ParseOptionalCommaAlign
2111
///   ::=
2112
///   ::= ',' align 4
2113
///
2114
/// This returns with AteExtraComma set to true if it ate an excess comma at the
2115
/// end.
2116
bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
2117
343k
                                       bool &AteExtraComma) {
2118
343k
  AteExtraComma = false;
2119
578k
  while (EatIfPresent(lltok::comma)) {
2120
245k
    // Metadata at the end is an early exit.
2121
245k
    if (Lex.getKind() == lltok::MetadataVar) {
2122
10.4k
      AteExtraComma = true;
2123
10.4k
      return false;
2124
10.4k
    }
2125
234k
2126
234k
    if (Lex.getKind() != lltok::kw_align)
2127
0
      return Error(Lex.getLoc(), "expected metadata or 'align'");
2128
234k
2129
234k
    if (ParseOptionalAlignment(Alignment)) 
return true2
;
2130
234k
  }
2131
343k
2132
343k
  
return false333k
;
2133
343k
}
2134
2135
/// ParseOptionalCommaAddrSpace
2136
///   ::=
2137
///   ::= ',' addrspace(1)
2138
///
2139
/// This returns with AteExtraComma set to true if it ate an excess comma at the
2140
/// end.
2141
bool LLParser::ParseOptionalCommaAddrSpace(unsigned &AddrSpace,
2142
                                           LocTy &Loc,
2143
47.1k
                                           bool &AteExtraComma) {
2144
47.1k
  AteExtraComma = false;
2145
47.8k
  while (EatIfPresent(lltok::comma)) {
2146
708
    // Metadata at the end is an early exit.
2147
708
    if (Lex.getKind() == lltok::MetadataVar) {
2148
51
      AteExtraComma = true;
2149
51
      return false;
2150
51
    }
2151
657
2152
657
    Loc = Lex.getLoc();
2153
657
    if (Lex.getKind() != lltok::kw_addrspace)
2154
0
      return Error(Lex.getLoc(), "expected metadata or 'addrspace'");
2155
657
2156
657
    if (ParseOptionalAddrSpace(AddrSpace))
2157
0
      return true;
2158
657
  }
2159
47.1k
2160
47.1k
  
return false47.1k
;
2161
47.1k
}
2162
2163
bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
2164
25
                                       Optional<unsigned> &HowManyArg) {
2165
25
  Lex.Lex();
2166
25
2167
25
  auto StartParen = Lex.getLoc();
2168
25
  if (!EatIfPresent(lltok::lparen))
2169
0
    return Error(StartParen, "expected '('");
2170
25
2171
25
  if (ParseUInt32(BaseSizeArg))
2172
0
    return true;
2173
25
2174
25
  if (EatIfPresent(lltok::comma)) {
2175
12
    auto HowManyAt = Lex.getLoc();
2176
12
    unsigned HowMany;
2177
12
    if (ParseUInt32(HowMany))
2178
0
      return true;
2179
12
    if (HowMany == BaseSizeArg)
2180
1
      return Error(HowManyAt,
2181
1
                   "'allocsize' indices can't refer to the same parameter");
2182
11
    HowManyArg = HowMany;
2183
11
  } else
2184
13
    HowManyArg = None;
2185
25
2186
25
  auto EndParen = Lex.getLoc();
2187
24
  if (!EatIfPresent(lltok::rparen))
2188
0
    return Error(EndParen, "expected ')'");
2189
24
  return false;
2190
24
}
2191
2192
/// ParseScopeAndOrdering
2193
///   if isAtomic: ::= SyncScope? AtomicOrdering
2194
///   else: ::=
2195
///
2196
/// This sets Scope and Ordering to the parsed values.
2197
bool LLParser::ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
2198
355k
                                     AtomicOrdering &Ordering) {
2199
355k
  if (!isAtomic)
2200
340k
    return false;
2201
14.6k
2202
14.6k
  return ParseScope(SSID) || ParseOrdering(Ordering);
2203
14.6k
}
2204
2205
/// ParseScope
2206
///   ::= syncscope("singlethread" | "<target scope>")?
2207
///
2208
/// This sets synchronization scope ID to the ID of the parsed value.
2209
14.6k
bool LLParser::ParseScope(SyncScope::ID &SSID) {
2210
14.6k
  SSID = SyncScope::System;
2211
14.6k
  if (EatIfPresent(lltok::kw_syncscope)) {
2212
2.54k
    auto StartParenAt = Lex.getLoc();
2213
2.54k
    if (!EatIfPresent(lltok::lparen))
2214
0
      return Error(StartParenAt, "Expected '(' in syncscope");
2215
2.54k
2216
2.54k
    std::string SSN;
2217
2.54k
    auto SSNAt = Lex.getLoc();
2218
2.54k
    if (ParseStringConstant(SSN))
2219
0
      return Error(SSNAt, "Expected synchronization scope name");
2220
2.54k
2221
2.54k
    auto EndParenAt = Lex.getLoc();
2222
2.54k
    if (!EatIfPresent(lltok::rparen))
2223
0
      return Error(EndParenAt, "Expected ')' in syncscope");
2224
2.54k
2225
2.54k
    SSID = Context.getOrInsertSyncScopeID(SSN);
2226
2.54k
  }
2227
14.6k
2228
14.6k
  return false;
2229
14.6k
}
2230
2231
/// ParseOrdering
2232
///   ::= AtomicOrdering
2233
///
2234
/// This sets Ordering to the parsed value.
2235
16.5k
bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
2236
16.5k
  switch (Lex.getKind()) {
2237
16.5k
  
default: return TokError("Expected ordering on atomic instruction")0
;
2238
16.5k
  
case lltok::kw_unordered: Ordering = AtomicOrdering::Unordered; break1.11k
;
2239
16.5k
  
case lltok::kw_monotonic: Ordering = AtomicOrdering::Monotonic; break3.59k
;
2240
16.5k
  // Not specified yet:
2241
16.5k
  // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break;
2242
16.5k
  
case lltok::kw_acquire: Ordering = AtomicOrdering::Acquire; break2.46k
;
2243
16.5k
  
case lltok::kw_release: Ordering = AtomicOrdering::Release; break1.61k
;
2244
16.5k
  
case lltok::kw_acq_rel: Ordering = AtomicOrdering::AcquireRelease; break1.25k
;
2245
16.5k
  case lltok::kw_seq_cst:
2246
6.53k
    Ordering = AtomicOrdering::SequentiallyConsistent;
2247
6.53k
    break;
2248
16.5k
  }
2249
16.5k
  Lex.Lex();
2250
16.5k
  return false;
2251
16.5k
}
2252
2253
/// ParseOptionalStackAlignment
2254
///   ::= /* empty */
2255
///   ::= 'alignstack' '(' 4 ')'
2256
34
bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
2257
34
  Alignment = 0;
2258
34
  if (!EatIfPresent(lltok::kw_alignstack))
2259
0
    return false;
2260
34
  LocTy ParenLoc = Lex.getLoc();
2261
34
  if (!EatIfPresent(lltok::lparen))
2262
0
    return Error(ParenLoc, "expected '('");
2263
34
  LocTy AlignLoc = Lex.getLoc();
2264
34
  if (ParseUInt32(Alignment)) 
return true0
;
2265
34
  ParenLoc = Lex.getLoc();
2266
34
  if (!EatIfPresent(lltok::rparen))
2267
0
    return Error(ParenLoc, "expected ')'");
2268
34
  if (!isPowerOf2_32(Alignment))
2269
0
    return Error(AlignLoc, "stack alignment is not a power of two");
2270
34
  return false;
2271
34
}
2272
2273
/// ParseIndexList - This parses the index list for an insert/extractvalue
2274
/// instruction.  This sets AteExtraComma in the case where we eat an extra
2275
/// comma at the end of the line and find that it is followed by metadata.
2276
/// Clients that don't allow metadata can call the version of this function that
2277
/// only takes one argument.
2278
///
2279
/// ParseIndexList
2280
///    ::=  (',' uint32)+
2281
///
2282
bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
2283
15.7k
                              bool &AteExtraComma) {
2284
15.7k
  AteExtraComma = false;
2285
15.7k
2286
15.7k
  if (Lex.getKind() != lltok::comma)
2287
0
    return TokError("expected ',' as start of index list");
2288
15.7k
2289
31.8k
  
while (15.7k
EatIfPresent(lltok::comma)) {
2290
16.1k
    if (Lex.getKind() == lltok::MetadataVar) {
2291
76
      if (Indices.empty()) 
return TokError("expected index")1
;
2292
75
      AteExtraComma = true;
2293
75
      return false;
2294
75
    }
2295
16.0k
    unsigned Idx = 0;
2296
16.0k
    if (ParseUInt32(Idx)) 
return true0
;
2297
16.0k
    Indices.push_back(Idx);
2298
16.0k
  }
2299
15.7k
2300
15.7k
  
return false15.6k
;
2301
15.7k
}
2302
2303
//===----------------------------------------------------------------------===//
2304
// Type Parsing.
2305
//===----------------------------------------------------------------------===//
2306
2307
/// ParseType - Parse a type.
2308
7.61M
bool LLParser::ParseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
2309
7.61M
  SMLoc TypeLoc = Lex.getLoc();
2310
7.61M
  switch (Lex.getKind()) {
2311
7.61M
  default:
2312
5
    return TokError(Msg);
2313
7.61M
  case lltok::Type:
2314
5.77M
    // Type ::= 'float' | 'void' (etc)
2315
5.77M
    Result = Lex.getTyVal();
2316
5.77M
    Lex.Lex();
2317
5.77M
    break;
2318
7.61M
  case lltok::lbrace:
2319
40.6k
    // Type ::= StructType
2320
40.6k
    if (ParseAnonStructType(Result, false))
2321
1
      return true;
2322
40.6k
    break;
2323
198k
  case lltok::lsquare:
2324
198k
    // Type ::= '[' ... ']'
2325
198k
    Lex.Lex(); // eat the lsquare.
2326
198k
    if (ParseArrayVectorType(Result, false))
2327
3
      return true;
2328
198k
    break;
2329
1.50M
  case lltok::less: // Either vector or packed struct.
2330
1.50M
    // Type ::= '<' ... '>'
2331
1.50M
    Lex.Lex();
2332
1.50M
    if (Lex.getKind() == lltok::lbrace) {
2333
835
      if (ParseAnonStructType(Result, true) ||
2334
835
          ParseToken(lltok::greater, "expected '>' at end of packed struct"))
2335
0
        return true;
2336
1.50M
    } else if (ParseArrayVectorType(Result, true))
2337
0
      return true;
2338
1.50M
    break;
2339
1.50M
  case lltok::LocalVar: {
2340
91.8k
    // Type ::= %foo
2341
91.8k
    std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
2342
91.8k
2343
91.8k
    // If the type hasn't been defined yet, create a forward definition and
2344
91.8k
    // remember where that forward def'n was seen (in case it never is defined).
2345
91.8k
    if (!Entry.first) {
2346
4.21k
      Entry.first = StructType::create(Context, Lex.getStrVal());
2347
4.21k
      Entry.second = Lex.getLoc();
2348
4.21k
    }
2349
91.8k
    Result = Entry.first;
2350
91.8k
    Lex.Lex();
2351
91.8k
    break;
2352
1.50M
  }
2353
1.50M
2354
1.50M
  case lltok::LocalVarID: {
2355
2.64k
    // Type ::= %4
2356
2.64k
    std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
2357
2.64k
2358
2.64k
    // If the type hasn't been defined yet, create a forward definition and
2359
2.64k
    // remember where that forward def'n was seen (in case it never is defined).
2360
2.64k
    if (!Entry.first) {
2361
281
      Entry.first = StructType::create(Context);
2362
281
      Entry.second = Lex.getLoc();
2363
281
    }
2364
2.64k
    Result = Entry.first;
2365
2.64k
    Lex.Lex();
2366
2.64k
    break;
2367
7.61M
  }
2368
7.61M
  }
2369
7.61M
2370
7.61M
  // Parse the type suffixes.
2371
8.54M
  
while (7.61M
true) {
2372
8.54M
    switch (Lex.getKind()) {
2373
8.54M
    // End of type.
2374
8.54M
    default:
2375
7.61M
      if (!AllowVoid && 
Result->isVoidTy()6.74M
)
2376
1
        return Error(TypeLoc, "void type only allowed for function results");
2377
7.61M
      return false;
2378
7.61M
2379
7.61M
    // Type ::= Type '*'
2380
7.61M
    case lltok::star:
2381
793k
      if (Result->isLabelTy())
2382
1
        return TokError("basic block pointers are invalid");
2383
793k
      if (Result->isVoidTy())
2384
0
        return TokError("pointers to void are invalid - use i8* instead");
2385
793k
      if (!PointerType::isValidElementType(Result))
2386
0
        return TokError("pointer to this type is invalid");
2387
793k
      Result = PointerType::getUnqual(Result);
2388
793k
      Lex.Lex();
2389
793k
      break;
2390
793k
2391
793k
    // Type ::= Type 'addrspace' '(' uint32 ')' '*'
2392
793k
    case lltok::kw_addrspace: {
2393
115k
      if (Result->isLabelTy())
2394
0
        return TokError("basic block pointers are invalid");
2395
115k
      if (Result->isVoidTy())
2396
0
        return TokError("pointers to void are invalid; use i8* instead");
2397
115k
      if (!PointerType::isValidElementType(Result))
2398
0
        return TokError("pointer to this type is invalid");
2399
115k
      unsigned AddrSpace;
2400
115k
      if (ParseOptionalAddrSpace(AddrSpace) ||
2401
115k
          ParseToken(lltok::star, "expected '*' in address space"))
2402
0
        return true;
2403
115k
2404
115k
      Result = PointerType::get(Result, AddrSpace);
2405
115k
      break;
2406
115k
    }
2407
115k
2408
115k
    /// Types '(' ArgTypeListI ')' OptFuncAttrs
2409
115k
    case lltok::lparen:
2410
21.2k
      if (ParseFunctionType(Result))
2411
0
        return true;
2412
21.2k
      break;
2413
8.54M
    }
2414
8.54M
  }
2415
7.61M
}
2416
2417
/// ParseParameterList
2418
///    ::= '(' ')'
2419
///    ::= '(' Arg (',' Arg)* ')'
2420
///  Arg
2421
///    ::= Type OptionalAttributes Value OptionalAttributes
2422
bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
2423
                                  PerFunctionState &PFS, bool IsMustTailCall,
2424
191k
                                  bool InVarArgsFunc) {
2425
191k
  if (ParseToken(lltok::lparen, "expected '(' in call"))
2426
0
    return true;
2427
191k
2428
579k
  
while (191k
Lex.getKind() != lltok::rparen) {
2429
388k
    // If this isn't the first argument, we need a comma.
2430
388k
    if (!ArgList.empty() &&
2431
388k
        
ParseToken(lltok::comma, "expected ',' in argument list")227k
)
2432
0
      return true;
2433
388k
2434
388k
    // Parse an ellipsis if this is a musttail call in a variadic function.
2435
388k
    if (Lex.getKind() == lltok::dotdotdot) {
2436
53
      const char *Msg = "unexpected ellipsis in argument list for ";
2437
53
      if (!IsMustTailCall)
2438
0
        return TokError(Twine(Msg) + "non-musttail call");
2439
53
      if (!InVarArgsFunc)
2440
1
        return TokError(Twine(Msg) + "musttail call in non-varargs function");
2441
52
      Lex.Lex();  // Lex the '...', it is purely for readability.
2442
52
      return ParseToken(lltok::rparen, "expected ')' at end of argument list");
2443
52
    }
2444
388k
2445
388k
    // Parse the argument.
2446
388k
    LocTy ArgLoc;
2447
388k
    Type *ArgTy = nullptr;
2448
388k
    AttrBuilder ArgAttrs;
2449
388k
    Value *V;
2450
388k
    if (ParseType(ArgTy, ArgLoc))
2451
0
      return true;
2452
388k
2453
388k
    if (ArgTy->isMetadataTy()) {
2454
25.2k
      if (ParseMetadataAsValue(V, PFS))
2455
3
        return true;
2456
363k
    } else {
2457
363k
      // Otherwise, handle normal operands.
2458
363k
      if (ParseOptionalParamAttrs(ArgAttrs) || 
ParseValue(ArgTy, V, PFS)363k
)
2459
3
        return true;
2460
388k
    }
2461
388k
    ArgList.push_back(ParamInfo(
2462
388k
        ArgLoc, V, AttributeSet::get(V->getContext(), ArgAttrs)));
2463
388k
  }
2464
191k
2465
191k
  
if (190k
IsMustTailCall190k
&&
InVarArgsFunc150
)
2466
1
    return TokError("expected '...' at end of argument list for musttail call "
2467
1
                    "in varargs function");
2468
190k
2469
190k
  Lex.Lex();  // Lex the ')'.
2470
190k
  return false;
2471
190k
}
2472
2473
/// ParseByValWithOptionalType
2474
///   ::= byval
2475
///   ::= byval(<ty>)
2476
1.77k
bool LLParser::ParseByValWithOptionalType(Type *&Result) {
2477
1.77k
  Result = nullptr;
2478
1.77k
  if (!EatIfPresent(lltok::kw_byval))
2479
0
    return true;
2480
1.77k
  if (!EatIfPresent(lltok::lparen))
2481
1.72k
    return false;
2482
47
  if (ParseType(Result))
2483
1
    return true;
2484
46
  if (!EatIfPresent(lltok::rparen))
2485
0
    return Error(Lex.getLoc(), "expected ')'");
2486
46
  return false;
2487
46
}
2488
2489
/// ParseOptionalOperandBundles
2490
///    ::= /*empty*/
2491
///    ::= '[' OperandBundle [, OperandBundle ]* ']'
2492
///
2493
/// OperandBundle
2494
///    ::= bundle-tag '(' ')'
2495
///    ::= bundle-tag '(' Type Value [, Type Value ]* ')'
2496
///
2497
/// bundle-tag ::= String Constant
2498
bool LLParser::ParseOptionalOperandBundles(
2499
191k
    SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) {
2500
191k
  LocTy BeginLoc = Lex.getLoc();
2501
191k
  if (!EatIfPresent(lltok::lsquare))
2502
189k
    return false;
2503
1.77k
2504
3.59k
  
while (1.77k
Lex.getKind() != lltok::rsquare) {
2505
1.81k
    // If this isn't the first operand bundle, we need a comma.
2506
1.81k
    if (!BundleList.empty() &&
2507
1.81k
        
ParseToken(lltok::comma, "expected ',' in input list")40
)
2508
0
      return true;
2509
1.81k
2510
1.81k
    std::string Tag;
2511
1.81k
    if (ParseStringConstant(Tag))
2512
0
      return true;
2513
1.81k
2514
1.81k
    if (ParseToken(lltok::lparen, "expected '(' in operand bundle"))
2515
0
      return true;
2516
1.81k
2517
1.81k
    std::vector<Value *> Inputs;
2518
3.18k
    while (Lex.getKind() != lltok::rparen) {
2519
1.37k
      // If this isn't the first input, we need a comma.
2520
1.37k
      if (!Inputs.empty() &&
2521
1.37k
          
ParseToken(lltok::comma, "expected ',' in input list")576
)
2522
0
        return true;
2523
1.37k
2524
1.37k
      Type *Ty = nullptr;
2525
1.37k
      Value *Input = nullptr;
2526
1.37k
      if (ParseType(Ty) || ParseValue(Ty, Input, PFS))
2527
0
        return true;
2528
1.37k
      Inputs.push_back(Input);
2529
1.37k
    }
2530
1.81k
2531
1.81k
    BundleList.emplace_back(std::move(Tag), std::move(Inputs));
2532
1.81k
2533
1.81k
    Lex.Lex(); // Lex the ')'.
2534
1.81k
  }
2535
1.77k
2536
1.77k
  if (BundleList.empty())
2537
0
    return Error(BeginLoc, "operand bundle set must not be empty");
2538
1.77k
2539
1.77k
  Lex.Lex(); // Lex the ']'.
2540
1.77k
  return false;
2541
1.77k
}
2542
2543
/// ParseArgumentList - Parse the argument list for a function type or function
2544
/// prototype.
2545
///   ::= '(' ArgTypeListI ')'
2546
/// ArgTypeListI
2547
///   ::= /*empty*/
2548
///   ::= '...'
2549
///   ::= ArgTypeList ',' '...'
2550
///   ::= ArgType (',' ArgType)*
2551
///
2552
bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
2553
399k
                                 bool &isVarArg){
2554
399k
  isVarArg = false;
2555
399k
  assert(Lex.getKind() == lltok::lparen);
2556
399k
  Lex.Lex(); // eat the (.
2557
399k
2558
399k
  if (Lex.getKind() == lltok::rparen) {
2559
56.1k
    // empty
2560
343k
  } else if (Lex.getKind() == lltok::dotdotdot) {
2561
7.56k
    isVarArg = true;
2562
7.56k
    Lex.Lex();
2563
336k
  } else {
2564
336k
    LocTy TypeLoc = Lex.getLoc();
2565
336k
    Type *ArgTy = nullptr;
2566
336k
    AttrBuilder Attrs;
2567
336k
    std::string Name;
2568
336k
2569
336k
    if (ParseType(ArgTy) ||
2570
336k
        
ParseOptionalParamAttrs(Attrs)336k
)
return true3
;
2571
336k
2572
336k
    if (ArgTy->isVoidTy())
2573
0
      return Error(TypeLoc, "argument can not have void type");
2574
336k
2575
336k
    if (Lex.getKind() == lltok::LocalVar) {
2576
255k
      Name = Lex.getStrVal();
2577
255k
      Lex.Lex();
2578
255k
    }
2579
336k
2580
336k
    if (!FunctionType::isValidArgumentType(ArgTy))
2581
0
      return Error(TypeLoc, "invalid type for function argument");
2582
336k
2583
336k
    ArgList.emplace_back(TypeLoc, ArgTy,
2584
336k
                         AttributeSet::get(ArgTy->getContext(), Attrs),
2585
336k
                         std::move(Name));
2586
336k
2587
679k
    while (EatIfPresent(lltok::comma)) {
2588
347k
      // Handle ... at end of arg list.
2589
347k
      if (EatIfPresent(lltok::dotdotdot)) {
2590
5.12k
        isVarArg = true;
2591
5.12k
        break;
2592
5.12k
      }
2593
342k
2594
342k
      // Otherwise must be an argument type.
2595
342k
      TypeLoc = Lex.getLoc();
2596
342k
      if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) 
return true0
;
2597
342k
2598
342k
      if (ArgTy->isVoidTy())
2599
0
        return Error(TypeLoc, "argument can not have void type");
2600
342k
2601
342k
      if (Lex.getKind() == lltok::LocalVar) {
2602
244k
        Name = Lex.getStrVal();
2603
244k
        Lex.Lex();
2604
244k
      } else {
2605
97.9k
        Name = "";
2606
97.9k
      }
2607
342k
2608
342k
      if (!ArgTy->isFirstClassType())
2609
0
        return Error(TypeLoc, "invalid type for function argument");
2610
342k
2611
342k
      ArgList.emplace_back(TypeLoc, ArgTy,
2612
342k
                           AttributeSet::get(ArgTy->getContext(), Attrs),
2613
342k
                           std::move(Name));
2614
342k
    }
2615
336k
  }
2616
399k
2617
399k
  
return ParseToken(lltok::rparen, "expected ')' at end of argument list")399k
;
2618
399k
}
2619
2620
/// ParseFunctionType
2621
///  ::= Type ArgumentList OptionalAttrs
2622
21.2k
bool LLParser::ParseFunctionType(Type *&Result) {
2623
21.2k
  assert(Lex.getKind() == lltok::lparen);
2624
21.2k
2625
21.2k
  if (!FunctionType::isValidReturnType(Result))
2626
0
    return TokError("invalid function return type");
2627
21.2k
2628
21.2k
  SmallVector<ArgInfo, 8> ArgList;
2629
21.2k
  bool isVarArg;
2630
21.2k
  if (ParseArgumentList(ArgList, isVarArg))
2631
0
    return true;
2632
21.2k
2633
21.2k
  // Reject names on the arguments lists.
2634
40.4k
  
for (unsigned i = 0, e = ArgList.size(); 21.2k
i != e;
++i19.1k
) {
2635
19.1k
    if (!ArgList[i].Name.empty())
2636
0
      return Error(ArgList[i].Loc, "argument name invalid in function type");
2637
19.1k
    if (ArgList[i].Attrs.hasAttributes())
2638
0
      return Error(ArgList[i].Loc,
2639
0
                   "argument attributes invalid in function type");
2640
19.1k
  }
2641
21.2k
2642
21.2k
  SmallVector<Type*, 16> ArgListTy;
2643
40.4k
  for (unsigned i = 0, e = ArgList.size(); i != e; 
++i19.1k
)
2644
19.1k
    ArgListTy.push_back(ArgList[i].Ty);
2645
21.2k
2646
21.2k
  Result = FunctionType::get(Result, ArgListTy, isVarArg);
2647
21.2k
  return false;
2648
21.2k
}
2649
2650
/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
2651
/// other structs.
2652
41.5k
bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
2653
41.5k
  SmallVector<Type*, 8> Elts;
2654
41.5k
  if (ParseStructBody(Elts)) 
return true1
;
2655
41.5k
2656
41.5k
  Result = StructType::get(Context, Elts, Packed);
2657
41.5k
  return false;
2658
41.5k
}
2659
2660
/// ParseStructDefinition - Parse a struct in a 'type' definition.
2661
bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
2662
                                     std::pair<Type*, LocTy> &Entry,
2663
13.3k
                                     Type *&ResultTy) {
2664
13.3k
  // If the type was already defined, diagnose the redefinition.
2665
13.3k
  if (Entry.first && 
!Entry.second.isValid()4.49k
)
2666
0
    return Error(TypeLoc, "redefinition of type");
2667
13.3k
2668
13.3k
  // If we have opaque, just return without filling in the definition for the
2669
13.3k
  // struct.  This counts as a definition as far as the .ll file goes.
2670
13.3k
  if (EatIfPresent(lltok::kw_opaque)) {
2671
809
    // This type is being defined, so clear the location to indicate this.
2672
809
    Entry.second = SMLoc();
2673
809
2674
809
    // If this type number has never been uttered, create it.
2675
809
    if (!Entry.first)
2676
486
      Entry.first = StructType::create(Context, Name);
2677
809
    ResultTy = Entry.first;
2678
809
    return false;
2679
809
  }
2680
12.5k
2681
12.5k
  // If the type starts with '<', then it is either a packed struct or a vector.
2682
12.5k
  bool isPacked = EatIfPresent(lltok::less);
2683
12.5k
2684
12.5k
  // If we don't have a struct, then we have a random type alias, which we
2685
12.5k
  // accept for compatibility with old files.  These types are not allowed to be
2686
12.5k
  // forward referenced and not allowed to be recursive.
2687
12.5k
  if (Lex.getKind() != lltok::lbrace) {
2688
706
    if (Entry.first)
2689
0
      return Error(TypeLoc, "forward references to non-struct type");
2690
706
2691
706
    ResultTy = nullptr;
2692
706
    if (isPacked)
2693
624
      return ParseArrayVectorType(ResultTy, true);
2694
82
    return ParseType(ResultTy);
2695
82
  }
2696
11.8k
2697
11.8k
  // This type is being defined, so clear the location to indicate this.
2698
11.8k
  Entry.second = SMLoc();
2699
11.8k
2700
11.8k
  // If this type number has never been uttered, create it.
2701
11.8k
  if (!Entry.first)
2702
7.64k
    Entry.first = StructType::create(Context, Name);
2703
11.8k
2704
11.8k
  StructType *STy = cast<StructType>(Entry.first);
2705
11.8k
2706
11.8k
  SmallVector<Type*, 8> Body;
2707
11.8k
  if (ParseStructBody(Body) ||
2708
11.8k
      
(11.8k
isPacked11.8k
&&
ParseToken(lltok::greater, "expected '>' in packed struct")777
))
2709
1
    return true;
2710
11.8k
2711
11.8k
  STy->setBody(Body, isPacked);
2712
11.8k
  ResultTy = STy;
2713
11.8k
  return false;
2714
11.8k
}
2715
2716
/// ParseStructType: Handles packed and unpacked types.  </> parsed elsewhere.
2717
///   StructType
2718
///     ::= '{' '}'
2719
///     ::= '{' Type (',' Type)* '}'
2720
///     ::= '<' '{' '}' '>'
2721
///     ::= '<' '{' Type (',' Type)* '}' '>'
2722
53.3k
bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
2723
53.3k
  assert(Lex.getKind() == lltok::lbrace);
2724
53.3k
  Lex.Lex(); // Consume the '{'
2725
53.3k
2726
53.3k
  // Handle the empty struct.
2727
53.3k
  if (EatIfPresent(lltok::rbrace))
2728
756
    return false;
2729
52.5k
2730
52.5k
  LocTy EltTyLoc = Lex.getLoc();
2731
52.5k
  Type *Ty = nullptr;
2732
52.5k
  if (ParseType(Ty)) 
return true0
;
2733
52.5k
  Body.push_back(Ty);
2734
52.5k
2735
52.5k
  if (!StructType::isValidElementType(Ty))
2736
0
    return Error(EltTyLoc, "invalid element type for struct");
2737
52.5k
2738
160k
  
while (52.5k
EatIfPresent(lltok::comma)) {
2739
108k
    EltTyLoc = Lex.getLoc();
2740
108k
    if (ParseType(Ty)) 
return true1
;
2741
108k
2742
108k
    if (!StructType::isValidElementType(Ty))
2743
1
      return Error(EltTyLoc, "invalid element type for struct");
2744
108k
2745
108k
    Body.push_back(Ty);
2746
108k
  }
2747
52.5k
2748
52.5k
  
return ParseToken(lltok::rbrace, "expected '}' at end of struct")52.5k
;
2749
52.5k
}
2750
2751
/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2752
/// token has already been consumed.
2753
///   Type
2754
///     ::= '[' APSINTVAL 'x' Types ']'
2755
///     ::= '<' APSINTVAL 'x' Types '>'
2756
///     ::= '<' 'vscale' 'x' APSINTVAL 'x' Types '>'
2757
1.70M
bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
2758
1.70M
  bool Scalable = false;
2759
1.70M
2760
1.70M
  if (isVector && 
Lex.getKind() == lltok::kw_vscale1.50M
) {
2761
6
    Lex.Lex(); // consume the 'vscale'
2762
6
    if (ParseToken(lltok::kw_x, "expected 'x' after vscale"))
2763
0
      return true;
2764
6
2765
6
    Scalable = true;
2766
6
  }
2767
1.70M
2768
1.70M
  if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2769
1.70M
      Lex.getAPSIntVal().getBitWidth() > 64)
2770
0
    return TokError("expected number in address space");
2771
1.70M
2772
1.70M
  LocTy SizeLoc = Lex.getLoc();
2773
1.70M
  uint64_t Size = Lex.getAPSIntVal().getZExtValue();
2774
1.70M
  Lex.Lex();
2775
1.70M
2776
1.70M
  if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2777
0
      return true;
2778
1.70M
2779
1.70M
  LocTy TypeLoc = Lex.getLoc();
2780
1.70M
  Type *EltTy = nullptr;
2781
1.70M
  if (ParseType(EltTy)) 
return true1
;
2782
1.70M
2783
1.70M
  if (ParseToken(isVector ? 
lltok::greater1.50M
:
lltok::rsquare198k
,
2784
1.70M
                 "expected end of sequential type"))
2785
0
    return true;
2786
1.70M
2787
1.70M
  if (isVector) {
2788
1.50M
    if (Size == 0)
2789
0
      return Error(SizeLoc, "zero element vector is illegal");
2790
1.50M
    if ((unsigned)Size != Size)
2791
0
      return Error(SizeLoc, "size too large for vector");
2792
1.50M
    if (!VectorType::isValidElementType(EltTy))
2793
0
      return Error(TypeLoc, "invalid vector element type");
2794
1.50M
    Result = VectorType::get(EltTy, unsigned(Size), Scalable);
2795
1.50M
  } else {
2796
198k
    if (!ArrayType::isValidElementType(EltTy))
2797
2
      return Error(TypeLoc, "invalid array element type");
2798
198k
    Result = ArrayType::get(EltTy, Size);
2799
198k
  }
2800
1.70M
  
return false1.70M
;
2801
1.70M
}
2802
2803
//===----------------------------------------------------------------------===//
2804
// Function Semantic Analysis.
2805
//===----------------------------------------------------------------------===//
2806
2807
LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2808
                                             int functionNumber)
2809
297k
  : P(p), F(f), FunctionNumber(functionNumber) {
2810
297k
2811
297k
  // Insert unnamed arguments into the NumberedVals list.
2812
297k
  for (Argument &A : F.args())
2813
500k
    if (!A.hasName())
2814
6.63k
      NumberedVals.push_back(&A);
2815
297k
}
2816
2817
297k
LLParser::PerFunctionState::~PerFunctionState() {
2818
297k
  // If there were any forward referenced non-basicblock values, delete them.
2819
297k
2820
297k
  for (const auto &P : ForwardRefVals) {
2821
11
    if (isa<BasicBlock>(P.second.first))
2822
5
      continue;
2823
6
    P.second.first->replaceAllUsesWith(
2824
6
        UndefValue::get(P.second.first->getType()));
2825
6
    P.second.first->deleteValue();
2826
6
  }
2827
297k
2828
297k
  for (const auto &P : ForwardRefValIDs) {
2829
1
    if (isa<BasicBlock>(P.second.first))
2830
0
      continue;
2831
1
    P.second.first->replaceAllUsesWith(
2832
1
        UndefValue::get(P.second.first->getType()));
2833
1
    P.second.first->deleteValue();
2834
1
  }
2835
297k
}
2836
2837
297k
bool LLParser::PerFunctionState::FinishFunction() {
2838
297k
  if (!ForwardRefVals.empty())
2839
2
    return P.Error(ForwardRefVals.begin()->second.second,
2840
2
                   "use of undefined value '%" + ForwardRefVals.begin()->first +
2841
2
                   "'");
2842
297k
  if (!ForwardRefValIDs.empty())
2843
0
    return P.Error(ForwardRefValIDs.begin()->second.second,
2844
0
                   "use of undefined value '%" +
2845
0
                   Twine(ForwardRefValIDs.begin()->first) + "'");
2846
297k
  return false;
2847
297k
}
2848
2849
/// GetVal - Get a value with the specified name or ID, creating a
2850
/// forward reference record if needed.  This can return null if the value
2851
/// exists but does not have the right type.
2852
Value *LLParser::PerFunctionState::GetVal(const std::string &Name, Type *Ty,
2853
1.98M
                                          LocTy Loc, bool IsCall) {
2854
1.98M
  // Look this name up in the normal function symbol table.
2855
1.98M
  Value *Val = F.getValueSymbolTable()->lookup(Name);
2856
1.98M
2857
1.98M
  // If this is a forward reference for the value, see if we already created a
2858
1.98M
  // forward ref record.
2859
1.98M
  if (!Val) {
2860
215k
    auto I = ForwardRefVals.find(Name);
2861
215k
    if (I != ForwardRefVals.end())
2862
1.20k
      Val = I->second.first;
2863
215k
  }
2864
1.98M
2865
1.98M
  // If we have the value in the symbol table or fwd-ref table, return it.
2866
1.98M
  if (Val)
2867
1.76M
    return P.checkValidVariableType(Loc, "%" + Name, Ty, Val, IsCall);
2868
214k
2869
214k
  // Don't make placeholders with invalid type.
2870
214k
  if (!Ty->isFirstClassType()) {
2871
0
    P.Error(Loc, "invalid use of a non-first-class type");
2872
0
    return nullptr;
2873
0
  }
2874
214k
2875
214k
  // Otherwise, create a new forward reference for this value and remember it.
2876
214k
  Value *FwdVal;
2877
214k
  if (Ty->isLabelTy()) {
2878
196k
    FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
2879
196k
  } else {
2880
17.9k
    FwdVal = new Argument(Ty, Name);
2881
17.9k
  }
2882
214k
2883
214k
  ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2884
214k
  return FwdVal;
2885
214k
}
2886
2887
Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty, LocTy Loc,
2888
481k
                                          bool IsCall) {
2889
481k
  // Look this name up in the normal function symbol table.
2890
481k
  Value *Val = ID < NumberedVals.size() ? 
NumberedVals[ID]276k
:
nullptr205k
;
2891
481k
2892
481k
  // If this is a forward reference for the value, see if we already created a
2893
481k
  // forward ref record.
2894
481k
  if (!Val) {
2895
205k
    auto I = ForwardRefValIDs.find(ID);
2896
205k
    if (I != ForwardRefValIDs.end())
2897
3.96k
      Val = I->second.first;
2898
205k
  }
2899
481k
2900
481k
  // If we have the value in the symbol table or fwd-ref table, return it.
2901
481k
  if (Val)
2902
280k
    return P.checkValidVariableType(Loc, "%" + Twine(ID), Ty, Val, IsCall);
2903
201k
2904
201k
  if (!Ty->isFirstClassType()) {
2905
0
    P.Error(Loc, "invalid use of a non-first-class type");
2906
0
    return nullptr;
2907
0
  }
2908
201k
2909
201k
  // Otherwise, create a new forward reference for this value and remember it.
2910
201k
  Value *FwdVal;
2911
201k
  if (Ty->isLabelTy()) {
2912
200k
    FwdVal = BasicBlock::Create(F.getContext(), "", &F);
2913
200k
  } else {
2914
1.19k
    FwdVal = new Argument(Ty);
2915
1.19k
  }
2916
201k
2917
201k
  ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2918
201k
  return FwdVal;
2919
201k
}
2920
2921
/// SetInstName - After an instruction is parsed and inserted into its
2922
/// basic block, this installs its name.
2923
bool LLParser::PerFunctionState::SetInstName(int NameID,
2924
                                             const std::string &NameStr,
2925
1.74M
                                             LocTy NameLoc, Instruction *Inst) {
2926
1.74M
  // If this instruction has void type, it cannot have a name or ID specified.
2927
1.74M
  if (Inst->getType()->isVoidTy()) {
2928
599k
    if (NameID != -1 || !NameStr.empty())
2929
0
      return P.Error(NameLoc, "instructions returning void cannot have a name");
2930
599k
    return false;
2931
599k
  }
2932
1.14M
2933
1.14M
  // If this was a numbered instruction, verify that the instruction is the
2934
1.14M
  // expected value and resolve any forward references.
2935
1.14M
  if (NameStr.empty()) {
2936
252k
    // If neither a name nor an ID was specified, just use the next ID.
2937
252k
    if (NameID == -1)
2938
6.45k
      NameID = NumberedVals.size();
2939
252k
2940
252k
    if (unsigned(NameID) != NumberedVals.size())
2941
0
      return P.Error(NameLoc, "instruction expected to be numbered '%" +
2942
0
                     Twine(NumberedVals.size()) + "'");
2943
252k
2944
252k
    auto FI = ForwardRefValIDs.find(NameID);
2945
252k
    if (FI != ForwardRefValIDs.end()) {
2946
1.19k
      Value *Sentinel = FI->second.first;
2947
1.19k
      if (Sentinel->getType() != Inst->getType())
2948
0
        return P.Error(NameLoc, "instruction forward referenced with type '" +
2949
0
                       getTypeString(FI->second.first->getType()) + "'");
2950
1.19k
2951
1.19k
      Sentinel->replaceAllUsesWith(Inst);
2952
1.19k
      Sentinel->deleteValue();
2953
1.19k
      ForwardRefValIDs.erase(FI);
2954
1.19k
    }
2955
252k
2956
252k
    NumberedVals.push_back(Inst);
2957
252k
    return false;
2958
896k
  }
2959
896k
2960
896k
  // Otherwise, the instruction had a name.  Resolve forward refs and set it.
2961
896k
  auto FI = ForwardRefVals.find(NameStr);
2962
896k
  if (FI != ForwardRefVals.end()) {
2963
17.9k
    Value *Sentinel = FI->second.first;
2964
17.9k
    if (Sentinel->getType() != Inst->getType())
2965
0
      return P.Error(NameLoc, "instruction forward referenced with type '" +
2966
0
                     getTypeString(FI->second.first->getType()) + "'");
2967
17.9k
2968
17.9k
    Sentinel->replaceAllUsesWith(Inst);
2969
17.9k
    Sentinel->deleteValue();
2970
17.9k
    ForwardRefVals.erase(FI);
2971
17.9k
  }
2972
896k
2973
896k
  // Set the name on the instruction.
2974
896k
  Inst->setName(NameStr);
2975
896k
2976
896k
  if (Inst->getName() != NameStr)
2977
1
    return P.Error(NameLoc, "multiple definition of local value named '" +
2978
1
                   NameStr + "'");
2979
896k
  return false;
2980
896k
}
2981
2982
/// GetBB - Get a basic block with the specified name or ID, creating a
2983
/// forward reference record if needed.
2984
BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2985
196k
                                              LocTy Loc) {
2986
196k
  return dyn_cast_or_null<BasicBlock>(
2987
196k
      GetVal(Name, Type::getLabelTy(F.getContext()), Loc, /*IsCall=*/false));
2988
196k
}
2989
2990
200k
BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
2991
200k
  return dyn_cast_or_null<BasicBlock>(
2992
200k
      GetVal(ID, Type::getLabelTy(F.getContext()), Loc, /*IsCall=*/false));
2993
200k
}
2994
2995
/// DefineBB - Define the specified basic block, which is either named or
2996
/// unnamed.  If there is an error, this returns null otherwise it returns
2997
/// the block being defined.
2998
BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2999
396k
                                                 int NameID, LocTy Loc) {
3000
396k
  BasicBlock *BB;
3001
396k
  if (Name.empty()) {
3002
200k
    if (NameID != -1 && 
unsigned(NameID) != NumberedVals.size()236
) {
3003
1
      P.Error(Loc, "label expected to be numbered '" +
3004
1
                       Twine(NumberedVals.size()) + "'");
3005
1
      return nullptr;
3006
1
    }
3007
200k
    BB = GetBB(NumberedVals.size(), Loc);
3008
200k
    if (!BB) {
3009
0
      P.Error(Loc, "unable to create block numbered '" +
3010
0
                       Twine(NumberedVals.size()) + "'");
3011
0
      return nullptr;
3012
0
    }
3013
196k
  } else {
3014
196k
    BB = GetBB(Name, Loc);
3015
196k
    if (!BB) {
3016
1
      P.Error(Loc, "unable to create block named '" + Name + "'");
3017
1
      return nullptr;
3018
1
    }
3019
396k
  }
3020
396k
3021
396k
  // Move the block to the end of the function.  Forward ref'd blocks are
3022
396k
  // inserted wherever they happen to be referenced.
3023
396k
  F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
3024
396k
3025
396k
  // Remove the block from forward ref sets.
3026
396k
  if (Name.empty()) {
3027
200k
    ForwardRefValIDs.erase(NumberedVals.size());
3028
200k
    NumberedVals.push_back(BB);
3029
200k
  } else {
3030
196k
    // BB forward references are already in the function symbol table.
3031
196k
    ForwardRefVals.erase(Name);
3032
196k
  }
3033
396k
3034
396k
  return BB;
3035
396k
}
3036
3037
//===----------------------------------------------------------------------===//
3038
// Constants.
3039
//===----------------------------------------------------------------------===//
3040
3041
/// ParseValID - Parse an abstract value that doesn't necessarily have a
3042
/// type implied.  For example, if we parse "4" we don't know what integer type
3043
/// it has.  The value will later be combined with its type and checked for
3044
/// sanity.  PFS is used to convert function-local operands of metadata (since
3045
/// metadata operands are not just parsed here but also converted to values).
3046
/// PFS can be null when we are not parsing metadata values inside a function.
3047
4.38M
bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
3048
4.38M
  ID.Loc = Lex.getLoc();
3049
4.38M
  switch (Lex.getKind()) {
3050
4.38M
  
default: return TokError("expected value token")1
;
3051
4.38M
  case lltok::GlobalID:  // @42
3052
665
    ID.UIntVal = Lex.getUIntVal();
3053
665
    ID.Kind = ValID::t_GlobalID;
3054
665
    break;
3055
4.38M
  case lltok::GlobalVar:  // @foo
3056
298k
    ID.StrVal = Lex.getStrVal();
3057
298k
    ID.Kind = ValID::t_GlobalName;
3058
298k
    break;
3059
4.38M
  case lltok::LocalVarID:  // %42
3060
281k
    ID.UIntVal = Lex.getUIntVal();
3061
281k
    ID.Kind = ValID::t_LocalID;
3062
281k
    break;
3063
4.38M
  case lltok::LocalVar:  // %foo
3064
1.78M
    ID.StrVal = Lex.getStrVal();
3065
1.78M
    ID.Kind = ValID::t_LocalName;
3066
1.78M
    break;
3067
4.38M
  case lltok::APSInt:
3068
1.48M
    ID.APSIntVal = Lex.getAPSIntVal();
3069
1.48M
    ID.Kind = ValID::t_APSInt;
3070
1.48M
    break;
3071
4.38M
  case lltok::APFloat:
3072
67.1k
    ID.APFloatVal = Lex.getAPFloatVal();
3073
67.1k
    ID.Kind = ValID::t_APFloat;
3074
67.1k
    break;
3075
4.38M
  case lltok::kw_true:
3076
10.3k
    ID.ConstantVal = ConstantInt::getTrue(Context);
3077
10.3k
    ID.Kind = ValID::t_Constant;
3078
10.3k
    break;
3079
4.38M
  case lltok::kw_false:
3080
14.2k
    ID.ConstantVal = ConstantInt::getFalse(Context);
3081
14.2k
    ID.Kind = ValID::t_Constant;
3082
14.2k
    break;
3083
4.38M
  
case lltok::kw_null: ID.Kind = ValID::t_Null; break8.77k
;
3084
4.38M
  
case lltok::kw_undef: ID.Kind = ValID::t_Undef; break215k
;
3085
4.38M
  
case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break33.5k
;
3086
4.38M
  
case lltok::kw_none: ID.Kind = ValID::t_None; break582
;
3087
4.38M
3088
4.38M
  case lltok::lbrace: {
3089
1.85k
    // ValID ::= '{' ConstVector '}'
3090
1.85k
    Lex.Lex();
3091
1.85k
    SmallVector<Constant*, 16> Elts;
3092
1.85k
    if (ParseGlobalValueVector(Elts) ||
3093
1.85k
        
ParseToken(lltok::rbrace, "expected end of struct constant")1.85k
)
3094
5
      return true;
3095
1.85k
3096
1.85k
    ID.ConstantStructElts = make_unique<Constant *[]>(Elts.size());
3097
1.85k
    ID.UIntVal = Elts.size();
3098
1.85k
    memcpy(ID.ConstantStructElts.get(), Elts.data(),
3099
1.85k
           Elts.size() * sizeof(Elts[0]));
3100
1.85k
    ID.Kind = ValID::t_ConstantStruct;
3101
1.85k
    return false;
3102
1.85k
  }
3103
91.5k
  case lltok::less: {
3104
91.5k
    // ValID ::= '<' ConstVector '>'         --> Vector.
3105
91.5k
    // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
3106
91.5k
    Lex.Lex();
3107
91.5k
    bool isPackedStruct = EatIfPresent(lltok::lbrace);
3108
91.5k
3109
91.5k
    SmallVector<Constant*, 16> Elts;
3110
91.5k
    LocTy FirstEltLoc = Lex.getLoc();
3111
91.5k
    if (ParseGlobalValueVector(Elts) ||
3112
91.5k
        (isPackedStruct &&
3113
91.5k
         
ParseToken(lltok::rbrace, "expected end of packed struct")102
) ||
3114
91.5k
        ParseToken(lltok::greater, "expected end of constant"))
3115
0
      return true;
3116
91.5k
3117
91.5k
    if (isPackedStruct) {
3118
102
      ID.ConstantStructElts = make_unique<Constant *[]>(Elts.size());
3119
102
      memcpy(ID.ConstantStructElts.get(), Elts.data(),
3120
102
             Elts.size() * sizeof(Elts[0]));
3121
102
      ID.UIntVal = Elts.size();
3122
102
      ID.Kind = ValID::t_PackedConstantStruct;
3123
102
      return false;
3124
102
    }
3125
91.3k
3126
91.3k
    if (Elts.empty())
3127
0
      return Error(ID.Loc, "constant vector must not be empty");
3128
91.3k
3129
91.3k
    if (!Elts[0]->getType()->isIntegerTy() &&
3130
91.3k
        
!Elts[0]->getType()->isFloatingPointTy()8.17k
&&
3131
91.3k
        
!Elts[0]->getType()->isPointerTy()63
)
3132
0
      return Error(FirstEltLoc,
3133
0
            "vector elements must have integer, pointer or floating point type");
3134
91.3k
3135
91.3k
    // Verify that all the vector elements have the same type.
3136
1.05M
    
for (unsigned i = 1, e = Elts.size(); 91.3k
i != e;
++i964k
)
3137
964k
      if (Elts[i]->getType() != Elts[0]->getType())
3138
0
        return Error(FirstEltLoc,
3139
0
                     "vector element #" + Twine(i) +
3140
0
                    " is not of type '" + getTypeString(Elts[0]->getType()));
3141
91.3k
3142
91.3k
    ID.ConstantVal = ConstantVector::get(Elts);
3143
91.3k
    ID.Kind = ValID::t_Constant;
3144
91.3k
    return false;
3145
91.3k
  }
3146
91.3k
  case lltok::lsquare: {   // Array Constant
3147
1.59k
    Lex.Lex();
3148
1.59k
    SmallVector<Constant*, 16> Elts;
3149
1.59k
    LocTy FirstEltLoc = Lex.getLoc();
3150
1.59k
    if (ParseGlobalValueVector(Elts) ||
3151
1.59k
        ParseToken(lltok::rsquare, "expected end of array constant"))
3152
0
      return true;
3153
1.59k
3154
1.59k
    // Handle empty element.
3155
1.59k
    if (Elts.empty()) {
3156
19
      // Use undef instead of an array because it's inconvenient to determine
3157
19
      // the element type at this point, there being no elements to examine.
3158
19
      ID.Kind = ValID::t_EmptyArray;
3159
19
      return false;
3160
19
    }
3161
1.57k
3162
1.57k
    if (!Elts[0]->getType()->isFirstClassType())
3163
0
      return Error(FirstEltLoc, "invalid array element type: " +
3164
0
                   getTypeString(Elts[0]->getType()));
3165
1.57k
3166
1.57k
    ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
3167
1.57k
3168
1.57k
    // Verify all elements are correct type!
3169
7.49k
    for (unsigned i = 0, e = Elts.size(); i != e; 
++i5.92k
) {
3170
5.92k
      if (Elts[i]->getType() != Elts[0]->getType())
3171
0
        return Error(FirstEltLoc,
3172
0
                     "array element #" + Twine(i) +
3173
0
                     " is not of type '" + getTypeString(Elts[0]->getType()));
3174
5.92k
    }
3175
1.57k
3176
1.57k
    ID.ConstantVal = ConstantArray::get(ATy, Elts);
3177
1.57k
    ID.Kind = ValID::t_Constant;
3178
1.57k
    return false;
3179
1.57k
  }
3180
2.23k
  case lltok::kw_c:  // c "foo"
3181
2.23k
    Lex.Lex();
3182
2.23k
    ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
3183
2.23k
                                                  false);
3184
2.23k
    if (ParseToken(lltok::StringConstant, "expected string")) 
return true0
;
3185
2.23k
    ID.Kind = ValID::t_Constant;
3186
2.23k
    return false;
3187
2.23k
3188
10.5k
  case lltok::kw_asm: {
3189
10.5k
    // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
3190
10.5k
    //             STRINGCONSTANT
3191
10.5k
    bool HasSideEffect, AlignStack, AsmDialect;
3192
10.5k
    Lex.Lex();
3193
10.5k
    if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
3194
10.5k
        ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
3195
10.5k
        ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
3196
10.5k
        ParseStringConstant(ID.StrVal) ||
3197
10.5k
        ParseToken(lltok::comma, "expected comma in inline asm expression") ||
3198
10.5k
        ParseToken(lltok::StringConstant, "expected constraint string"))
3199
0
      return true;
3200
10.5k
    ID.StrVal2 = Lex.getStrVal();
3201
10.5k
    ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
3202
10.5k
      (unsigned(AsmDialect)<<2);
3203
10.5k
    ID.Kind = ValID::t_InlineAsm;
3204
10.5k
    return false;
3205
10.5k
  }
3206
10.5k
3207
10.5k
  case lltok::kw_blockaddress: {
3208
768
    // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
3209
768
    Lex.Lex();
3210
768
3211
768
    ValID Fn, Label;
3212
768
3213
768
    if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
3214
768
        ParseValID(Fn) ||
3215
768
        ParseToken(lltok::comma, "expected comma in block address expression")||
3216
768
        ParseValID(Label) ||
3217
768
        ParseToken(lltok::rparen, "expected ')' in block address expression"))
3218
0
      return true;
3219
768
3220
768
    if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
3221
0
      return Error(Fn.Loc, "expected function name in blockaddress");
3222
768
    if (Label.Kind != ValID::t_LocalID && 
Label.Kind != ValID::t_LocalName732
)
3223
0
      return Error(Label.Loc, "expected basic block name in blockaddress");
3224
768
3225
768
    // Try to find the function (but skip it if it's forward-referenced).
3226
768
    GlobalValue *GV = nullptr;
3227
768
    if (Fn.Kind == ValID::t_GlobalID) {
3228
0
      if (Fn.UIntVal < NumberedVals.size())
3229
0
        GV = NumberedVals[Fn.UIntVal];
3230
768
    } else if (!ForwardRefVals.count(Fn.StrVal)) {
3231
768
      GV = M->getNamedValue(Fn.StrVal);
3232
768
    }
3233
768
    Function *F = nullptr;
3234
768
    if (GV) {
3235
384
      // Confirm that it's actually a function with a definition.
3236
384
      if (!isa<Function>(GV))
3237
0
        return Error(Fn.Loc, "expected function name in blockaddress");
3238
384
      F = cast<Function>(GV);
3239
384
      if (F->isDeclaration())
3240
0
        return Error(Fn.Loc, "cannot take blockaddress inside a declaration");
3241
768
    }
3242
768
3243
768
    if (!F) {
3244
384
      // Make a global variable as a placeholder for this reference.
3245
384
      GlobalValue *&FwdRef =
3246
384
          ForwardRefBlockAddresses.insert(std::make_pair(
3247
384
                                              std::move(Fn),
3248
384
                                              std::map<ValID, GlobalValue *>()))
3249
384
              .first->second.insert(std::make_pair(std::move(Label), nullptr))
3250
384
              .first->second;
3251
384
      if (!FwdRef)
3252
332
        FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
3253
332
                                    GlobalValue::InternalLinkage, nullptr, "");
3254
384
      ID.ConstantVal = FwdRef;
3255
384
      ID.Kind = ValID::t_Constant;
3256
384
      return false;
3257
384
    }
3258
384
3259
384
    // We found the function; now find the basic block.  Don't use PFS, since we
3260
384
    // might be inside a constant expression.
3261
384
    BasicBlock *BB;
3262
384
    if (BlockAddressPFS && 
F == &BlockAddressPFS->getFunction()382
) {
3263
307
      if (Label.Kind == ValID::t_LocalID)
3264
18
        BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc);
3265
289
      else
3266
289
        BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc);
3267
307
      if (!BB)
3268
0
        return Error(Label.Loc, "referenced value is not a basic block");
3269
77
    } else {
3270
77
      if (Label.Kind == ValID::t_LocalID)
3271
0
        return Error(Label.Loc, "cannot take address of numeric label after "
3272
0
                                "the function is defined");
3273
77
      BB = dyn_cast_or_null<BasicBlock>(
3274
77
          F->getValueSymbolTable()->lookup(Label.StrVal));
3275
77
      if (!BB)
3276
0
        return Error(Label.Loc, "referenced value is not a basic block");
3277
384
    }
3278
384
3279
384
    ID.ConstantVal = BlockAddress::get(F, BB);
3280
384
    ID.Kind = ValID::t_Constant;
3281
384
    return false;
3282
384
  }
3283
384
3284
9.58k
  case lltok::kw_trunc:
3285
9.58k
  case lltok::kw_zext:
3286
9.58k
  case lltok::kw_sext:
3287
9.58k
  case lltok::kw_fptrunc:
3288
9.58k
  case lltok::kw_fpext:
3289
9.58k
  case lltok::kw_bitcast:
3290
9.58k
  case lltok::kw_addrspacecast:
3291
9.58k
  case lltok::kw_uitofp:
3292
9.58k
  case lltok::kw_sitofp:
3293
9.58k
  case lltok::kw_fptoui:
3294
9.58k
  case lltok::kw_fptosi:
3295
9.58k
  case lltok::kw_inttoptr:
3296
9.58k
  case lltok::kw_ptrtoint: {
3297
9.58k
    unsigned Opc = Lex.getUIntVal();
3298
9.58k
    Type *DestTy = nullptr;
3299
9.58k
    Constant *SrcVal;
3300
9.58k
    Lex.Lex();
3301
9.58k
    if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
3302
9.58k
        ParseGlobalTypeAndValue(SrcVal) ||
3303
9.58k
        
ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast")9.57k
||
3304
9.58k
        
ParseType(DestTy)9.57k
||
3305
9.58k
        
ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast")9.57k
)
3306
1
      return true;
3307
9.57k
    if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
3308
10
      return Error(ID.Loc, "invalid cast opcode for cast from '" +
3309
10
                   getTypeString(SrcVal->getType()) + "' to '" +
3310
10
                   getTypeString(DestTy) + "'");
3311
9.56k
    ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
3312
9.56k
                                                 SrcVal, DestTy);
3313
9.56k
    ID.Kind = ValID::t_Constant;
3314
9.56k
    return false;
3315
9.56k
  }
3316
9.56k
  case lltok::kw_extractvalue: {
3317
11
    Lex.Lex();
3318
11
    Constant *Val;
3319
11
    SmallVector<unsigned, 4> Indices;
3320
11
    if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
3321
11
        ParseGlobalTypeAndValue(Val) ||
3322
11
        ParseIndexList(Indices) ||
3323
11
        ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
3324
0
      return true;
3325
11
3326
11
    if (!Val->getType()->isAggregateType())
3327
0
      return Error(ID.Loc, "extractvalue operand must be aggregate type");
3328
11
    if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
3329
0
      return Error(ID.Loc, "invalid indices for extractvalue");
3330
11
    ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
3331
11
    ID.Kind = ValID::t_Constant;
3332
11
    return false;
3333
11
  }
3334
11
  case lltok::kw_insertvalue: {
3335
10
    Lex.Lex();
3336
10
    Constant *Val0, *Val1;
3337
10
    SmallVector<unsigned, 4> Indices;
3338
10
    if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
3339
10
        ParseGlobalTypeAndValue(Val0) ||
3340
10
        ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
3341
10
        ParseGlobalTypeAndValue(Val1) ||
3342
10
        ParseIndexList(Indices) ||
3343
10
        ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
3344
0
      return true;
3345
10
    if (!Val0->getType()->isAggregateType())
3346
0
      return Error(ID.Loc, "insertvalue operand must be aggregate type");
3347
10
    Type *IndexedType =
3348
10
        ExtractValueInst::getIndexedType(Val0->getType(), Indices);
3349
10
    if (!IndexedType)
3350
0
      return Error(ID.Loc, "invalid indices for insertvalue");
3351
10
    if (IndexedType != Val1->getType())
3352
1
      return Error(ID.Loc, "insertvalue operand and field disagree in type: '" +
3353
1
                               getTypeString(Val1->getType()) +
3354
1
                               "' instead of '" + getTypeString(IndexedType) +
3355
1
                               "'");
3356
9
    ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
3357
9
    ID.Kind = ValID::t_Constant;
3358
9
    return false;
3359
9
  }
3360
312
  case lltok::kw_icmp:
3361
312
  case lltok::kw_fcmp: {
3362
312
    unsigned PredVal, Opc = Lex.getUIntVal();
3363
312
    Constant *Val0, *Val1;
3364
312
    Lex.Lex();
3365
312
    if (ParseCmpPredicate(PredVal, Opc) ||
3366
312
        ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
3367
312
        ParseGlobalTypeAndValue(Val0) ||
3368
312
        ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
3369
312
        ParseGlobalTypeAndValue(Val1) ||
3370
312
        ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
3371
0
      return true;
3372
312
3373
312
    if (Val0->getType() != Val1->getType())
3374
0
      return Error(ID.Loc, "compare operands must have the same type");
3375
312
3376
312
    CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
3377
312
3378
312
    if (Opc == Instruction::FCmp) {
3379
6
      if (!Val0->getType()->isFPOrFPVectorTy())
3380
0
        return Error(ID.Loc, "fcmp requires floating point operands");
3381
6
      ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
3382
306
    } else {
3383
306
      assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
3384
306
      if (!Val0->getType()->isIntOrIntVectorTy() &&
3385
306
          
!Val0->getType()->isPtrOrPtrVectorTy()220
)
3386
0
        return Error(ID.Loc, "icmp requires pointer or integer operands");
3387
306
      ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
3388
306
    }
3389
312
    ID.Kind = ValID::t_Constant;
3390
312
    return false;
3391
312
  }
3392
312
 
3393
312
  // Unary Operators.
3394
312
  case lltok::kw_fneg: {
3395
4
    unsigned Opc = Lex.getUIntVal();
3396
4
    Constant *Val;
3397
4
    Lex.Lex();
3398
4
    if (ParseToken(lltok::lparen, "expected '(' in unary constantexpr") ||
3399
4
        ParseGlobalTypeAndValue(Val) ||
3400
4
        ParseToken(lltok::rparen, "expected ')' in unary constantexpr"))
3401
0
      return true;
3402
4
    
3403
4
    // Check that the type is valid for the operator.
3404
4
    switch (Opc) {
3405
4
    case Instruction::FNeg:
3406
4
      if (!Val->getType()->isFPOrFPVectorTy())
3407
0
        return Error(ID.Loc, "constexpr requires fp operands");
3408
4
      break;
3409
4
    
default: 0
llvm_unreachable0
("Unknown unary operator!");
3410
4
    }
3411
4
    unsigned Flags = 0;
3412
4
    Constant *C = ConstantExpr::get(Opc, Val, Flags);
3413
4
    ID.ConstantVal = C;
3414
4
    ID.Kind = ValID::t_Constant;
3415
4
    return false;
3416
4
  }
3417
4
  // Binary Operators.
3418
729
  case lltok::kw_add:
3419
729
  case lltok::kw_fadd:
3420
729
  case lltok::kw_sub:
3421
729
  case lltok::kw_fsub:
3422
729
  case lltok::kw_mul:
3423
729
  case lltok::kw_fmul:
3424
729
  case lltok::kw_udiv:
3425
729
  case lltok::kw_sdiv:
3426
729
  case lltok::kw_fdiv:
3427
729
  case lltok::kw_urem:
3428
729
  case lltok::kw_srem:
3429
729
  case lltok::kw_frem:
3430
729
  case lltok::kw_shl:
3431
729
  case lltok::kw_lshr:
3432
729
  case lltok::kw_ashr: {
3433
729
    bool NUW = false;
3434
729
    bool NSW = false;
3435
729
    bool Exact = false;
3436
729
    unsigned Opc = Lex.getUIntVal();
3437
729
    Constant *Val0, *Val1;
3438
729
    Lex.Lex();
3439
729
    if (Opc == Instruction::Add || 
Opc == Instruction::Sub629
||
3440
729
        
Opc == Instruction::Mul268
||
Opc == Instruction::Shl197
) {
3441
579
      if (EatIfPresent(lltok::kw_nuw))
3442
240
        NUW = true;
3443
579
      if (EatIfPresent(lltok::kw_nsw)) {
3444
259
        NSW = true;
3445
259
        if (EatIfPresent(lltok::kw_nuw))
3446
10
          NUW = true;
3447
259
      }
3448
579
    } else 
if (150
Opc == Instruction::SDiv150
||
Opc == Instruction::UDiv124
||
3449
150
               
Opc == Instruction::LShr114
||
Opc == Instruction::AShr58
) {
3450
117
      if (EatIfPresent(lltok::kw_exact))
3451
24
        Exact = true;
3452
117
    }
3453
729
    if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
3454
729
        ParseGlobalTypeAndValue(Val0) ||
3455
729
        ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
3456
729
        ParseGlobalTypeAndValue(Val1) ||
3457
729
        ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
3458
0
      return true;
3459
729
    if (Val0->getType() != Val1->getType())
3460
0
      return Error(ID.Loc, "operands of constexpr must have same type");
3461
729
    // Check that the type is valid for the operator.
3462
729
    switch (Opc) {
3463
729
    case Instruction::Add:
3464
707
    case Instruction::Sub:
3465
707
    case Instruction::Mul:
3466
707
    case Instruction::UDiv:
3467
707
    case Instruction::SDiv:
3468
707
    case Instruction::URem:
3469
707
    case Instruction::SRem:
3470
707
    case Instruction::Shl:
3471
707
    case Instruction::AShr:
3472
707
    case Instruction::LShr:
3473
707
      if (!Val0->getType()->isIntOrIntVectorTy())
3474
1
        return Error(ID.Loc, "constexpr requires integer operands");
3475
706
      break;
3476
706
    case Instruction::FAdd:
3477
22
    case Instruction::FSub:
3478
22
    case Instruction::FMul:
3479
22
    case Instruction::FDiv:
3480
22
    case Instruction::FRem:
3481
22
      if (!Val0->getType()->isFPOrFPVectorTy())
3482
0
        return Error(ID.Loc, "constexpr requires fp operands");
3483
22
      break;
3484
22
    
default: 0
llvm_unreachable0
("Unknown binary operator!");
3485
728
    }
3486
728
    unsigned Flags = 0;
3487
728
    if (NUW)   
Flags |= OverflowingBinaryOperator::NoUnsignedWrap250
;
3488
728
    if (NSW)   
Flags |= OverflowingBinaryOperator::NoSignedWrap259
;
3489
728
    if (Exact) 
Flags |= PossiblyExactOperator::IsExact24
;
3490
728
    Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
3491
728
    ID.ConstantVal = C;
3492
728
    ID.Kind = ValID::t_Constant;
3493
728
    return false;
3494
728
  }
3495
728
3496
728
  // Logical Operations
3497
728
  case lltok::kw_and:
3498
114
  case lltok::kw_or:
3499
114
  case lltok::kw_xor: {
3500
114
    unsigned Opc = Lex.getUIntVal();
3501
114
    Constant *Val0, *Val1;
3502
114
    Lex.Lex();
3503
114
    if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
3504
114
        ParseGlobalTypeAndValue(Val0) ||
3505
114
        ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
3506
114
        ParseGlobalTypeAndValue(Val1) ||
3507
114
        ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
3508
0
      return true;
3509
114
    if (Val0->getType() != Val1->getType())
3510
0
      return Error(ID.Loc, "operands of constexpr must have same type");
3511
114
    if (!Val0->getType()->isIntOrIntVectorTy())
3512
0
      return Error(ID.Loc,
3513
0
                   "constexpr requires integer or integer vector operands");
3514
114
    ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
3515
114
    ID.Kind = ValID::t_Constant;
3516
114
    return false;
3517
114
  }
3518
114
3519
71.1k
  case lltok::kw_getelementptr:
3520
71.1k
  case lltok::kw_shufflevector:
3521
71.1k
  case lltok::kw_insertelement:
3522
71.1k
  case lltok::kw_extractelement:
3523
71.1k
  case lltok::kw_select: {
3524
71.1k
    unsigned Opc = Lex.getUIntVal();
3525
71.1k
    SmallVector<Constant*, 16> Elts;
3526
71.1k
    bool InBounds = false;
3527
71.1k
    Type *Ty;
3528
71.1k
    Lex.Lex();
3529
71.1k
3530
71.1k
    if (Opc == Instruction::GetElementPtr)
3531
71.0k
      InBounds = EatIfPresent(lltok::kw_inbounds);
3532
71.1k
3533
71.1k
    if (ParseToken(lltok::lparen, "expected '(' in constantexpr"))
3534
0
      return true;
3535
71.1k
3536
71.1k
    LocTy ExplicitTypeLoc = Lex.getLoc();
3537
71.1k
    if (Opc == Instruction::GetElementPtr) {
3538
71.0k
      if (ParseType(Ty) ||
3539
71.0k
          ParseToken(lltok::comma, "expected comma after getelementptr's type"))
3540
0
        return true;
3541
71.1k
    }
3542
71.1k
3543
71.1k
    Optional<unsigned> InRangeOp;
3544
71.1k
    if (ParseGlobalValueVector(
3545
71.1k
            Elts, Opc == Instruction::GetElementPtr ? 
&InRangeOp71.0k
:
nullptr92
) ||
3546
71.1k
        
ParseToken(lltok::rparen, "expected ')' in constantexpr")71.1k
)
3547
1
      return true;
3548
71.1k
3549
71.1k
    if (Opc == Instruction::GetElementPtr) {
3550
71.0k
      if (Elts.size() == 0 ||
3551
71.0k
          !Elts[0]->getType()->isPtrOrPtrVectorTy())
3552
0
        return Error(ID.Loc, "base of getelementptr must be a pointer");
3553
71.0k
3554
71.0k
      Type *BaseType = Elts[0]->getType();
3555
71.0k
      auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
3556
71.0k
      if (Ty != BasePointerType->getElementType())
3557
0
        return Error(
3558
0
            ExplicitTypeLoc,
3559
0
            "explicit pointee type doesn't match operand's pointee type");
3560
71.0k
3561
71.0k
      unsigned GEPWidth =
3562
71.0k
          BaseType->isVectorTy() ? 
BaseType->getVectorNumElements()17
:
071.0k
;
3563
71.0k
3564
71.0k
      ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
3565
142k
      for (Constant *Val : Indices) {
3566
142k
        Type *ValTy = Val->getType();
3567
142k
        if (!ValTy->isIntOrIntVectorTy())
3568
0
          return Error(ID.Loc, "getelementptr index must be an integer");
3569
142k
        if (ValTy->isVectorTy()) {
3570
38
          unsigned ValNumEl = ValTy->getVectorNumElements();
3571
38
          if (GEPWidth && 
(ValNumEl != GEPWidth)36
)
3572
2
            return Error(
3573
2
                ID.Loc,
3574
2
                "getelementptr vector index has a wrong number of elements");
3575
36
          // GEPWidth may have been unknown because the base is a scalar,
3576
36
          // but it is known now.
3577
36
          GEPWidth = ValNumEl;
3578
36
        }
3579
142k
      }
3580
71.0k
3581
71.0k
      SmallPtrSet<Type*, 4> Visited;
3582
71.0k
      if (!Indices.empty() && 
!Ty->isSized(&Visited)71.0k
)
3583
0
        return Error(ID.Loc, "base element of getelementptr must be sized");
3584
71.0k
3585
71.0k
      if (!GetElementPtrInst::getIndexedType(Ty, Indices))
3586
0
        return Error(ID.Loc, "invalid getelementptr indices");
3587
71.0k
3588
71.0k
      if (InRangeOp) {
3589
72
        if (*InRangeOp == 0)
3590
0
          return Error(ID.Loc,
3591
0
                       "inrange keyword may not appear on pointer operand");
3592
72
        --*InRangeOp;
3593
72
      }
3594
71.0k
3595
71.0k
      ID.ConstantVal = ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices,
3596
71.0k
                                                      InBounds, InRangeOp);
3597
71.0k
    } else 
if (92
Opc == Instruction::Select92
) {
3598
57
      if (Elts.size() != 3)
3599
0
        return Error(ID.Loc, "expected three operands to select");
3600
57
      if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
3601
0
                                                              Elts[2]))
3602
0
        return Error(ID.Loc, Reason);
3603
57
      ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
3604
57
    } else 
if (35
Opc == Instruction::ShuffleVector35
) {
3605
10
      if (Elts.size() != 3)
3606
0
        return Error(ID.Loc, "expected three operands to shufflevector");
3607
10
      if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
3608
0
        return Error(ID.Loc, "invalid operands to shufflevector");
3609
10
      ID.ConstantVal =
3610
10
                 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
3611
25
    } else if (Opc == Instruction::ExtractElement) {
3612
23
      if (Elts.size() != 2)
3613
0
        return Error(ID.Loc, "expected two operands to extractelement");
3614
23
      if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
3615
0
        return Error(ID.Loc, "invalid extractelement operands");
3616
23
      ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
3617
23
    } else {
3618
2
      assert(Opc == Instruction::InsertElement && "Unknown opcode");
3619
2
      if (Elts.size() != 3)
3620
0
      return Error(ID.Loc, "expected three operands to insertelement");
3621
2
      if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
3622
0
        return Error(ID.Loc, "invalid insertelement operands");
3623
2
      ID.ConstantVal =
3624
2
                 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
3625
2
    }
3626
71.1k
3627
71.1k
    ID.Kind = ValID::t_Constant;
3628
71.1k
    return false;
3629
4.19M
  }
3630
4.19M
  }
3631
4.19M
3632
4.19M
  Lex.Lex();
3633
4.19M
  return false;
3634
4.19M
}
3635
3636
/// ParseGlobalValue - Parse a global value with the specified type.
3637
1.31M
bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
3638
1.31M
  C = nullptr;
3639
1.31M
  ValID ID;
3640
1.31M
  Value *V = nullptr;
3641
1.31M
  bool Parsed = ParseValID(ID) ||
3642
1.31M
                
ConvertValIDToValue(Ty, ID, V, nullptr, /*IsCall=*/false)1.31M
;
3643
1.31M
  if (V && 
!(C = dyn_cast<Constant>(V))1.31M
)
3644
0
    return Error(ID.Loc, "global values must be constants");
3645
1.31M
  return Parsed;
3646
1.31M
}
3647
3648
1.29M
bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
3649
1.29M
  Type *Ty = nullptr;
3650
1.29M
  return ParseType(Ty) ||
3651
1.29M
         ParseGlobalValue(Ty, V);
3652
1.29M
}
3653
3654
379k
bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
3655
379k
  C = nullptr;
3656
379k
3657
379k
  LocTy KwLoc = Lex.getLoc();
3658
379k
  if (!EatIfPresent(lltok::kw_comdat))
3659
378k
    return false;
3660
907
3661
907
  if (EatIfPresent(lltok::lparen)) {
3662
318
    if (Lex.getKind() != lltok::ComdatVar)
3663
0
      return TokError("expected comdat variable");
3664
318
    C = getComdat(Lex.getStrVal(), Lex.getLoc());
3665
318
    Lex.Lex();
3666
318
    if (ParseToken(lltok::rparen, "expected ')' after comdat var"))
3667
0
      return true;
3668
589
  } else {
3669
589
    if (GlobalName.empty())
3670
1
      return TokError("comdat cannot be unnamed");
3671
588
    C = getComdat(GlobalName, KwLoc);
3672
588
  }
3673
907
3674
907
  
return false906
;
3675
907
}
3676
3677
/// ParseGlobalValueVector
3678
///   ::= /*empty*/
3679
///   ::= [inrange] TypeAndValue (',' [inrange] TypeAndValue)*
3680
bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts,
3681
166k
                                      Optional<unsigned> *InRangeOp) {
3682
166k
  // Empty list.
3683
166k
  if (Lex.getKind() == lltok::rbrace ||
3684
166k
      
Lex.getKind() == lltok::rsquare166k
||
3685
166k
      
Lex.getKind() == lltok::greater166k
||
3686
166k
      
Lex.getKind() == lltok::rparen166k
)
3687
29
    return false;
3688
166k
3689
1.28M
  
do 166k
{
3690
1.28M
    if (InRangeOp && 
!*InRangeOp213k
&&
EatIfPresent(lltok::kw_inrange)213k
)
3691
72
      *InRangeOp = Elts.size();
3692
1.28M
3693
1.28M
    Constant *C;
3694
1.28M
    if (ParseGlobalTypeAndValue(C)) 
return true6
;
3695
1.28M
    Elts.push_back(C);
3696
1.28M
  } while (EatIfPresent(lltok::comma));
3697
166k
3698
166k
  
return false166k
;
3699
166k
}
3700
3701
24.8k
bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) {
3702
24.8k
  SmallVector<Metadata *, 16> Elts;
3703
24.8k
  if (ParseMDNodeVector(Elts))
3704
8
    return true;
3705
24.8k
3706
24.8k
  MD = (IsDistinct ? 
MDTuple::getDistinct737
:
MDTuple::get24.1k
)(Context, Elts);
3707
24.8k
  return false;
3708
24.8k
}
3709
3710
/// MDNode:
3711
///  ::= !{ ... }
3712
///  ::= !7
3713
///  ::= !DILocation(...)
3714
42.1k
bool LLParser::ParseMDNode(MDNode *&N) {
3715
42.1k
  if (Lex.getKind() == lltok::MetadataVar)
3716
96
    return ParseSpecializedMDNode(N);
3717
42.0k
3718
42.0k
  return ParseToken(lltok::exclaim, "expected '!' here") ||
3719
42.0k
         ParseMDNodeTail(N);
3720
42.0k
}
3721
3722
142k
bool LLParser::ParseMDNodeTail(MDNode *&N) {
3723
142k
  // !{ ... }
3724
142k
  if (Lex.getKind() == lltok::lbrace)
3725
456
    return ParseMDTuple(N);
3726
141k
3727
141k
  // !42
3728
141k
  return ParseMDNodeID(N);
3729
141k
}
3730
3731
namespace {
3732
3733
/// Structure to represent an optional metadata field.
3734
template <class FieldTy> struct MDFieldImpl {
3735
  typedef MDFieldImpl ImplTy;
3736
  FieldTy Val;
3737
  bool Seen;
3738
3739
188k
  void assign(FieldTy Val) {
3740
188k
    Seen = true;
3741
188k
    this->Val = std::move(Val);
3742
188k
  }
LLParser.cpp:(anonymous namespace)::MDFieldImpl<unsigned long long>::assign(unsigned long long)
Line
Count
Source
3739
70.4k
  void assign(FieldTy Val) {
3740
70.4k
    Seen = true;
3741
70.4k
    this->Val = std::move(Val);
3742
70.4k
  }
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::Metadata*>::assign(llvm::Metadata*)
Line
Count
Source
3739
64.2k
  void assign(FieldTy Val) {
3740
64.2k
    Seen = true;
3741
64.2k
    this->Val = std::move(Val);
3742
64.2k
  }
LLParser.cpp:(anonymous namespace)::MDFieldImpl<bool>::assign(bool)
Line
Count
Source
3739
14.2k
  void assign(FieldTy Val) {
3740
14.2k
    Seen = true;
3741
14.2k
    this->Val = std::move(Val);
3742
14.2k
  }
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::MDString*>::assign(llvm::MDString*)
Line
Count
Source
3739
35.2k
  void assign(FieldTy Val) {
3740
35.2k
    Seen = true;
3741
35.2k
    this->Val = std::move(Val);
3742
35.2k
  }
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::SmallVector<llvm::Metadata*, 4u> >::assign(llvm::SmallVector<llvm::Metadata*, 4u>)
Line
Count
Source
3739
12
  void assign(FieldTy Val) {
3740
12
    Seen = true;
3741
12
    this->Val = std::move(Val);
3742
12
  }
LLParser.cpp:(anonymous namespace)::MDFieldImpl<long long>::assign(long long)
Line
Count
Source
3739
297
  void assign(FieldTy Val) {
3740
297
    Seen = true;
3741
297
    this->Val = std::move(Val);
3742
297
  }
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::DINode::DIFlags>::assign(llvm::DINode::DIFlags)
Line
Count
Source
3739
3.81k
  void assign(FieldTy Val) {
3740
3.81k
    Seen = true;
3741
3.81k
    this->Val = std::move(Val);
3742
3.81k
  }
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::DIFile::ChecksumKind>::assign(llvm::DIFile::ChecksumKind)
Line
Count
Source
3739
225
  void assign(FieldTy Val) {
3740
225
    Seen = true;
3741
225
    this->Val = std::move(Val);
3742
225
  }
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::DISubprogram::DISPFlags>::assign(llvm::DISubprogram::DISPFlags)
Line
Count
Source
3739
395
  void assign(FieldTy Val) {
3740
395
    Seen = true;
3741
395
    this->Val = std::move(Val);
3742
395
  }
3743
3744
  explicit MDFieldImpl(FieldTy Default)
3745
360k
      : Val(std::move(Default)), Seen(false) {}
LLParser.cpp:(anonymous namespace)::MDFieldImpl<unsigned long long>::MDFieldImpl(unsigned long long)
Line
Count
Source
3745
109k
      : Val(std::move(Default)), Seen(false) {}
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::Metadata*>::MDFieldImpl(llvm::Metadata*)
Line
Count
Source
3745
114k
      : Val(std::move(Default)), Seen(false) {}
LLParser.cpp:(anonymous namespace)::MDFieldImpl<bool>::MDFieldImpl(bool)
Line
Count
Source
3745
45.9k
      : Val(std::move(Default)), Seen(false) {}
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::MDString*>::MDFieldImpl(llvm::MDString*)
Line
Count
Source
3745
52.5k
      : Val(std::move(Default)), Seen(false) {}
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::SmallVector<llvm::Metadata*, 4u> >::MDFieldImpl(llvm::SmallVector<llvm::Metadata*, 4u>)
Line
Count
Source
3745
39
      : Val(std::move(Default)), Seen(false) {}
LLParser.cpp:(anonymous namespace)::MDFieldImpl<long long>::MDFieldImpl(long long)
Line
Count
Source
3745
16.0k
      : Val(std::move(Default)), Seen(false) {}
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::DINode::DIFlags>::MDFieldImpl(llvm::DINode::DIFlags)
Line
Count
Source
3745
15.1k
      : Val(std::move(Default)), Seen(false) {}
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::DIFile::ChecksumKind>::MDFieldImpl(llvm::DIFile::ChecksumKind)
Line
Count
Source
3745
3.10k
      : Val(std::move(Default)), Seen(false) {}
LLParser.cpp:(anonymous namespace)::MDFieldImpl<llvm::DISubprogram::DISPFlags>::MDFieldImpl(llvm::DISubprogram::DISPFlags)
Line
Count
Source
3745
3.72k
      : Val(std::move(Default)), Seen(false) {}
3746
};
3747
3748
/// Structure to represent an optional metadata field that
3749
/// can be of either type (A or B) and encapsulates the
3750
/// MD<typeofA>Field and MD<typeofB>Field structs, so not
3751
/// to reimplement the specifics for representing each Field.
3752
template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
3753
  typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
3754
  FieldTypeA A;
3755
  FieldTypeB B;
3756
  bool Seen;
3757
3758
  enum {
3759
    IsInvalid = 0,
3760
    IsTypeA = 1,
3761
    IsTypeB = 2
3762
  } WhatIs;
3763
3764
242
  void assign(FieldTypeA A) {
3765
242
    Seen = true;
3766
242
    this->A = std::move(A);
3767
242
    WhatIs = IsTypeA;
3768
242
  }
LLParser.cpp:(anonymous namespace)::MDEitherFieldImpl<(anonymous namespace)::MDSignedField, (anonymous namespace)::MDField>::assign((anonymous namespace)::MDSignedField)
Line
Count
Source
3764
212
  void assign(FieldTypeA A) {
3765
212
    Seen = true;
3766
212
    this->A = std::move(A);
3767
212
    WhatIs = IsTypeA;
3768
212
  }
LLParser.cpp:(anonymous namespace)::MDEitherFieldImpl<(anonymous namespace)::MDSignedField, (anonymous namespace)::MDUnsignedField>::assign((anonymous namespace)::MDSignedField)
Line
Count
Source
3764
30
  void assign(FieldTypeA A) {
3765
30
    Seen = true;
3766
30
    this->A = std::move(A);
3767
30
    WhatIs = IsTypeA;
3768
30
  }
3769
3770
11.8k
  void assign(FieldTypeB B) {
3771
11.8k
    Seen = true;
3772
11.8k
    this->B = std::move(B);
3773
11.8k
    WhatIs = IsTypeB;
3774
11.8k
  }
LLParser.cpp:(anonymous namespace)::MDEitherFieldImpl<(anonymous namespace)::MDSignedField, (anonymous namespace)::MDField>::assign((anonymous namespace)::MDField)
Line
Count
Source
3770
22
  void assign(FieldTypeB B) {
3771
22
    Seen = true;
3772
22
    this->B = std::move(B);
3773
22
    WhatIs = IsTypeB;
3774
22
  }
LLParser.cpp:(anonymous namespace)::MDEitherFieldImpl<(anonymous namespace)::MDSignedField, (anonymous namespace)::MDUnsignedField>::assign((anonymous namespace)::MDUnsignedField)
Line
Count
Source
3770
11.8k
  void assign(FieldTypeB B) {
3771
11.8k
    Seen = true;
3772
11.8k
    this->B = std::move(B);
3773
11.8k
    WhatIs = IsTypeB;
3774
11.8k
  }
3775
3776
  explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
3777
      : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
3778
12.1k
        WhatIs(IsInvalid) {}
LLParser.cpp:(anonymous namespace)::MDEitherFieldImpl<(anonymous namespace)::MDSignedField, (anonymous namespace)::MDField>::MDEitherFieldImpl((anonymous namespace)::MDSignedField, (anonymous namespace)::MDField)
Line
Count
Source
3778
238
        WhatIs(IsInvalid) {}
LLParser.cpp:(anonymous namespace)::MDEitherFieldImpl<(anonymous namespace)::MDSignedField, (anonymous namespace)::MDUnsignedField>::MDEitherFieldImpl((anonymous namespace)::MDSignedField, (anonymous namespace)::MDUnsignedField)
Line
Count
Source
3778
11.8k
        WhatIs(IsInvalid) {}
3779
};
3780
3781
struct MDUnsignedField : public MDFieldImpl<uint64_t> {
3782
  uint64_t Max;
3783
3784
  MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
3785
109k
      : ImplTy(Default), Max(Max) {}
3786
};
3787
3788
struct LineField : public MDUnsignedField {
3789
28.0k
  LineField() : MDUnsignedField(0, UINT32_MAX) {}
3790
};
3791
3792
struct ColumnField : public MDUnsignedField {
3793
11.9k
  ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
3794
};
3795
3796
struct DwarfTagField : public MDUnsignedField {
3797
4.40k
  DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
3798
  DwarfTagField(dwarf::Tag DefaultTag)
3799
2.00k
      : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
3800
};
3801
3802
struct DwarfMacinfoTypeField : public MDUnsignedField {
3803
21
  DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
3804
  DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
3805
27
    : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
3806
};
3807
3808
struct DwarfAttEncodingField : public MDUnsignedField {
3809
1.94k
  DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
3810
};
3811
3812
struct DwarfVirtualityField : public MDUnsignedField {
3813
3.72k
  DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
3814
};
3815
3816
struct DwarfLangField : public MDUnsignedField {
3817
3.71k
  DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
3818
};
3819
3820
struct DwarfCCField : public MDUnsignedField {
3821
2.44k
  DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
3822
};
3823
3824
struct EmissionKindField : public MDUnsignedField {
3825
2.42k
  EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {}
3826
};
3827
3828
struct NameTableKindField : public MDUnsignedField {
3829
  NameTableKindField()
3830
      : MDUnsignedField(
3831
            0, (unsigned)
3832
2.42k
                   DICompileUnit::DebugNameTableKind::LastDebugNameTableKind) {}
3833
};
3834
3835
struct DIFlagField : public MDFieldImpl<DINode::DIFlags> {
3836
15.1k
  DIFlagField() : MDFieldImpl(DINode::FlagZero) {}
3837
};
3838
3839
struct DISPFlagField : public MDFieldImpl<DISubprogram::DISPFlags> {
3840
3.72k
  DISPFlagField() : MDFieldImpl(DISubprogram::SPFlagZero) {}
3841
};
3842
3843
struct MDSignedField : public MDFieldImpl<int64_t> {
3844
  int64_t Min;
3845
  int64_t Max;
3846
3847
  MDSignedField(int64_t Default = 0)
3848
12.1k
      : ImplTy(Default), Min(INT64_MIN), Max(INT64_MAX) {}
3849
  MDSignedField(int64_t Default, int64_t Min, int64_t Max)
3850
3.96k
      : ImplTy(Default), Min(Min), Max(Max) {}
3851
};
3852
3853
struct MDBoolField : public MDFieldImpl<bool> {
3854
45.9k
  MDBoolField(bool Default = false) : ImplTy(Default) {}
3855
};
3856
3857
struct MDField : public MDFieldImpl<Metadata *> {
3858
  bool AllowNull;
3859
3860
114k
  MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
3861
};
3862
3863
struct MDConstant : public MDFieldImpl<ConstantAsMetadata *> {
3864
0
  MDConstant() : ImplTy(nullptr) {}
3865
};
3866
3867
struct MDStringField : public MDFieldImpl<MDString *> {
3868
  bool AllowEmpty;
3869
  MDStringField(bool AllowEmpty = true)
3870
52.5k
      : ImplTy(nullptr), AllowEmpty(AllowEmpty) {}
3871
};
3872
3873
struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
3874
39
  MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
3875
};
3876
3877
struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
3878
3.10k
  ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
3879
};
3880
3881
struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
3882
  MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
3883
0
      : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
3884
3885
  MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
3886
                    bool AllowNull = true)
3887
238
      : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
3888
3889
232
  bool isMDSignedField() const { return WhatIs == IsTypeA; }
3890
22
  bool isMDField() const { return WhatIs == IsTypeB; }
3891
210
  int64_t getMDSignedValue() const {
3892
210
    assert(isMDSignedField() && "Wrong field type");
3893
210
    return A.Val;
3894
210
  }
3895
22
  Metadata *getMDFieldValue() const {
3896
22
    assert(isMDField() && "Wrong field type");
3897
22
    return B.Val;
3898
22
  }
3899
};
3900
3901
struct MDSignedOrUnsignedField
3902
    : MDEitherFieldImpl<MDSignedField, MDUnsignedField> {
3903
11.8k
  MDSignedOrUnsignedField() : ImplTy(MDSignedField(0), MDUnsignedField(0)) {}
3904
3905
11.9k
  bool isMDSignedField() const { return WhatIs == IsTypeA; }
3906
0
  bool isMDUnsignedField() const { return WhatIs == IsTypeB; }
3907
30
  int64_t getMDSignedValue() const {
3908
30
    assert(isMDSignedField() && "Wrong field type");
3909
30
    return A.Val;
3910
30
  }
3911
11.8k
  uint64_t getMDUnsignedValue() const {
3912
11.8k
    assert(isMDUnsignedField() && "Wrong field type");
3913
11.8k
    return B.Val;
3914
11.8k
  }
3915
};
3916
3917
} // end anonymous namespace
3918
3919
namespace llvm {
3920
3921
template <>
3922
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3923
58.5k
                            MDUnsignedField &Result) {
3924
58.5k
  if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
3925
1
    return TokError("expected unsigned integer");
3926
58.5k
3927
58.5k
  auto &U = Lex.getAPSIntVal();
3928
58.5k
  if (U.ugt(Result.Max))
3929
6
    return TokError("value for '" + Name + "' too large, limit is " +
3930
6
                    Twine(Result.Max));
3931
58.4k
  Result.assign(U.getZExtValue());
3932
58.4k
  assert(Result.Val <= Result.Max && "Expected value in range");
3933
58.4k
  Lex.Lex();
3934
58.4k
  return false;
3935
58.4k
}
3936
3937
template <>
3938
24.3k
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, LineField &Result) {
3939
24.3k
  return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3940
24.3k
}
3941
template <>
3942
9.21k
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
3943
9.21k
  return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3944
9.21k
}
3945
3946
template <>
3947
4.85k
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
3948
4.85k
  if (Lex.getKind() == lltok::APSInt)
3949
22
    return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3950
4.83k
3951
4.83k
  if (Lex.getKind() != lltok::DwarfTag)
3952
1
    return TokError("expected DWARF tag");
3953
4.83k
3954
4.83k
  unsigned Tag = dwarf::getTag(Lex.getStrVal());
3955
4.83k
  if (Tag == dwarf::DW_TAG_invalid)
3956
1
    return TokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
3957
4.82k
  assert(Tag <= Result.Max && "Expected valid DWARF tag");
3958
4.82k
3959
4.82k
  Result.assign(Tag);
3960
4.82k
  Lex.Lex();
3961
4.82k
  return false;
3962
4.82k
}
3963
3964
template <>
3965
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3966
23
                            DwarfMacinfoTypeField &Result) {
3967
23
  if (Lex.getKind() == lltok::APSInt)
3968
0
    return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3969
23
3970
23
  if (Lex.getKind() != lltok::DwarfMacinfo)
3971
0
    return TokError("expected DWARF macinfo type");
3972
23
3973
23
  unsigned Macinfo = dwarf::getMacinfo(Lex.getStrVal());
3974
23
  if (Macinfo == dwarf::DW_MACINFO_invalid)
3975
0
    return TokError(
3976
0
        "invalid DWARF macinfo type" + Twine(" '") + Lex.getStrVal() + "'");
3977
23
  assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
3978
23
3979
23
  Result.assign(Macinfo);
3980
23
  Lex.Lex();
3981
23
  return false;
3982
23
}
3983
3984
template <>
3985
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3986
80
                            DwarfVirtualityField &Result) {
3987
80
  if (Lex.getKind() == lltok::APSInt)
3988
0
    return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3989
80
3990
80
  if (Lex.getKind() != lltok::DwarfVirtuality)
3991
0
    return TokError("expected DWARF virtuality code");
3992
80
3993
80
  unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
3994
80
  if (Virtuality == dwarf::DW_VIRTUALITY_invalid)
3995
0
    return TokError("invalid DWARF virtuality code" + Twine(" '") +
3996
0
                    Lex.getStrVal() + "'");
3997
80
  assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
3998
80
  Result.assign(Virtuality);
3999
80
  Lex.Lex();
4000
80
  return false;
4001
80
}
4002
4003
template <>
4004
2.46k
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
4005
2.46k
  if (Lex.getKind() == lltok::APSInt)
4006
14
    return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4007
2.44k
4008
2.44k
  if (Lex.getKind() != lltok::DwarfLang)
4009
0
    return TokError("expected DWARF language");
4010
2.44k
4011
2.44k
  unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
4012
2.44k
  if (!Lang)
4013
1
    return TokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
4014
1
                    "'");
4015
2.44k
  assert(Lang <= Result.Max && "Expected valid DWARF language");
4016
2.44k
  Result.assign(Lang);
4017
2.44k
  Lex.Lex();
4018
2.44k
  return false;
4019
2.44k
}
4020
4021
template <>
4022
90
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
4023
90
  if (Lex.getKind() == lltok::APSInt)
4024
0
    return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4025
90
4026
90
  if (Lex.getKind() != lltok::DwarfCC)
4027
0
    return TokError("expected DWARF calling convention");
4028
90
4029
90
  unsigned CC = dwarf::getCallingConvention(Lex.getStrVal());
4030
90
  if (!CC)
4031
0
    return TokError("invalid DWARF calling convention" + Twine(" '") + Lex.getStrVal() +
4032
0
                    "'");
4033
90
  assert(CC <= Result.Max && "Expected valid DWARF calling convention");
4034
90
  Result.assign(CC);
4035
90
  Lex.Lex();
4036
90
  return false;
4037
90
}
4038
4039
template <>
4040
2.34k
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, EmissionKindField &Result) {
4041
2.34k
  if (Lex.getKind() == lltok::APSInt)
4042
60
    return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4043
2.28k
4044
2.28k
  if (Lex.getKind() != lltok::EmissionKind)
4045
0
    return TokError("expected emission kind");
4046
2.28k
4047
2.28k
  auto Kind = DICompileUnit::getEmissionKind(Lex.getStrVal());
4048
2.28k
  if (!Kind)
4049
0
    return TokError("invalid emission kind" + Twine(" '") + Lex.getStrVal() +
4050
0
                    "'");
4051
2.28k
  assert(*Kind <= Result.Max && "Expected valid emission kind");
4052
2.28k
  Result.assign(*Kind);
4053
2.28k
  Lex.Lex();
4054
2.28k
  return false;
4055
2.28k
}
4056
4057
template <>
4058
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
4059
285
                            NameTableKindField &Result) {
4060
285
  if (Lex.getKind() == lltok::APSInt)
4061
0
    return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4062
285
4063
285
  if (Lex.getKind() != lltok::NameTableKind)
4064
0
    return TokError("expected nameTable kind");
4065
285
4066
285
  auto Kind = DICompileUnit::getNameTableKind(Lex.getStrVal());
4067
285
  if (!Kind)
4068
0
    return TokError("invalid nameTable kind" + Twine(" '") + Lex.getStrVal() +
4069
0
                    "'");
4070
285
  assert(((unsigned)*Kind) <= Result.Max && "Expected valid nameTable kind");
4071
285
  Result.assign((unsigned)*Kind);
4072
285
  Lex.Lex();
4073
285
  return false;
4074
285
}
4075
4076
template <>
4077
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
4078
1.91k
                            DwarfAttEncodingField &Result) {
4079
1.91k
  if (Lex.getKind() == lltok::APSInt)
4080
3
    return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4081
1.91k
4082
1.91k
  if (Lex.getKind() != lltok::DwarfAttEncoding)
4083
0
    return TokError("expected DWARF type attribute encoding");
4084
1.91k
4085
1.91k
  unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
4086
1.91k
  if (!Encoding)
4087
0
    return TokError("invalid DWARF type attribute encoding" + Twine(" '") +
4088
0
                    Lex.getStrVal() + "'");
4089
1.91k
  assert(Encoding <= Result.Max && "Expected valid DWARF language");
4090
1.91k
  Result.assign(Encoding);
4091
1.91k
  Lex.Lex();
4092
1.91k
  return false;
4093
1.91k
}
4094
4095
/// DIFlagField
4096
///  ::= uint32
4097
///  ::= DIFlagVector
4098
///  ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
4099
template <>
4100
3.81k
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
4101
3.81k
4102
3.81k
  // Parser for a single flag.
4103
4.63k
  auto parseFlag = [&](DINode::DIFlags &Val) {
4104
4.63k
    if (Lex.getKind() == lltok::APSInt && 
!Lex.getAPSIntVal().isSigned()2
) {
4105
2
      uint32_t TempVal = static_cast<uint32_t>(Val);
4106
2
      bool Res = ParseUInt32(TempVal);
4107
2
      Val = static_cast<DINode::DIFlags>(TempVal);
4108
2
      return Res;
4109
2
    }
4110
4.63k
4111
4.63k
    if (Lex.getKind() != lltok::DIFlag)
4112
0
      return TokError("expected debug info flag");
4113
4.63k
4114
4.63k
    Val = DINode::getFlag(Lex.getStrVal());
4115
4.63k
    if (!Val)
4116
0
      return TokError(Twine("invalid debug info flag flag '") +
4117
0
                      Lex.getStrVal() + "'");
4118
4.63k
    Lex.Lex();
4119
4.63k
    return false;
4120
4.63k
  };
4121
3.81k
4122
3.81k
  // Parse the flags and combine them together.
4123
3.81k
  DINode::DIFlags Combined = DINode::FlagZero;
4124
4.63k
  do {
4125
4.63k
    DINode::DIFlags Val;
4126
4.63k
    if (parseFlag(Val))
4127
0
      return true;
4128
4.63k
    Combined |= Val;
4129
4.63k
  } while (EatIfPresent(lltok::bar));
4130
3.81k
4131
3.81k
  Result.assign(Combined);
4132
3.81k
  return false;
4133
3.81k
}
4134
4135
/// DISPFlagField
4136
///  ::= uint32
4137
///  ::= DISPFlagVector
4138
///  ::= DISPFlagVector '|' DISPFlag* '|' uint32
4139
template <>
4140
395
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DISPFlagField &Result) {
4141
395
4142
395
  // Parser for a single flag.
4143
608
  auto parseFlag = [&](DISubprogram::DISPFlags &Val) {
4144
608
    if (Lex.getKind() == lltok::APSInt && 
!Lex.getAPSIntVal().isSigned()62
) {
4145
62
      uint32_t TempVal = static_cast<uint32_t>(Val);
4146
62
      bool Res = ParseUInt32(TempVal);
4147
62
      Val = static_cast<DISubprogram::DISPFlags>(TempVal);
4148
62
      return Res;
4149
62
    }
4150
546
4151
546
    if (Lex.getKind() != lltok::DISPFlag)
4152
0
      return TokError("expected debug info flag");
4153
546
4154
546
    Val = DISubprogram::getFlag(Lex.getStrVal());
4155
546
    if (!Val)
4156
0
      return TokError(Twine("invalid subprogram debug info flag '") +
4157
0
                      Lex.getStrVal() + "'");
4158
546
    Lex.Lex();
4159
546
    return false;
4160
546
  };
4161
395
4162
395
  // Parse the flags and combine them together.
4163
395
  DISubprogram::DISPFlags Combined = DISubprogram::SPFlagZero;
4164
608
  do {
4165
608
    DISubprogram::DISPFlags Val;
4166
608
    if (parseFlag(Val))
4167
0
      return true;
4168
608
    Combined |= Val;
4169
608
  } while (EatIfPresent(lltok::bar));
4170
395
4171
395
  Result.assign(Combined);
4172
395
  return false;
4173
395
}
4174
4175
template <>
4176
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
4177
301
                            MDSignedField &Result) {
4178
301
  if (Lex.getKind() != lltok::APSInt)
4179
0
    return TokError("expected signed integer");
4180
301
4181
301
  auto &S = Lex.getAPSIntVal();
4182
301
  if (S < Result.Min)
4183
2
    return TokError("value for '" + Name + "' too small, limit is " +
4184
2
                    Twine(Result.Min));
4185
299
  if (S > Result.Max)
4186
2
    return TokError("value for '" + Name + "' too large, limit is " +
4187
2
                    Twine(Result.Max));
4188
297
  Result.assign(S.getExtValue());
4189
297
  assert(Result.Val >= Result.Min && "Expected value in range");
4190
297
  assert(Result.Val <= Result.Max && "Expected value in range");
4191
297
  Lex.Lex();
4192
297
  return false;
4193
297
}
4194
4195
template <>
4196
14.2k
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
4197
14.2k
  switch (Lex.getKind()) {
4198
14.2k
  default:
4199
0
    return TokError("expected 'true' or 'false'");
4200
14.2k
  case lltok::kw_true:
4201
6.68k
    Result.assign(true);
4202
6.68k
    break;
4203
14.2k
  case lltok::kw_false:
4204
7.52k
    Result.assign(false);
4205
7.52k
    break;
4206
14.2k
  }
4207
14.2k
  Lex.Lex();
4208
14.2k
  return false;
4209
14.2k
}
4210
4211
template <>
4212
64.2k
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
4213
64.2k
  if (Lex.getKind() == lltok::kw_null) {
4214
788
    if (!Result.AllowNull)
4215
6
      return TokError("'" + Name + "' cannot be null");
4216
782
    Lex.Lex();
4217
782
    Result.assign(nullptr);
4218
782
    return false;
4219
782
  }
4220
63.4k
4221
63.4k
  Metadata *MD;
4222
63.4k
  if (ParseMetadata(MD, nullptr))
4223
0
    return true;
4224
63.4k
4225
63.4k
  Result.assign(MD);
4226
63.4k
  return false;
4227
63.4k
}
4228
4229
template <>
4230
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
4231
237
                            MDSignedOrMDField &Result) {
4232
237
  // Try to parse a signed int.
4233
237
  if (Lex.getKind() == lltok::APSInt) {
4234
214
    MDSignedField Res = Result.A;
4235
214
    if (!ParseMDField(Loc, Name, Res)) {
4236
212
      Result.assign(Res);
4237
212
      return false;
4238
212
    }
4239
2
    return true;
4240
2
  }
4241
23
4242
23
  // Otherwise, try to parse as an MDField.
4243
23
  MDField Res = Result.B;
4244
23
  if (!ParseMDField(Loc, Name, Res)) {
4245
22
    Result.assign(Res);
4246
22
    return false;
4247
22
  }
4248
1
4249
1
  return true;
4250
1
}
4251
4252
template <>
4253
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
4254
11.8k
                            MDSignedOrUnsignedField &Result) {
4255
11.8k
  if (Lex.getKind() != lltok::APSInt)
4256
0
    return false;
4257
11.8k
4258
11.8k
  if (Lex.getAPSIntVal().isSigned()) {
4259
30
    MDSignedField Res = Result.A;
4260
30
    if (ParseMDField(Loc, Name, Res))
4261
0
      return true;
4262
30
    Result.assign(Res);
4263
30
    return false;
4264
30
  }
4265
11.8k
4266
11.8k
  MDUnsignedField Res = Result.B;
4267
11.8k
  if (ParseMDField(Loc, Name, Res))
4268
0
    return true;
4269
11.8k
  Result.assign(Res);
4270
11.8k
  return false;
4271
11.8k
}
4272
4273
template <>
4274
35.2k
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
4275
35.2k
  LocTy ValueLoc = Lex.getLoc();
4276
35.2k
  std::string S;
4277
35.2k
  if (ParseStringConstant(S))
4278
0
    return true;
4279
35.2k
4280
35.2k
  if (!Result.AllowEmpty && 
S.empty()1.23k
)
4281
1
    return Error(ValueLoc, "'" + Name + "' cannot be empty");
4282
35.2k
4283
35.2k
  Result.assign(S.empty() ? 
nullptr419
:
MDString::get(Context, S)34.8k
);
4284
35.2k
  return false;
4285
35.2k
}
4286
4287
template <>
4288
12
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
4289
12
  SmallVector<Metadata *, 4> MDs;
4290
12
  if (ParseMDNodeVector(MDs))
4291
0
    return true;
4292
12
4293
12
  Result.assign(std::move(MDs));
4294
12
  return false;
4295
12
}
4296
4297
template <>
4298
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
4299
225
                            ChecksumKindField &Result) {
4300
225
  Optional<DIFile::ChecksumKind> CSKind =
4301
225
      DIFile::getChecksumKind(Lex.getStrVal());
4302
225
4303
225
  if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
4304
0
    return TokError(
4305
0
        "invalid checksum kind" + Twine(" '") + Lex.getStrVal() + "'");
4306
225
4307
225
  Result.assign(*CSKind);
4308
225
  Lex.Lex();
4309
225
  return false;
4310
225
}
4311
4312
} // end namespace llvm
4313
4314
template <class ParserTy>
4315
48.0k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
188k
  do {
4317
188k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
188k
4320
188k
    if (parseField())
4321
23
      return true;
4322
188k
  } while (EatIfPresent(lltok::comma));
4323
48.0k
4324
48.0k
  
return false48.0k
;
4325
48.0k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDILocation(llvm::MDNode*&, bool)::$_0>(llvm::LLParser::ParseDILocation(llvm::MDNode*&, bool)::$_0)
Line
Count
Source
4315
10.6k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
29.8k
  do {
4317
29.8k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
29.8k
4320
29.8k
    if (parseField())
4321
5
      return true;
4322
29.8k
  } while (EatIfPresent(lltok::comma));
4323
10.6k
4324
10.6k
  
return false10.6k
;
4325
10.6k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseGenericDINode(llvm::MDNode*&, bool)::$_1>(llvm::LLParser::ParseGenericDINode(llvm::MDNode*&, bool)::$_1)
Line
Count
Source
4315
39
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
63
  do {
4317
63
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
63
4320
63
    if (parseField())
4321
3
      return true;
4322
60
  } while (EatIfPresent(lltok::comma));
4323
39
4324
39
  
return false36
;
4325
39
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDISubrange(llvm::MDNode*&, bool)::$_2>(llvm::LLParser::ParseDISubrange(llvm::MDNode*&, bool)::$_2)
Line
Count
Source
4315
238
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
276
  do {
4317
276
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
276
4320
276
    if (parseField())
4321
5
      return true;
4322
271
  } while (EatIfPresent(lltok::comma));
4323
238
4324
238
  
return false233
;
4325
238
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIEnumerator(llvm::MDNode*&, bool)::$_3>(llvm::LLParser::ParseDIEnumerator(llvm::MDNode*&, bool)::$_3)
Line
Count
Source
4315
11.8k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
23.7k
  do {
4317
23.7k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
23.7k
4320
23.7k
    if (parseField())
4321
0
      return true;
4322
23.7k
  } while (EatIfPresent(lltok::comma));
4323
11.8k
4324
11.8k
  return false;
4325
11.8k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIBasicType(llvm::MDNode*&, bool)::$_4>(llvm::LLParser::ParseDIBasicType(llvm::MDNode*&, bool)::$_4)
Line
Count
Source
4315
1.93k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
7.21k
  do {
4317
7.21k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
7.21k
4320
7.21k
    if (parseField())
4321
0
      return true;
4322
7.21k
  } while (EatIfPresent(lltok::comma));
4323
1.93k
4324
1.93k
  return false;
4325
1.93k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIDerivedType(llvm::MDNode*&, bool)::$_5>(llvm::LLParser::ParseDIDerivedType(llvm::MDNode*&, bool)::$_5)
Line
Count
Source
4315
2.81k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
14.7k
  do {
4317
14.7k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
14.7k
4320
14.7k
    if (parseField())
4321
0
      return true;
4322
14.7k
  } while (EatIfPresent(lltok::comma));
4323
2.81k
4324
2.81k
  return false;
4325
2.81k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDICompositeType(llvm::MDNode*&, bool)::$_6>(llvm::LLParser::ParseDICompositeType(llvm::MDNode*&, bool)::$_6)
Line
Count
Source
4315
1.28k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
8.52k
  do {
4317
8.52k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
8.52k
4320
8.52k
    if (parseField())
4321
0
      return true;
4322
8.52k
  } while (EatIfPresent(lltok::comma));
4323
1.28k
4324
1.28k
  return false;
4325
1.28k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDISubroutineType(llvm::MDNode*&, bool)::$_7>(llvm::LLParser::ParseDISubroutineType(llvm::MDNode*&, bool)::$_7)
Line
Count
Source
4315
2.44k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
2.55k
  do {
4317
2.55k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
2.55k
4320
2.55k
    if (parseField())
4321
0
      return true;
4322
2.55k
  } while (EatIfPresent(lltok::comma));
4323
2.44k
4324
2.44k
  return false;
4325
2.44k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIFile(llvm::MDNode*&, bool)::$_8>(llvm::LLParser::ParseDIFile(llvm::MDNode*&, bool)::$_8)
Line
Count
Source
4315
3.10k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
6.69k
  do {
4317
6.69k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
6.69k
4320
6.69k
    if (parseField())
4321
0
      return true;
4322
6.69k
  } while (EatIfPresent(lltok::comma));
4323
3.10k
4324
3.10k
  return false;
4325
3.10k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDICompileUnit(llvm::MDNode*&, bool)::$_9>(llvm::LLParser::ParseDICompileUnit(llvm::MDNode*&, bool)::$_9)
Line
Count
Source
4315
2.42k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
18.6k
  do {
4317
18.6k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
18.6k
4320
18.6k
    if (parseField())
4321
4
      return true;
4322
18.6k
  } while (EatIfPresent(lltok::comma));
4323
2.42k
4324
2.42k
  
return false2.41k
;
4325
2.42k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDISubprogram(llvm::MDNode*&, bool)::$_10>(llvm::LLParser::ParseDISubprogram(llvm::MDNode*&, bool)::$_10)
Line
Count
Source
4315
3.71k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
41.5k
  do {
4317
41.5k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
41.5k
4320
41.5k
    if (parseField())
4321
0
      return true;
4322
41.5k
  } while (EatIfPresent(lltok::comma));
4323
3.71k
4324
3.71k
  return false;
4325
3.71k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDILexicalBlock(llvm::MDNode*&, bool)::$_11>(llvm::LLParser::ParseDILexicalBlock(llvm::MDNode*&, bool)::$_11)
Line
Count
Source
4315
1.25k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
4.90k
  do {
4317
4.90k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
4.90k
4320
4.90k
    if (parseField())
4321
1
      return true;
4322
4.89k
  } while (EatIfPresent(lltok::comma));
4323
1.25k
4324
1.25k
  
return false1.25k
;
4325
1.25k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDILexicalBlockFile(llvm::MDNode*&, bool)::$_12>(llvm::LLParser::ParseDILexicalBlockFile(llvm::MDNode*&, bool)::$_12)
Line
Count
Source
4315
245
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
722
  do {
4317
722
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
722
4320
722
    if (parseField())
4321
1
      return true;
4322
721
  } while (EatIfPresent(lltok::comma));
4323
245
4324
245
  
return false244
;
4325
245
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDICommonBlock(llvm::MDNode*&, bool)::$_13>(llvm::LLParser::ParseDICommonBlock(llvm::MDNode*&, bool)::$_13)
Line
Count
Source
4315
3
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
15
  do {
4317
15
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
15
4320
15
    if (parseField())
4321
0
      return true;
4322
15
  } while (EatIfPresent(lltok::comma));
4323
3
4324
3
  return false;
4325
3
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDINamespace(llvm::MDNode*&, bool)::$_14>(llvm::LLParser::ParseDINamespace(llvm::MDNode*&, bool)::$_14)
Line
Count
Source
4315
114
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
216
  do {
4317
216
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
216
4320
216
    if (parseField())
4321
0
      return true;
4322
216
  } while (EatIfPresent(lltok::comma));
4323
114
4324
114
  return false;
4325
114
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIMacro(llvm::MDNode*&, bool)::$_15>(llvm::LLParser::ParseDIMacro(llvm::MDNode*&, bool)::$_15)
Line
Count
Source
4315
21
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
77
  do {
4317
77
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
77
4320
77
    if (parseField())
4321
0
      return true;
4322
77
  } while (EatIfPresent(lltok::comma));
4323
21
4324
21
  return false;
4325
21
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIMacroFile(llvm::MDNode*&, bool)::$_16>(llvm::LLParser::ParseDIMacroFile(llvm::MDNode*&, bool)::$_16)
Line
Count
Source
4315
27
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
70
  do {
4317
70
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
70
4320
70
    if (parseField())
4321
0
      return true;
4322
70
  } while (EatIfPresent(lltok::comma));
4323
27
4324
27
  return false;
4325
27
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIModule(llvm::MDNode*&, bool)::$_17>(llvm::LLParser::ParseDIModule(llvm::MDNode*&, bool)::$_17)
Line
Count
Source
4315
23
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
81
  do {
4317
81
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
81
4320
81
    if (parseField())
4321
0
      return true;
4322
81
  } while (EatIfPresent(lltok::comma));
4323
23
4324
23
  return false;
4325
23
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDITemplateTypeParameter(llvm::MDNode*&, bool)::$_18>(llvm::LLParser::ParseDITemplateTypeParameter(llvm::MDNode*&, bool)::$_18)
Line
Count
Source
4315
72
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
119
  do {
4317
119
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
119
4320
119
    if (parseField())
4321
0
      return true;
4322
119
  } while (EatIfPresent(lltok::comma));
4323
72
4324
72
  return false;
4325
72
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDITemplateValueParameter(llvm::MDNode*&, bool)::$_19>(llvm::LLParser::ParseDITemplateValueParameter(llvm::MDNode*&, bool)::$_19)
Line
Count
Source
4315
57
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
164
  do {
4317
164
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
164
4320
164
    if (parseField())
4321
0
      return true;
4322
164
  } while (EatIfPresent(lltok::comma));
4323
57
4324
57
  return false;
4325
57
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIGlobalVariable(llvm::MDNode*&, bool)::$_20>(llvm::LLParser::ParseDIGlobalVariable(llvm::MDNode*&, bool)::$_20)
Line
Count
Source
4315
1.23k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
8.81k
  do {
4317
8.81k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
8.81k
4320
8.81k
    if (parseField())
4321
1
      return true;
4322
8.81k
  } while (EatIfPresent(lltok::comma));
4323
1.23k
4324
1.23k
  
return false1.23k
;
4325
1.23k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDILocalVariable(llvm::MDNode*&, bool)::$_21>(llvm::LLParser::ParseDILocalVariable(llvm::MDNode*&, bool)::$_21)
Line
Count
Source
4315
2.96k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
15.8k
  do {
4317
15.8k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
15.8k
4320
15.8k
    if (parseField())
4321
3
      return true;
4322
15.8k
  } while (EatIfPresent(lltok::comma));
4323
2.96k
4324
2.96k
  
return false2.96k
;
4325
2.96k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDILabel(llvm::MDNode*&, bool)::$_22>(llvm::LLParser::ParseDILabel(llvm::MDNode*&, bool)::$_22)
Line
Count
Source
4315
26
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
104
  do {
4317
104
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
104
4320
104
    if (parseField())
4321
0
      return true;
4322
104
  } while (EatIfPresent(lltok::comma));
4323
26
4324
26
  return false;
4325
26
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIGlobalVariableExpression(llvm::MDNode*&, bool)::$_23>(llvm::LLParser::ParseDIGlobalVariableExpression(llvm::MDNode*&, bool)::$_23)
Line
Count
Source
4315
1.21k
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
2.42k
  do {
4317
2.42k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
2.42k
4320
2.42k
    if (parseField())
4321
0
      return true;
4322
2.42k
  } while (EatIfPresent(lltok::comma));
4323
1.21k
4324
1.21k
  return false;
4325
1.21k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIObjCProperty(llvm::MDNode*&, bool)::$_24>(llvm::LLParser::ParseDIObjCProperty(llvm::MDNode*&, bool)::$_24)
Line
Count
Source
4315
14
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
75
  do {
4317
75
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
75
4320
75
    if (parseField())
4321
0
      return true;
4322
75
  } while (EatIfPresent(lltok::comma));
4323
14
4324
14
  return false;
4325
14
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImplBody<llvm::LLParser::ParseDIImportedEntity(llvm::MDNode*&, bool)::$_25>(llvm::LLParser::ParseDIImportedEntity(llvm::MDNode*&, bool)::$_25)
Line
Count
Source
4315
264
bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
4316
1.28k
  do {
4317
1.28k
    if (Lex.getKind() != lltok::LabelStr)
4318
0
      return TokError("expected field label here");
4319
1.28k
4320
1.28k
    if (parseField())
4321
0
      return true;
4322
1.28k
  } while (EatIfPresent(lltok::comma));
4323
264
4324
264
  return false;
4325
264
}
4326
4327
template <class ParserTy>
4328
48.1k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
48.1k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
48.1k
  Lex.Lex();
4331
48.1k
4332
48.1k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
48.1k
  if (Lex.getKind() != lltok::rparen)
4335
48.0k
    if (ParseMDFieldsImplBody(parseField))
4336
23
      return true;
4337
48.0k
4338
48.0k
  ClosingLoc = Lex.getLoc();
4339
48.0k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
48.0k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDILocation(llvm::MDNode*&, bool)::$_0>(llvm::LLParser::ParseDILocation(llvm::MDNode*&, bool)::$_0, llvm::SMLoc&)
Line
Count
Source
4328
10.6k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
10.6k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
10.6k
  Lex.Lex();
4331
10.6k
4332
10.6k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
10.6k
  if (Lex.getKind() != lltok::rparen)
4335
10.6k
    if (ParseMDFieldsImplBody(parseField))
4336
5
      return true;
4337
10.6k
4338
10.6k
  ClosingLoc = Lex.getLoc();
4339
10.6k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
10.6k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseGenericDINode(llvm::MDNode*&, bool)::$_1>(llvm::LLParser::ParseGenericDINode(llvm::MDNode*&, bool)::$_1, llvm::SMLoc&)
Line
Count
Source
4328
39
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
39
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
39
  Lex.Lex();
4331
39
4332
39
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
39
  if (Lex.getKind() != lltok::rparen)
4335
39
    if (ParseMDFieldsImplBody(parseField))
4336
3
      return true;
4337
36
4338
36
  ClosingLoc = Lex.getLoc();
4339
36
  return ParseToken(lltok::rparen, "expected ')' here");
4340
36
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDISubrange(llvm::MDNode*&, bool)::$_2>(llvm::LLParser::ParseDISubrange(llvm::MDNode*&, bool)::$_2, llvm::SMLoc&)
Line
Count
Source
4328
238
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
238
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
238
  Lex.Lex();
4331
238
4332
238
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
238
  if (Lex.getKind() != lltok::rparen)
4335
238
    if (ParseMDFieldsImplBody(parseField))
4336
5
      return true;
4337
233
4338
233
  ClosingLoc = Lex.getLoc();
4339
233
  return ParseToken(lltok::rparen, "expected ')' here");
4340
233
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIEnumerator(llvm::MDNode*&, bool)::$_3>(llvm::LLParser::ParseDIEnumerator(llvm::MDNode*&, bool)::$_3, llvm::SMLoc&)
Line
Count
Source
4328
11.8k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
11.8k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
11.8k
  Lex.Lex();
4331
11.8k
4332
11.8k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
11.8k
  if (Lex.getKind() != lltok::rparen)
4335
11.8k
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
11.8k
4338
11.8k
  ClosingLoc = Lex.getLoc();
4339
11.8k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
11.8k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIBasicType(llvm::MDNode*&, bool)::$_4>(llvm::LLParser::ParseDIBasicType(llvm::MDNode*&, bool)::$_4, llvm::SMLoc&)
Line
Count
Source
4328
1.94k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
1.94k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
1.94k
  Lex.Lex();
4331
1.94k
4332
1.94k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
1.94k
  if (Lex.getKind() != lltok::rparen)
4335
1.93k
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
1.94k
4338
1.94k
  ClosingLoc = Lex.getLoc();
4339
1.94k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
1.94k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIDerivedType(llvm::MDNode*&, bool)::$_5>(llvm::LLParser::ParseDIDerivedType(llvm::MDNode*&, bool)::$_5, llvm::SMLoc&)
Line
Count
Source
4328
2.81k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
2.81k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
2.81k
  Lex.Lex();
4331
2.81k
4332
2.81k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
2.81k
  if (Lex.getKind() != lltok::rparen)
4335
2.81k
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
2.81k
4338
2.81k
  ClosingLoc = Lex.getLoc();
4339
2.81k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
2.81k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDICompositeType(llvm::MDNode*&, bool)::$_6>(llvm::LLParser::ParseDICompositeType(llvm::MDNode*&, bool)::$_6, llvm::SMLoc&)
Line
Count
Source
4328
1.28k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
1.28k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
1.28k
  Lex.Lex();
4331
1.28k
4332
1.28k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
1.28k
  if (Lex.getKind() != lltok::rparen)
4335
1.28k
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
1.28k
4338
1.28k
  ClosingLoc = Lex.getLoc();
4339
1.28k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
1.28k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDISubroutineType(llvm::MDNode*&, bool)::$_7>(llvm::LLParser::ParseDISubroutineType(llvm::MDNode*&, bool)::$_7, llvm::SMLoc&)
Line
Count
Source
4328
2.44k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
2.44k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
2.44k
  Lex.Lex();
4331
2.44k
4332
2.44k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
2.44k
  if (Lex.getKind() != lltok::rparen)
4335
2.44k
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
2.44k
4338
2.44k
  ClosingLoc = Lex.getLoc();
4339
2.44k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
2.44k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIFile(llvm::MDNode*&, bool)::$_8>(llvm::LLParser::ParseDIFile(llvm::MDNode*&, bool)::$_8, llvm::SMLoc&)
Line
Count
Source
4328
3.10k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
3.10k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
3.10k
  Lex.Lex();
4331
3.10k
4332
3.10k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
3.10k
  if (Lex.getKind() != lltok::rparen)
4335
3.10k
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
3.10k
4338
3.10k
  ClosingLoc = Lex.getLoc();
4339
3.10k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
3.10k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDICompileUnit(llvm::MDNode*&, bool)::$_9>(llvm::LLParser::ParseDICompileUnit(llvm::MDNode*&, bool)::$_9, llvm::SMLoc&)
Line
Count
Source
4328
2.42k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
2.42k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
2.42k
  Lex.Lex();
4331
2.42k
4332
2.42k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
2.42k
  if (Lex.getKind() != lltok::rparen)
4335
2.42k
    if (ParseMDFieldsImplBody(parseField))
4336
4
      return true;
4337
2.41k
4338
2.41k
  ClosingLoc = Lex.getLoc();
4339
2.41k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
2.41k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDISubprogram(llvm::MDNode*&, bool)::$_10>(llvm::LLParser::ParseDISubprogram(llvm::MDNode*&, bool)::$_10, llvm::SMLoc&)
Line
Count
Source
4328
3.72k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
3.72k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
3.72k
  Lex.Lex();
4331
3.72k
4332
3.72k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
3.72k
  if (Lex.getKind() != lltok::rparen)
4335
3.71k
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
3.72k
4338
3.72k
  ClosingLoc = Lex.getLoc();
4339
3.72k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
3.72k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDILexicalBlock(llvm::MDNode*&, bool)::$_11>(llvm::LLParser::ParseDILexicalBlock(llvm::MDNode*&, bool)::$_11, llvm::SMLoc&)
Line
Count
Source
4328
1.25k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
1.25k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
1.25k
  Lex.Lex();
4331
1.25k
4332
1.25k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
1.25k
  if (Lex.getKind() != lltok::rparen)
4335
1.25k
    if (ParseMDFieldsImplBody(parseField))
4336
1
      return true;
4337
1.25k
4338
1.25k
  ClosingLoc = Lex.getLoc();
4339
1.25k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
1.25k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDILexicalBlockFile(llvm::MDNode*&, bool)::$_12>(llvm::LLParser::ParseDILexicalBlockFile(llvm::MDNode*&, bool)::$_12, llvm::SMLoc&)
Line
Count
Source
4328
245
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
245
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
245
  Lex.Lex();
4331
245
4332
245
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
245
  if (Lex.getKind() != lltok::rparen)
4335
245
    if (ParseMDFieldsImplBody(parseField))
4336
1
      return true;
4337
244
4338
244
  ClosingLoc = Lex.getLoc();
4339
244
  return ParseToken(lltok::rparen, "expected ')' here");
4340
244
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDICommonBlock(llvm::MDNode*&, bool)::$_13>(llvm::LLParser::ParseDICommonBlock(llvm::MDNode*&, bool)::$_13, llvm::SMLoc&)
Line
Count
Source
4328
3
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
3
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
3
  Lex.Lex();
4331
3
4332
3
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
3
  if (Lex.getKind() != lltok::rparen)
4335
3
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
3
4338
3
  ClosingLoc = Lex.getLoc();
4339
3
  return ParseToken(lltok::rparen, "expected ')' here");
4340
3
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDINamespace(llvm::MDNode*&, bool)::$_14>(llvm::LLParser::ParseDINamespace(llvm::MDNode*&, bool)::$_14, llvm::SMLoc&)
Line
Count
Source
4328
114
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
114
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
114
  Lex.Lex();
4331
114
4332
114
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
114
  if (Lex.getKind() != lltok::rparen)
4335
114
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
114
4338
114
  ClosingLoc = Lex.getLoc();
4339
114
  return ParseToken(lltok::rparen, "expected ')' here");
4340
114
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIMacro(llvm::MDNode*&, bool)::$_15>(llvm::LLParser::ParseDIMacro(llvm::MDNode*&, bool)::$_15, llvm::SMLoc&)
Line
Count
Source
4328
21
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
21
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
21
  Lex.Lex();
4331
21
4332
21
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
21
  if (Lex.getKind() != lltok::rparen)
4335
21
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
21
4338
21
  ClosingLoc = Lex.getLoc();
4339
21
  return ParseToken(lltok::rparen, "expected ')' here");
4340
21
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIMacroFile(llvm::MDNode*&, bool)::$_16>(llvm::LLParser::ParseDIMacroFile(llvm::MDNode*&, bool)::$_16, llvm::SMLoc&)
Line
Count
Source
4328
27
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
27
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
27
  Lex.Lex();
4331
27
4332
27
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
27
  if (Lex.getKind() != lltok::rparen)
4335
27
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
27
4338
27
  ClosingLoc = Lex.getLoc();
4339
27
  return ParseToken(lltok::rparen, "expected ')' here");
4340
27
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIModule(llvm::MDNode*&, bool)::$_17>(llvm::LLParser::ParseDIModule(llvm::MDNode*&, bool)::$_17, llvm::SMLoc&)
Line
Count
Source
4328
23
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
23
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
23
  Lex.Lex();
4331
23
4332
23
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
23
  if (Lex.getKind() != lltok::rparen)
4335
23
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
23
4338
23
  ClosingLoc = Lex.getLoc();
4339
23
  return ParseToken(lltok::rparen, "expected ')' here");
4340
23
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDITemplateTypeParameter(llvm::MDNode*&, bool)::$_18>(llvm::LLParser::ParseDITemplateTypeParameter(llvm::MDNode*&, bool)::$_18, llvm::SMLoc&)
Line
Count
Source
4328
72
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
72
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
72
  Lex.Lex();
4331
72
4332
72
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
72
  if (Lex.getKind() != lltok::rparen)
4335
72
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
72
4338
72
  ClosingLoc = Lex.getLoc();
4339
72
  return ParseToken(lltok::rparen, "expected ')' here");
4340
72
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDITemplateValueParameter(llvm::MDNode*&, bool)::$_19>(llvm::LLParser::ParseDITemplateValueParameter(llvm::MDNode*&, bool)::$_19, llvm::SMLoc&)
Line
Count
Source
4328
57
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
57
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
57
  Lex.Lex();
4331
57
4332
57
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
57
  if (Lex.getKind() != lltok::rparen)
4335
57
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
57
4338
57
  ClosingLoc = Lex.getLoc();
4339
57
  return ParseToken(lltok::rparen, "expected ')' here");
4340
57
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIGlobalVariable(llvm::MDNode*&, bool)::$_20>(llvm::LLParser::ParseDIGlobalVariable(llvm::MDNode*&, bool)::$_20, llvm::SMLoc&)
Line
Count
Source
4328
1.23k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
1.23k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
1.23k
  Lex.Lex();
4331
1.23k
4332
1.23k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
1.23k
  if (Lex.getKind() != lltok::rparen)
4335
1.23k
    if (ParseMDFieldsImplBody(parseField))
4336
1
      return true;
4337
1.23k
4338
1.23k
  ClosingLoc = Lex.getLoc();
4339
1.23k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
1.23k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDILocalVariable(llvm::MDNode*&, bool)::$_21>(llvm::LLParser::ParseDILocalVariable(llvm::MDNode*&, bool)::$_21, llvm::SMLoc&)
Line
Count
Source
4328
2.96k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
2.96k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
2.96k
  Lex.Lex();
4331
2.96k
4332
2.96k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
2.96k
  if (Lex.getKind() != lltok::rparen)
4335
2.96k
    if (ParseMDFieldsImplBody(parseField))
4336
3
      return true;
4337
2.96k
4338
2.96k
  ClosingLoc = Lex.getLoc();
4339
2.96k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
2.96k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDILabel(llvm::MDNode*&, bool)::$_22>(llvm::LLParser::ParseDILabel(llvm::MDNode*&, bool)::$_22, llvm::SMLoc&)
Line
Count
Source
4328
26
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
26
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
26
  Lex.Lex();
4331
26
4332
26
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
26
  if (Lex.getKind() != lltok::rparen)
4335
26
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
26
4338
26
  ClosingLoc = Lex.getLoc();
4339
26
  return ParseToken(lltok::rparen, "expected ')' here");
4340
26
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIGlobalVariableExpression(llvm::MDNode*&, bool)::$_23>(llvm::LLParser::ParseDIGlobalVariableExpression(llvm::MDNode*&, bool)::$_23, llvm::SMLoc&)
Line
Count
Source
4328
1.21k
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
1.21k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
1.21k
  Lex.Lex();
4331
1.21k
4332
1.21k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
1.21k
  if (Lex.getKind() != lltok::rparen)
4335
1.21k
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
1.21k
4338
1.21k
  ClosingLoc = Lex.getLoc();
4339
1.21k
  return ParseToken(lltok::rparen, "expected ')' here");
4340
1.21k
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIObjCProperty(llvm::MDNode*&, bool)::$_24>(llvm::LLParser::ParseDIObjCProperty(llvm::MDNode*&, bool)::$_24, llvm::SMLoc&)
Line
Count
Source
4328
21
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
21
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
21
  Lex.Lex();
4331
21
4332
21
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
21
  if (Lex.getKind() != lltok::rparen)
4335
14
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
21
4338
21
  ClosingLoc = Lex.getLoc();
4339
21
  return ParseToken(lltok::rparen, "expected ')' here");
4340
21
}
LLParser.cpp:bool llvm::LLParser::ParseMDFieldsImpl<llvm::LLParser::ParseDIImportedEntity(llvm::MDNode*&, bool)::$_25>(llvm::LLParser::ParseDIImportedEntity(llvm::MDNode*&, bool)::$_25, llvm::SMLoc&)
Line
Count
Source
4328
264
bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4329
264
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4330
264
  Lex.Lex();
4331
264
4332
264
  if (ParseToken(lltok::lparen, "expected '(' here"))
4333
0
    return true;
4334
264
  if (Lex.getKind() != lltok::rparen)
4335
264
    if (ParseMDFieldsImplBody(parseField))
4336
0
      return true;
4337
264
4338
264
  ClosingLoc = Lex.getLoc();
4339
264
  return ParseToken(lltok::rparen, "expected ')' here");
4340
264
}
4341
4342
template <class FieldTy>
4343
188k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
188k
  if (Result.Seen)
4345
1
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
188k
4347
188k
  LocTy Loc = Lex.getLoc();
4348
188k
  Lex.Lex();
4349
188k
  return ParseMDField(Loc, Name, Result);
4350
188k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::LineField>(llvm::StringRef, (anonymous namespace)::LineField&)
Line
Count
Source
4343
24.3k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
24.3k
  if (Result.Seen)
4345
1
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
24.3k
4347
24.3k
  LocTy Loc = Lex.getLoc();
4348
24.3k
  Lex.Lex();
4349
24.3k
  return ParseMDField(Loc, Name, Result);
4350
24.3k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::ColumnField>(llvm::StringRef, (anonymous namespace)::ColumnField&)
Line
Count
Source
4343
9.21k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
9.21k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
9.21k
4347
9.21k
  LocTy Loc = Lex.getLoc();
4348
9.21k
  Lex.Lex();
4349
9.21k
  return ParseMDField(Loc, Name, Result);
4350
9.21k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::MDField>(llvm::StringRef, (anonymous namespace)::MDField&)
Line
Count
Source
4343
64.1k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
64.1k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
64.1k
4347
64.1k
  LocTy Loc = Lex.getLoc();
4348
64.1k
  Lex.Lex();
4349
64.1k
  return ParseMDField(Loc, Name, Result);
4350
64.1k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::MDBoolField>(llvm::StringRef, (anonymous namespace)::MDBoolField&)
Line
Count
Source
4343
14.2k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
14.2k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
14.2k
4347
14.2k
  LocTy Loc = Lex.getLoc();
4348
14.2k
  Lex.Lex();
4349
14.2k
  return ParseMDField(Loc, Name, Result);
4350
14.2k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::DwarfTagField>(llvm::StringRef, (anonymous namespace)::DwarfTagField&)
Line
Count
Source
4343
4.85k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
4.85k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
4.85k
4347
4.85k
  LocTy Loc = Lex.getLoc();
4348
4.85k
  Lex.Lex();
4349
4.85k
  return ParseMDField(Loc, Name, Result);
4350
4.85k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::MDStringField>(llvm::StringRef, (anonymous namespace)::MDStringField&)
Line
Count
Source
4343
35.2k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
35.2k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
35.2k
4347
35.2k
  LocTy Loc = Lex.getLoc();
4348
35.2k
  Lex.Lex();
4349
35.2k
  return ParseMDField(Loc, Name, Result);
4350
35.2k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::MDFieldList>(llvm::StringRef, (anonymous namespace)::MDFieldList&)
Line
Count
Source
4343
12
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
12
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
12
4347
12
  LocTy Loc = Lex.getLoc();
4348
12
  Lex.Lex();
4349
12
  return ParseMDField(Loc, Name, Result);
4350
12
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::MDSignedOrMDField>(llvm::StringRef, (anonymous namespace)::MDSignedOrMDField&)
Line
Count
Source
4343
237
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
237
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
237
4347
237
  LocTy Loc = Lex.getLoc();
4348
237
  Lex.Lex();
4349
237
  return ParseMDField(Loc, Name, Result);
4350
237
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::MDSignedField>(llvm::StringRef, (anonymous namespace)::MDSignedField&)
Line
Count
Source
4343
57
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
57
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
57
4347
57
  LocTy Loc = Lex.getLoc();
4348
57
  Lex.Lex();
4349
57
  return ParseMDField(Loc, Name, Result);
4350
57
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::MDSignedOrUnsignedField>(llvm::StringRef, (anonymous namespace)::MDSignedOrUnsignedField&)
Line
Count
Source
4343
11.8k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
11.8k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
11.8k
4347
11.8k
  LocTy Loc = Lex.getLoc();
4348
11.8k
  Lex.Lex();
4349
11.8k
  return ParseMDField(Loc, Name, Result);
4350
11.8k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::MDUnsignedField>(llvm::StringRef, (anonymous namespace)::MDUnsignedField&)
Line
Count
Source
4343
12.9k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
12.9k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
12.9k
4347
12.9k
  LocTy Loc = Lex.getLoc();
4348
12.9k
  Lex.Lex();
4349
12.9k
  return ParseMDField(Loc, Name, Result);
4350
12.9k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::DwarfAttEncodingField>(llvm::StringRef, (anonymous namespace)::DwarfAttEncodingField&)
Line
Count
Source
4343
1.91k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
1.91k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
1.91k
4347
1.91k
  LocTy Loc = Lex.getLoc();
4348
1.91k
  Lex.Lex();
4349
1.91k
  return ParseMDField(Loc, Name, Result);
4350
1.91k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::DIFlagField>(llvm::StringRef, (anonymous namespace)::DIFlagField&)
Line
Count
Source
4343
3.81k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
3.81k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
3.81k
4347
3.81k
  LocTy Loc = Lex.getLoc();
4348
3.81k
  Lex.Lex();
4349
3.81k
  return ParseMDField(Loc, Name, Result);
4350
3.81k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::DwarfLangField>(llvm::StringRef, (anonymous namespace)::DwarfLangField&)
Line
Count
Source
4343
2.46k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
2.46k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
2.46k
4347
2.46k
  LocTy Loc = Lex.getLoc();
4348
2.46k
  Lex.Lex();
4349
2.46k
  return ParseMDField(Loc, Name, Result);
4350
2.46k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::DwarfCCField>(llvm::StringRef, (anonymous namespace)::DwarfCCField&)
Line
Count
Source
4343
90
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
90
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
90
4347
90
  LocTy Loc = Lex.getLoc();
4348
90
  Lex.Lex();
4349
90
  return ParseMDField(Loc, Name, Result);
4350
90
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::ChecksumKindField>(llvm::StringRef, (anonymous namespace)::ChecksumKindField&)
Line
Count
Source
4343
225
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
225
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
225
4347
225
  LocTy Loc = Lex.getLoc();
4348
225
  Lex.Lex();
4349
225
  return ParseMDField(Loc, Name, Result);
4350
225
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::EmissionKindField>(llvm::StringRef, (anonymous namespace)::EmissionKindField&)
Line
Count
Source
4343
2.34k
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
2.34k
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
2.34k
4347
2.34k
  LocTy Loc = Lex.getLoc();
4348
2.34k
  Lex.Lex();
4349
2.34k
  return ParseMDField(Loc, Name, Result);
4350
2.34k
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::NameTableKindField>(llvm::StringRef, (anonymous namespace)::NameTableKindField&)
Line
Count
Source
4343
285
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
285
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
285
4347
285
  LocTy Loc = Lex.getLoc();
4348
285
  Lex.Lex();
4349
285
  return ParseMDField(Loc, Name, Result);
4350
285
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::DwarfVirtualityField>(llvm::StringRef, (anonymous namespace)::DwarfVirtualityField&)
Line
Count
Source
4343
80
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
80
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
80
4347
80
  LocTy Loc = Lex.getLoc();
4348
80
  Lex.Lex();
4349
80
  return ParseMDField(Loc, Name, Result);
4350
80
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::DISPFlagField>(llvm::StringRef, (anonymous namespace)::DISPFlagField&)
Line
Count
Source
4343
395
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
395
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
395
4347
395
  LocTy Loc = Lex.getLoc();
4348
395
  Lex.Lex();
4349
395
  return ParseMDField(Loc, Name, Result);
4350
395
}
LLParser.cpp:bool llvm::LLParser::ParseMDField<(anonymous namespace)::DwarfMacinfoTypeField>(llvm::StringRef, (anonymous namespace)::DwarfMacinfoTypeField&)
Line
Count
Source
4343
23
bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4344
23
  if (Result.Seen)
4345
0
    return TokError("field '" + Name + "' cannot be specified more than once");
4346
23
4347
23
  LocTy Loc = Lex.getLoc();
4348
23
  Lex.Lex();
4349
23
  return ParseMDField(Loc, Name, Result);
4350
23
}
4351
4352
55.9k
bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
4353
55.9k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4354
55.9k
4355
55.9k
#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)                                  \
4356
407k
  if (Lex.getStrVal() == #CLASS)                                               \
4357
407k
    
return Parse##CLASS(N, IsDistinct)55.9k
;
4358
351k
#include 
"llvm/IR/Metadata.def"55.9k
4359
351k
4360
351k
  
return TokError("expected metadata type")1
;
4361
351k
}
4362
4363
350k
#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
4364
#define NOP_FIELD(NAME, TYPE, INIT)
4365
#define REQUIRE_FIELD(NAME, TYPE, INIT)                                        \
4366
64.4k
  if (!NAME.Seen)                                                              \
4367
64.4k
    
return Error(ClosingLoc, "missing required field '" #NAME "'")23
;
4368
#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT)                                    \
4369
845k
  if (Lex.getStrVal() == #NAME)                                                \
4370
845k
    
return ParseMDField(#NAME, NAME)188k
;
4371
#define PARSE_MD_FIELDS()                                                      \
4372
284k
  
VISIT_MD_FIELDS48.1k
(DECLARE_FIELD,
DECLARE_FIELD64.4k
) \
4373
50.5k
  do {                                                                         \
4374
48.1k
    LocTy ClosingLoc;                                                          \
4375
188k
    if (
ParseMDFieldsImpl([&]() -> bool 48.1k
{ \
4376
682k
      
VISIT_MD_FIELDS188k
(1
PARSE_MD_FIELD,
PARSE_MD_FIELD162k
) \
4377
1
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
656k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDILocation(llvm::MDNode*&, bool)::$_0::operator()() const
Line
Count
Source
4375
29.8k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
29.8k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
1
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
31.1k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseGenericDINode(llvm::MDNode*&, bool)::$_1::operator()() const
Line
Count
Source
4375
63
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
63
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
37
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDISubrange(llvm::MDNode*&, bool)::$_2::operator()() const
Line
Count
Source
4375
276
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
276
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
39
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIEnumerator(llvm::MDNode*&, bool)::$_3::operator()() const
Line
Count
Source
4375
23.7k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
23.7k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
11.9k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIBasicType(llvm::MDNode*&, bool)::$_4::operator()() const
Line
Count
Source
4375
7.21k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
7.21k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
16.4k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIDerivedType(llvm::MDNode*&, bool)::$_5::operator()() const
Line
Count
Source
4375
14.7k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
14.7k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
55.5k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDICompositeType(llvm::MDNode*&, bool)::$_6::operator()() const
Line
Count
Source
4375
8.52k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
8.52k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
42.9k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDISubroutineType(llvm::MDNode*&, bool)::$_7::operator()() const
Line
Count
Source
4375
2.55k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
2.55k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
4.97k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIFile(llvm::MDNode*&, bool)::$_8::operator()() const
Line
Count
Source
4375
6.69k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
6.69k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
4.42k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDICompileUnit(llvm::MDNode*&, bool)::$_9::operator()() const
Line
Count
Source
4375
18.6k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
18.6k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
88.1k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDISubprogram(llvm::MDNode*&, bool)::$_10::operator()() const
Line
Count
Source
4375
41.5k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
41.5k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
314k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDILexicalBlock(llvm::MDNode*&, bool)::$_11::operator()() const
Line
Count
Source
4375
4.90k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
4.90k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
7.19k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDILexicalBlockFile(llvm::MDNode*&, bool)::$_12::operator()() const
Line
Count
Source
4375
722
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
722
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
721
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDICommonBlock(llvm::MDNode*&, bool)::$_13::operator()() const
Line
Count
Source
4375
15
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
15
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
30
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDINamespace(llvm::MDNode*&, bool)::$_14::operator()() const
Line
Count
Source
4375
216
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
216
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
113
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIMacro(llvm::MDNode*&, bool)::$_15::operator()() const
Line
Count
Source
4375
77
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
77
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
115
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIMacroFile(llvm::MDNode*&, bool)::$_16::operator()() const
Line
Count
Source
4375
70
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
70
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
135
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIModule(llvm::MDNode*&, bool)::$_17::operator()() const
Line
Count
Source
4375
81
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
81
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
129
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDITemplateTypeParameter(llvm::MDNode*&, bool)::$_18::operator()() const
Line
Count
Source
4375
119
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
119
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
71
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDITemplateValueParameter(llvm::MDNode*&, bool)::$_19::operator()() const
Line
Count
Source
4375
164
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
164
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
315
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIGlobalVariable(llvm::MDNode*&, bool)::$_20::operator()() const
Line
Count
Source
4375
8.81k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
8.81k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
32.7k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDILocalVariable(llvm::MDNode*&, bool)::$_21::operator()() const
Line
Count
Source
4375
15.8k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
15.8k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
40.6k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDILabel(llvm::MDNode*&, bool)::$_22::operator()() const
Line
Count
Source
4375
104
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
104
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
156
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIGlobalVariableExpression(llvm::MDNode*&, bool)::$_23::operator()() const
Line
Count
Source
4375
2.42k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
2.42k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
1.21k
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIObjCProperty(llvm::MDNode*&, bool)::$_24::operator()() const
Line
Count
Source
4375
75
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
75
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
221
    }, ClosingLoc))                                                            \
LLParser.cpp:llvm::LLParser::ParseDIImportedEntity(llvm::MDNode*&, bool)::$_25::operator()() const
Line
Count
Source
4375
1.28k
    if (ParseMDFieldsImpl([&]() -> bool {                                      \
4376
1.28k
      VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
4377
0
      return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
4378
2.56k
    }, ClosingLoc))                                                            \
4379
48.1k
      
return true23
; \
4380
112k
    
VISIT_MD_FIELDS48.1k
(NOP_FIELD,
REQUIRE_FIELD64.4k
) \
4381
112k
  } while (
false48.0k
)
4382
#define GET_OR_DISTINCT(CLASS, ARGS)                                           \
4383
53.3k
  (IsDistinct ? 
CLASS::getDistinct ARGS5.64k
:
CLASS::get ARGS47.7k
)
4384
4385
/// ParseDILocationFields:
4386
///   ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6,
4387
///   isImplicitCode: true)
4388
10.6k
bool LLParser::ParseDILocation(MDNode *&Result, bool IsDistinct) {
4389
10.6k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4390
40.5k
  OPTIONAL(line, LineField, );                                                 \
4391
29.9k
  OPTIONAL(column, ColumnField, );                                             \
4392
32.5k
  REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
4393
11.2k
  OPTIONAL(inlinedAt, MDField, );                                              \
4394
10.6k
  OPTIONAL(isImplicitCode, MDBoolField, (false))
;1
4395
10.6k
  PARSE_MD_FIELDS();
4396
10.6k
#undef VISIT_MD_FIELDS
4397
10.6k
4398
10.6k
  Result =
4399
10.6k
      GET_OR_DISTINCT(DILocation, (Context, line.Val, column.Val, scope.Val,
4400
10.6k
                                   inlinedAt.Val, isImplicitCode.Val));
4401
10.6k
  return false;
4402
10.6k
}
4403
4404
/// ParseGenericDINode:
4405
///   ::= !GenericDINode(tag: 15, header: "...", operands: {...})
4406
39
bool LLParser::ParseGenericDINode(MDNode *&Result, bool IsDistinct) {
4407
39
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4408
138
  REQUIRED
(tag, DwarfTagField, )36
; \
4409
64
  OPTIONAL(header, MDStringField, );                                           \
4410
51
  OPTIONAL(operands, MDFieldList, )
;0
4411
39
  PARSE_MD_FIELDS();
4412
39
#undef VISIT_MD_FIELDS
4413
39
4414
39
  
Result = 35
GET_OR_DISTINCT35
(GenericDINode,
4415
35
                           (Context, tag.Val, header.Val, operands.Val));
4416
35
  return false;
4417
39
}
4418
4419
/// ParseDISubrange:
4420
///   ::= !DISubrange(count: 30, lowerBound: 2)
4421
///   ::= !DISubrange(count: !node, lowerBound: 2)
4422
238
bool LLParser::ParseDISubrange(MDNode *&Result, bool IsDistinct) {
4423
238
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4424
747
  REQUIRED
(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false))233
; \
4425
277
  OPTIONAL(lowerBound, MDSignedField, )
;0
4426
238
  PARSE_MD_FIELDS();
4427
238
#undef VISIT_MD_FIELDS
4428
238
4429
238
  
if (232
count.isMDSignedField()232
)
4430
210
    Result = GET_OR_DISTINCT(
4431
232
        DISubrange, (Context, count.getMDSignedValue(), lowerBound.Val));
4432
232
  else 
if (22
count.isMDField()22
)
4433
22
    Result = GET_OR_DISTINCT(
4434
22
        DISubrange, (Context, count.getMDFieldValue(), lowerBound.Val));
4435
22
  else
4436
22
    
return true0
;
4437
232
4438
232
  return false;
4439
232
}
4440
4441
/// ParseDIEnumerator:
4442
///   ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind")
4443
11.8k
bool LLParser::ParseDIEnumerator(MDNode *&Result, bool IsDistinct) {
4444
11.8k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4445
47.5k
  REQUIRED
(name, MDStringField, )11.8k
; \
4446
35.6k
  REQUIRED(value, MDSignedOrUnsignedField, );                                  \
4447
11.9k
  OPTIONAL(isUnsigned, MDBoolField, (false))
;0
4448
11.8k
  PARSE_MD_FIELDS();
4449
11.8k
#undef VISIT_MD_FIELDS
4450
11.8k
4451
11.8k
  
if (11.8k
isUnsigned.Val11.8k
&&
value.isMDSignedField()18
)
4452
0
    return TokError("unsigned enumerator with negative value");
4453
11.8k
4454
11.8k
  int64_t Value = value.isMDSignedField()
4455
11.8k
                      ? 
value.getMDSignedValue()30
4456
11.8k
                      : 
static_cast<int64_t>(value.getMDUnsignedValue())11.8k
;
4457
11.8k
  Result =
4458
11.8k
      GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val));
4459
11.8k
4460
11.8k
  return false;
4461
11.8k
}
4462
4463
/// ParseDIBasicType:
4464
///   ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32,
4465
///                    encoding: DW_ATE_encoding, flags: 0)
4466
1.94k
bool LLParser::ParseDIBasicType(MDNode *&Result, bool IsDistinct) {
4467
1.94k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4468
9.15k
  OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type));                     \
4469
8.71k
  OPTIONAL(name, MDStringField, );                                             \
4470
6.79k
  OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX));                            \
4471
4.86k
  OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));                           \
4472
3.87k
  OPTIONAL(encoding, DwarfAttEncodingField, );                                 \
4473
1.95k
  OPTIONAL(flags, DIFlagField, )
;0
4474
1.94k
  PARSE_MD_FIELDS();
4475
1.94k
#undef VISIT_MD_FIELDS
4476
1.94k
4477
1.94k
  Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
4478
1.94k
                                         align.Val, encoding.Val, flags.Val));
4479
1.94k
  return false;
4480
1.94k
}
4481
4482
/// ParseDIDerivedType:
4483
///   ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
4484
///                      line: 7, scope: !1, baseType: !2, size: 32,
4485
///                      align: 32, offset: 0, flags: 0, extraData: !3,
4486
///                      dwarfAddressSpace: 3)
4487
2.81k
bool LLParser::ParseDIDerivedType(MDNode *&Result, bool IsDistinct) {
4488
2.81k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4489
20.3k
  REQUIRED
(tag, DwarfTagField, )2.81k
; \
4490
14.7k
  OPTIONAL(name, MDStringField, );                                             \
4491
13.4k
  OPTIONAL(file, MDField, );                                                   \
4492
12.1k
  OPTIONAL(line, LineField, );                                                 \
4493
11.0k
  OPTIONAL(scope, MDField, );                                                  \
4494
12.6k
  REQUIRED(baseType, MDField, );                                               \
4495
7.06k
  OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX));                            \
4496
4.96k
  OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));                           \
4497
3.89k
  OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX));                          \
4498
3.45k
  OPTIONAL(flags, DIFlagField, );                                              \
4499
2.94k
  OPTIONAL(extraData, MDField, );                                              \
4500
2.83k
  OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX))
;0
4501
2.81k
  PARSE_MD_FIELDS();
4502
2.81k
#undef VISIT_MD_FIELDS
4503
2.81k
4504
2.81k
  Optional<unsigned> DWARFAddressSpace;
4505
2.81k
  if (dwarfAddressSpace.Val != UINT32_MAX)
4506
2.81k
    
DWARFAddressSpace = dwarfAddressSpace.Val22
;
4507
2.81k
4508
2.81k
  Result = GET_OR_DISTINCT(DIDerivedType,
4509
2.81k
                           (Context, tag.Val, name.Val, file.Val, line.Val,
4510
2.81k
                            scope.Val, baseType.Val, size.Val, align.Val,
4511
2.81k
                            offset.Val, DWARFAddressSpace, flags.Val,
4512
2.81k
                            extraData.Val));
4513
2.81k
  return false;
4514
2.81k
}
4515
4516
1.28k
bool LLParser::ParseDICompositeType(MDNode *&Result, bool IsDistinct) {
4517
1.28k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4518
11.1k
  REQUIRED
(tag, DwarfTagField, )1.28k
; \
4519
8.52k
  OPTIONAL(name, MDStringField, );                                             \
4520
7.57k
  OPTIONAL(file, MDField, );                                                   \
4521
6.55k
  OPTIONAL(line, LineField, );                                                 \
4522
5.62k
  OPTIONAL(scope, MDField, );                                                  \
4523
5.38k
  OPTIONAL(baseType, MDField, );                                               \
4524
5.12k
  OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX));                            \
4525
4.05k
  OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));                           \
4526
3.47k
  OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX));                          \
4527
3.45k
  OPTIONAL(flags, DIFlagField, );                                              \
4528
3.17k
  OPTIONAL(elements, MDField, );                                               \
4529
2.10k
  OPTIONAL(runtimeLang, DwarfLangField, );                                     \
4530
2.06k
  OPTIONAL(vtableHolder, MDField, );                                           \
4531
1.97k
  OPTIONAL(templateParams, MDField, );                                         \
4532
1.92k
  OPTIONAL(identifier, MDStringField, );                                       \
4533
1.30k
  OPTIONAL(discriminator, MDField, )
;0
4534
1.28k
  PARSE_MD_FIELDS();
4535
1.28k
#undef VISIT_MD_FIELDS
4536
1.28k
4537
1.28k
  // If this has an identifier try to build an ODR type.
4538
1.28k
  
if (1.28k
identifier.Val1.28k
)
4539
621
    if (auto *CT = DICompositeType::buildODRType(
4540
91
            Context, *identifier.Val, tag.Val, name.Val, file.Val, line.Val,
4541
91
            scope.Val, baseType.Val, size.Val, align.Val, offset.Val, flags.Val,
4542
91
            elements.Val, runtimeLang.Val, vtableHolder.Val,
4543
91
            templateParams.Val, discriminator.Val)) {
4544
91
      Result = CT;
4545
91
      return false;
4546
91
    }
4547
1.19k
4548
1.19k
  // Create a new node, and save it in the context if it belongs in the type
4549
1.19k
  // map.
4550
1.19k
  Result = GET_OR_DISTINCT(
4551
1.19k
      DICompositeType,
4552
1.19k
      (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
4553
1.19k
       size.Val, align.Val, offset.Val, flags.Val, elements.Val,
4554
1.19k
       runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val,
4555
1.19k
       discriminator.Val));
4556
1.19k
  return false;
4557
1.19k
}
4558
4559
2.44k
bool LLParser::ParseDISubroutineType(MDNode *&Result, bool IsDistinct) {
4560
2.44k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4561
5.00k
  OPTIONAL(flags, DIFlagField, );                                              \
4562
4.97k
  OPTIONAL(cc, DwarfCCField, );                                                \
4563
7.33k
  REQUIRED(types, MDField, )
;2.44k
4564
2.44k
  PARSE_MD_FIELDS();
4565
2.44k
#undef VISIT_MD_FIELDS
4566
2.44k
4567
2.44k
  
Result = 2.44k
GET_OR_DISTINCT2.44k
(DISubroutineType,
4568
2.44k
                           (Context, flags.Val, cc.Val, types.Val));
4569
2.44k
  return false;
4570
2.44k
}
4571
4572
/// ParseDIFileType:
4573
///   ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir",
4574
///                   checksumkind: CSK_MD5,
4575
///                   checksum: "000102030405060708090a0b0c0d0e0f",
4576
///                   source: "source file contents")
4577
3.10k
bool LLParser::ParseDIFile(MDNode *&Result, bool IsDistinct) {
4578
3.10k
  // The default constructed value for checksumkind is required, but will never
4579
3.10k
  // be used, as the parser checks if the field was actually Seen before using
4580
3.10k
  // the Val.
4581
3.10k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4582
12.9k
  REQUIRED
(filename, MDStringField, )3.10k
; \
4583
9.80k
  REQUIRED(directory, MDStringField, );                                        \
4584
3.60k
  OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5));                \
4585
3.37k
  OPTIONAL(checksum, MDStringField, );                                         \
4586
3.15k
  OPTIONAL(source, MDStringField, )
;0
4587
3.10k
  PARSE_MD_FIELDS();
4588
3.10k
#undef VISIT_MD_FIELDS
4589
3.10k
4590
3.10k
  Optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
4591
3.09k
  if (checksumkind.Seen && 
checksum.Seen225
)
4592
225
    OptChecksum.emplace(checksumkind.Val, checksum.Val);
4593
2.87k
  else if (checksumkind.Seen || checksum.Seen)
4594
0
    return Lex.Error("'checksumkind' and 'checksum' must be provided together");
4595
3.09k
4596
3.09k
  Optional<MDString *> OptSource;
4597
3.09k
  if (source.Seen)
4598
49
    OptSource = source.Val;
4599
3.09k
  Result = GET_OR_DISTINCT(DIFile, (Context, filename.Val, directory.Val,
4600
3.09k
                                    OptChecksum, OptSource));
4601
3.09k
  return false;
4602
3.09k
}
4603
4604
/// ParseDICompileUnit:
4605
///   ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
4606
///                      isOptimized: true, flags: "-O2", runtimeVersion: 1,
4607
///                      splitDebugFilename: "abc.debug",
4608
///                      emissionKind: FullDebug, enums: !1, retainedTypes: !2,
4609
///                      globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd)
4610
2.42k
bool LLParser::ParseDICompileUnit(MDNode *&Result, bool IsDistinct) {
4611
2.42k
  if (!IsDistinct)
4612
1
    return Lex.Error("missing 'distinct', required for !DICompileUnit");
4613
2.42k
4614
2.42k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4615
23.5k
  REQUIRED
(language, DwarfLangField, )2.41k
; \
4616
21.1k
  REQUIRED(file, MDField, (/* AllowNull */ false));                            \
4617
16.2k
  OPTIONAL(producer, MDStringField, );                                         \
4618
14.0k
  OPTIONAL(isOptimized, MDBoolField, );                                        \
4619
11.7k
  OPTIONAL(flags, MDStringField, );                                            \
4620
11.7k
  OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX));                  \
4621
9.92k
  OPTIONAL(splitDebugFilename, MDStringField, );                               \
4622
9.81k
  OPTIONAL(emissionKind, EmissionKindField, );                                 \
4623
7.47k
  OPTIONAL(enums, MDField, );                                                  \
4624
5.42k
  OPTIONAL(retainedTypes, MDField, );                                          \
4625
4.64k
  OPTIONAL(globals, MDField, );                                                \
4626
3.38k
  OPTIONAL(imports, MDField, );                                                \
4627
2.77k
  OPTIONAL(macros, MDField, );                                                 \
4628
2.75k
  OPTIONAL(dwoId, MDUnsignedField, );                                          \
4629
2.74k
  OPTIONAL(splitDebugInlining, MDBoolField, = true);                           \
4630
2.72k
  OPTIONAL(debugInfoForProfiling, MDBoolField, = false);                       \
4631
2.70k
  OPTIONAL(nameTableKind, NameTableKindField, );                               \
4632
2.42k
  OPTIONAL(debugBaseAddress, MDBoolField, = false)
;0
4633
2.42k
  PARSE_MD_FIELDS();
4634
2.42k
#undef VISIT_MD_FIELDS
4635
2.42k
4636
2.42k
  Result = DICompileUnit::getDistinct(
4637
2.41k
      Context, language.Val, file.Val, producer.Val, isOptimized.Val, flags.Val,
4638
2.41k
      runtimeVersion.Val, splitDebugFilename.Val, emissionKind.Val, enums.Val,
4639
2.41k
      retainedTypes.Val, globals.Val, imports.Val, macros.Val, dwoId.Val,
4640
2.41k
      splitDebugInlining.Val, debugInfoForProfiling.Val, nameTableKind.Val,
4641
2.41k
      debugBaseAddress.Val);
4642
2.41k
  return false;
4643
2.42k
}
4644
4645
/// ParseDISubprogram:
4646
///   ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
4647
///                     file: !1, line: 7, type: !2, isLocal: false,
4648
///                     isDefinition: true, scopeLine: 8, containingType: !3,
4649
///                     virtuality: DW_VIRTUALTIY_pure_virtual,
4650
///                     virtualIndex: 10, thisAdjustment: 4, flags: 11,
4651
///                     spFlags: 10, isOptimized: false, templateParams: !4,
4652
///                     declaration: !5, retainedNodes: !6, thrownTypes: !7)
4653
3.72k
bool LLParser::ParseDISubprogram(MDNode *&Result, bool IsDistinct) {
4654
3.72k
  auto Loc = Lex.getLoc();
4655
3.72k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4656
45.2k
  OPTIONAL(scope, MDField, );                                                  \
4657
41.6k
  OPTIONAL(name, MDStringField, );                                             \
4658
38.0k
  OPTIONAL(linkageName, MDStringField, );                                      \
4659
36.5k
  OPTIONAL(file, MDField, );                                                   \
4660
33.0k
  OPTIONAL(line, LineField, );                                                 \
4661
29.6k
  OPTIONAL(type, MDField, );                                                   \
4662
26.2k
  OPTIONAL(isLocal, MDBoolField, );                                            \
4663
23.1k
  OPTIONAL(isDefinition, MDBoolField, (true));                                 \
4664
19.9k
  OPTIONAL(scopeLine, LineField, );                                            \
4665
17.0k
  OPTIONAL(containingType, MDField, );                                         \
4666
16.9k
  OPTIONAL(virtuality, DwarfVirtualityField, );                                \
4667
16.8k
  OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX));                    \
4668
15.7k
  OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX));          \
4669
15.7k
  OPTIONAL(flags, DIFlagField, );                                              \
4670
13.0k
  OPTIONAL(spFlags, DISPFlagField, );                                          \
4671
12.6k
  OPTIONAL(isOptimized, MDBoolField, );                                        \
4672
9.49k
  OPTIONAL(unit, MDField, );                                                   \
4673
6.55k
  OPTIONAL(templateParams, MDField, );                                         \
4674
6.46k
  OPTIONAL(declaration, MDField, );                                            \
4675
6.19k
  OPTIONAL(retainedNodes, MDField, );                                          \
4676
3.73k
  OPTIONAL(thrownTypes, MDField, )
;0
4677
3.72k
  PARSE_MD_FIELDS();
4678
3.72k
#undef VISIT_MD_FIELDS
4679
3.72k
4680
3.72k
  // An explicit spFlags field takes precedence over individual fields in
4681
3.72k
  // older IR versions.
4682
3.72k
  DISubprogram::DISPFlags SPFlags =
4683
3.72k
      spFlags.Seen ? 
spFlags.Val395
4684
3.72k
                   : DISubprogram::toSPFlags(isLocal.Val, isDefinition.Val,
4685
3.32k
                                             isOptimized.Val, virtuality.Val);
4686
3.72k
  if ((SPFlags & DISubprogram::SPFlagDefinition) && 
!IsDistinct2.96k
)
4687
1
    return Lex.Error(
4688
1
        Loc,
4689
1
        "missing 'distinct', required for !DISubprogram that is a Definition");
4690
3.72k
  Result = GET_OR_DISTINCT(
4691
3.72k
      DISubprogram,
4692
3.72k
      (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val,
4693
3.72k
       type.Val, scopeLine.Val, containingType.Val, virtualIndex.Val,
4694
3.72k
       thisAdjustment.Val, flags.Val, SPFlags, unit.Val, templateParams.Val,
4695
3.72k
       declaration.Val, retainedNodes.Val, thrownTypes.Val));
4696
3.72k
  return false;
4697
3.72k
}
4698
4699
/// ParseDILexicalBlock:
4700
///   ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
4701
1.25k
bool LLParser::ParseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
4702
1.25k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4703
7.41k
  REQUIRED
(scope, MDField, (/* AllowNull */ false))1.25k
; \
4704
4.90k
  OPTIONAL(file, MDField, );                                                   \
4705
3.65k
  OPTIONAL(line, LineField, );                                                 \
4706
2.41k
  OPTIONAL(column, ColumnField, )
;0
4707
1.25k
  PARSE_MD_FIELDS();
4708
1.25k
#undef VISIT_MD_FIELDS
4709
1.25k
4710
1.25k
  
Result = 1.25k
GET_OR_DISTINCT1.25k
(
4711
1.25k
      DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
4712
1.25k
  return false;
4713
1.25k
}
4714
4715
/// ParseDILexicalBlockFile:
4716
///   ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
4717
245
bool LLParser::ParseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
4718
245
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4719
1.21k
  REQUIRED
(scope, MDField, (/* AllowNull */ false))244
; \
4720
723
  OPTIONAL(file, MDField, );                                                   \
4721
731
  REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX))
;242
4722
245
  PARSE_MD_FIELDS();
4723
245
#undef VISIT_MD_FIELDS
4724
245
4725
245
  
Result = 242
GET_OR_DISTINCT242
(DILexicalBlockFile,
4726
242
                           (Context, scope.Val, file.Val, discriminator.Val));
4727
242
  return false;
4728
245
}
4729
4730
/// ParseDICommonBlock:
4731
///   ::= !DICommonBlock(scope: !0, file: !2, name: "COMMON name", line: 9)
4732
3
bool LLParser::ParseDICommonBlock(MDNode *&Result, bool IsDistinct) {
4733
3
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4734
21
  REQUIRED
(scope, MDField, )3
; \
4735
15
  OPTIONAL(declaration, MDField, );                                            \
4736
12
  OPTIONAL(name, MDStringField, );                                             \
4737
9
  OPTIONAL(file, MDField, );                                                   \
4738
6
  OPTIONAL(line, LineField, )
;0
4739
3
  PARSE_MD_FIELDS();
4740
3
#undef VISIT_MD_FIELDS
4741
3
4742
3
  Result = GET_OR_DISTINCT(DICommonBlock,
4743
3
                           (Context, scope.Val, declaration.Val, name.Val,
4744
3
                            file.Val, line.Val));
4745
3
  return false;
4746
3
}
4747
4748
/// ParseDINamespace:
4749
///   ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
4750
114
bool LLParser::ParseDINamespace(MDNode *&Result, bool IsDistinct) {
4751
114
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4752
444
  REQUIRED
(scope, MDField, )114
; \
4753
217
  OPTIONAL(name, MDStringField, );                                             \
4754
124
  OPTIONAL(exportSymbols, MDBoolField, )
;0
4755
114
  PARSE_MD_FIELDS();
4756
114
#undef VISIT_MD_FIELDS
4757
114
4758
114
  
Result = 113
GET_OR_DISTINCT113
(DINamespace,
4759
113
                           (Context, scope.Val, name.Val, exportSymbols.Val));
4760
113
  return false;
4761
114
}
4762
4763
/// ParseDIMacro:
4764
///   ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value: "SomeValue")
4765
21
bool LLParser::ParseDIMacro(MDNode *&Result, bool IsDistinct) {
4766
21
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4767
119
  REQUIRED
(type, DwarfMacinfoTypeField, )21
; \
4768
77
  OPTIONAL(line, LineField, );                                                 \
4769
82
  REQUIRED(name, MDStringField, );                                             \
4770
40
  OPTIONAL(value, MDStringField, )
;0
4771
21
  PARSE_MD_FIELDS();
4772
21
#undef VISIT_MD_FIELDS
4773
21
4774
21
  Result = GET_OR_DISTINCT(DIMacro,
4775
21
                           (Context, type.Val, line.Val, name.Val, value.Val));
4776
21
  return false;
4777
21
}
4778
4779
/// ParseDIMacroFile:
4780
///   ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
4781
27
bool LLParser::ParseDIMacroFile(MDNode *&Result, bool IsDistinct) {
4782
27
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4783
97
  OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file));       \
4784
95
  OPTIONAL(line, LineField, );                                                 \
4785
101
  REQUIRED(file, MDField, );                                                   \
4786
47
  OPTIONAL(nodes, MDField, )
;0
4787
27
  PARSE_MD_FIELDS();
4788
27
#undef VISIT_MD_FIELDS
4789
27
4790
27
  Result = GET_OR_DISTINCT(DIMacroFile,
4791
27
                           (Context, type.Val, line.Val, file.Val, nodes.Val));
4792
27
  return false;
4793
27
}
4794
4795
/// ParseDIModule:
4796
///   ::= !DIModule(scope: !0, name: "SomeModule", configMacros: "-DNDEBUG",
4797
///                 includePath: "/usr/include", isysroot: "/")
4798
23
bool LLParser::ParseDIModule(MDNode *&Result, bool IsDistinct) {
4799
23
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4800
127
  REQUIRED
(scope, MDField, )23
; \
4801
104
  REQUIRED(name, MDStringField, );                                             \
4802
58
  OPTIONAL(configMacros, MDStringField, );                                     \
4803
48
  OPTIONAL(includePath, MDStringField, );                                      \
4804
34
  OPTIONAL(isysroot, MDStringField, )
;0
4805
23
  PARSE_MD_FIELDS();
4806
23
#undef VISIT_MD_FIELDS
4807
23
4808
23
  Result = GET_OR_DISTINCT(DIModule, (Context, scope.Val, name.Val,
4809
23
                           configMacros.Val, includePath.Val, isysroot.Val));
4810
23
  return false;
4811
23
}
4812
4813
/// ParseDITemplateTypeParameter:
4814
///   ::= !DITemplateTypeParameter(name: "Ty", type: !1)
4815
72
bool LLParser::ParseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
4816
72
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4817
191
  OPTIONAL(name, MDStringField, );                                             \
4818
215
  REQUIRED(type, MDField, )
;71
4819
72
  PARSE_MD_FIELDS();
4820
72
#undef VISIT_MD_FIELDS
4821
72
4822
72
  Result =
4823
71
      GET_OR_DISTINCT(DITemplateTypeParameter, (Context, name.Val, type.Val));
4824
71
  return false;
4825
72
}
4826
4827
/// ParseDITemplateValueParameter:
4828
///   ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
4829
///                                 name: "V", type: !1, value: i32 7)
4830
57
bool LLParser::ParseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
4831
57
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4832
221
  OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter));      \
4833
206
  OPTIONAL(name, MDStringField, );                                             \
4834
167
  OPTIONAL(type, MDField, );                                                   \
4835
170
  REQUIRED(value, MDField, )
;56
4836
57
  PARSE_MD_FIELDS();
4837
57
#undef VISIT_MD_FIELDS
4838
57
4839
57
  
Result = 56
GET_OR_DISTINCT56
(DITemplateValueParameter,
4840
56
                           (Context, tag.Val, name.Val, type.Val, value.Val));
4841
56
  return false;
4842
57
}
4843
4844
/// ParseDIGlobalVariable:
4845
///   ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
4846
///                         file: !1, line: 7, type: !2, isLocal: false,
4847
///                         isDefinition: true, templateParams: !3,
4848
///                         declaration: !4, align: 8)
4849
1.23k
bool LLParser::ParseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
4850
1.23k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4851
11.2k
  REQUIRED
(name, MDStringField, (/* AllowEmpty */ false))1.23k
; \
4852
8.81k
  OPTIONAL(scope, MDField, );                                                  \
4853
7.58k
  OPTIONAL(linkageName, MDStringField, );                                      \
4854
7.39k
  OPTIONAL(file, MDField, );                                                   \
4855
6.18k
  OPTIONAL(line, LineField, );                                                 \
4856
4.98k
  OPTIONAL(type, MDField, );                                                   \
4857
3.75k
  OPTIONAL(isLocal, MDBoolField, );                                            \
4858
2.52k
  OPTIONAL(isDefinition, MDBoolField, (true));                                 \
4859
1.29k
  OPTIONAL(templateParams, MDField, );                                         \
4860
1.28k
  OPTIONAL(declaration, MDField, );                                            \
4861
1.25k
  OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX))
;0
4862
1.23k
  PARSE_MD_FIELDS();
4863
1.23k
#undef VISIT_MD_FIELDS
4864
1.23k
4865
1.23k
  Result =
4866
1.23k
      GET_OR_DISTINCT(DIGlobalVariable,
4867
1.23k
                      (Context, scope.Val, name.Val, linkageName.Val, file.Val,
4868
1.23k
                       line.Val, type.Val, isLocal.Val, isDefinition.Val,
4869
1.23k
                       declaration.Val, templateParams.Val, align.Val));
4870
1.23k
  return false;
4871
1.23k
}
4872
4873
/// ParseDILocalVariable:
4874
///   ::= !DILocalVariable(arg: 7, scope: !0, name: "foo",
4875
///                        file: !1, line: 7, type: !2, arg: 2, flags: 7,
4876
///                        align: 8)
4877
///   ::= !DILocalVariable(scope: !0, name: "foo",
4878
///                        file: !1, line: 7, type: !2, arg: 2, flags: 7,
4879
///                        align: 8)
4880
2.96k
bool LLParser::ParseDILocalVariable(MDNode *&Result, bool IsDistinct) {
4881
2.96k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4882
21.7k
  REQUIRED
(scope, MDField, (/* AllowNull */ false))2.96k
; \
4883
15.8k
  OPTIONAL(name, MDStringField, );                                             \
4884
12.9k
  OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX));                             \
4885
11.4k
  OPTIONAL(file, MDField, );                                                   \
4886
8.78k
  OPTIONAL(line, LineField, );                                                 \
4887
6.13k
  OPTIONAL(type, MDField, );                                                   \
4888
3.26k
  OPTIONAL(flags, DIFlagField, );                                              \
4889
2.99k
  OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX))
;0
4890
2.96k
  PARSE_MD_FIELDS();
4891
2.96k
#undef VISIT_MD_FIELDS
4892
2.96k
4893
2.96k
  
Result = 2.96k
GET_OR_DISTINCT2.96k
(DILocalVariable,
4894
2.96k
                           (Context, scope.Val, name.Val, file.Val, line.Val,
4895
2.96k
                            type.Val, arg.Val, flags.Val, align.Val));
4896
2.96k
  return false;
4897
2.96k
}
4898
4899
/// ParseDILabel:
4900
///   ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7)
4901
26
bool LLParser::ParseDILabel(MDNode *&Result, bool IsDistinct) {
4902
26
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4903
156
  REQUIRED
(scope, MDField, (/* AllowNull */ false))26
; \
4904
130
  REQUIRED(name, MDStringField, );                                             \
4905
104
  REQUIRED(file, MDField, );                                                   \
4906
78
  REQUIRED(line, LineField, )
;26
4907
26
  PARSE_MD_FIELDS();
4908
26
#undef VISIT_MD_FIELDS
4909
26
4910
26
  Result = GET_OR_DISTINCT(DILabel,
4911
26
                           (Context, scope.Val, name.Val, file.Val, line.Val));
4912
26
  return false;
4913
26
}
4914
4915
/// ParseDIExpression:
4916
///   ::= !DIExpression(0, 7, -1)
4917
7.85k
bool LLParser::ParseDIExpression(MDNode *&Result, bool IsDistinct) {
4918
7.85k
  assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4919
7.85k
  Lex.Lex();
4920
7.85k
4921
7.85k
  if (ParseToken(lltok::lparen, "expected '(' here"))
4922
0
    return true;
4923
7.85k
4924
7.85k
  SmallVector<uint64_t, 8> Elements;
4925
7.85k
  if (Lex.getKind() != lltok::rparen)
4926
12.9k
    
do 4.33k
{
4927
12.9k
      if (Lex.getKind() == lltok::DwarfOp) {
4928
4.53k
        if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
4929
4.53k
          Lex.Lex();
4930
4.53k
          Elements.push_back(Op);
4931
4.53k
          continue;
4932
4.53k
        }
4933
0
        return TokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
4934
0
      }
4935
8.42k
4936
8.42k
      if (Lex.getKind() == lltok::DwarfAttEncoding) {
4937
28
        if (unsigned Op = dwarf::getAttributeEncoding(Lex.getStrVal())) {
4938
28
          Lex.Lex();
4939
28
          Elements.push_back(Op);
4940
28
          continue;
4941
28
        }
4942
0
        return TokError(Twine("invalid DWARF attribute encoding '") + Lex.getStrVal() + "'");
4943
0
      }
4944
8.40k
4945
8.40k
      if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
4946
0
        return TokError("expected unsigned integer");
4947
8.40k
4948
8.40k
      auto &U = Lex.getAPSIntVal();
4949
8.40k
      if (U.ugt(UINT64_MAX))
4950
1
        return TokError("element too large, limit is " + Twine(UINT64_MAX));
4951
8.40k
      Elements.push_back(U.getZExtValue());
4952
8.40k
      Lex.Lex();
4953
12.9k
    } while (EatIfPresent(lltok::comma));
4954
7.85k
4955
7.85k
  
if (7.85k
ParseToken(lltok::rparen, "expected ')' here")7.85k
)
4956
0
    return true;
4957
7.85k
4958
7.85k
  Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
4959
7.85k
  return false;
4960
7.85k
}
4961
4962
/// ParseDIGlobalVariableExpression:
4963
///   ::= !DIGlobalVariableExpression(var: !0, expr: !1)
4964
bool LLParser::ParseDIGlobalVariableExpression(MDNode *&Result,
4965
1.21k
                                               bool IsDistinct) {
4966
1.21k
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4967
4.84k
  REQUIRED
(var, MDField, )1.21k
; \
4968
3.63k
  REQUIRED(expr, MDField, )
;1.21k
4969
1.21k
  PARSE_MD_FIELDS();
4970
1.21k
#undef VISIT_MD_FIELDS
4971
1.21k
4972
1.21k
  Result =
4973
1.21k
      GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val));
4974
1.21k
  return false;
4975
1.21k
}
4976
4977
/// ParseDIObjCProperty:
4978
///   ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
4979
///                       getter: "getFoo", attributes: 7, type: !2)
4980
21
bool LLParser::ParseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
4981
21
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
4982
96
  OPTIONAL(name, MDStringField, );                                             \
4983
82
  OPTIONAL(file, MDField, );                                                   \
4984
72
  OPTIONAL(line, LineField, );                                                 \
4985
62
  OPTIONAL(setter, MDStringField, );                                           \
4986
53
  OPTIONAL(getter, MDStringField, );                                           \
4987
44
  OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX));                      \
4988
34
  OPTIONAL(type, MDField, )
;0
4989
21
  PARSE_MD_FIELDS();
4990
21
#undef VISIT_MD_FIELDS
4991
21
4992
21
  Result = GET_OR_DISTINCT(DIObjCProperty,
4993
21
                           (Context, name.Val, file.Val, line.Val, setter.Val,
4994
21
                            getter.Val, attributes.Val, type.Val));
4995
21
  return false;
4996
21
}
4997
4998
/// ParseDIImportedEntity:
4999
///   ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
5000
///                         line: 7, name: "foo")
5001
264
bool LLParser::ParseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
5002
264
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
5003
1.81k
  REQUIRED
(tag, DwarfTagField, )264
; \
5004
1.54k
  REQUIRED(scope, MDField, );                                                  \
5005
1.02k
  OPTIONAL(entity, MDField, );                                                 \
5006
768
  OPTIONAL(file, MDField, );                                                   \
5007
525
  OPTIONAL(line, LineField, );                                                 \
5008
280
  OPTIONAL(name, MDStringField, )
;0
5009
264
  PARSE_MD_FIELDS();
5010
264
#undef VISIT_MD_FIELDS
5011
264
5012
264
  
Result = 262
GET_OR_DISTINCT262
(
5013
262
      DIImportedEntity,
5014
262
      (Context, tag.Val, scope.Val, entity.Val, file.Val, line.Val, name.Val));
5015
262
  return false;
5016
264
}
5017
5018
#undef PARSE_MD_FIELD
5019
#undef NOP_FIELD
5020
#undef REQUIRE_FIELD
5021
#undef DECLARE_FIELD
5022
5023
/// ParseMetadataAsValue
5024
///  ::= metadata i32 %local
5025
///  ::= metadata i32 @global
5026
///  ::= metadata i32 7
5027
///  ::= metadata !0
5028
///  ::= metadata !{...}
5029
///  ::= metadata !"string"
5030
25.2k
bool LLParser::ParseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
5031
25.2k
  // Note: the type 'metadata' has already been parsed.
5032
25.2k
  Metadata *MD;
5033
25.2k
  if (ParseMetadata(MD, &PFS))
5034
3
    return true;
5035
25.2k
5036
25.2k
  V = MetadataAsValue::get(Context, MD);
5037
25.2k
  return false;
5038
25.2k
}
5039
5040
/// ParseValueAsMetadata
5041
///  ::= i32 %local
5042
///  ::= i32 @global
5043
///  ::= i32 7
5044
bool LLParser::ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
5045
30.6k
                                    PerFunctionState *PFS) {
5046
30.6k
  Type *Ty;
5047
30.6k
  LocTy Loc;
5048
30.6k
  if (ParseType(Ty, TypeMsg, Loc))
5049
1
    return true;
5050
30.6k
  if (Ty->isMetadataTy())
5051
1
    return Error(Loc, "invalid metadata-value-metadata roundtrip");
5052
30.6k
5053
30.6k
  Value *V;
5054
30.6k
  if (ParseValue(Ty, V, PFS))
5055
5
    return true;
5056
30.6k
5057
30.6k
  MD = ValueAsMetadata::get(V);
5058
30.6k
  return false;
5059
30.6k
}
5060
5061
/// ParseMetadata
5062
///  ::= i32 %local
5063
///  ::= i32 @global
5064
///  ::= i32 7
5065
///  ::= !42
5066
///  ::= !{...}
5067
///  ::= !"string"
5068
///  ::= !DILocation(...)
5069
155k
bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
5070
155k
  if (Lex.getKind() == lltok::MetadataVar) {
5071
7.39k
    MDNode *N;
5072
7.39k
    if (ParseSpecializedMDNode(N))
5073
0
      return true;
5074
7.39k
    MD = N;
5075
7.39k
    return false;
5076
7.39k
  }
5077
148k
5078
148k
  // ValueAsMetadata:
5079
148k
  // <type> <value>
5080
148k
  if (Lex.getKind() != lltok::exclaim)
5081
30.6k
    return ParseValueAsMetadata(MD, "expected metadata operand", PFS);
5082
117k
5083
117k
  // '!'.
5084
117k
  assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
5085
117k
  Lex.Lex();
5086
117k
5087
117k
  // MDString:
5088
117k
  //   ::= '!' STRINGCONSTANT
5089
117k
  if (Lex.getKind() == lltok::StringConstant) {
5090
17.6k
    MDString *S;
5091
17.6k
    if (ParseMDString(S))
5092
0
      return true;
5093
17.6k
    MD = S;
5094
17.6k
    return false;
5095
17.6k
  }
5096
100k
5097
100k
  // MDNode:
5098
100k
  // !{ ... }
5099
100k
  // !7
5100
100k
  MDNode *N;
5101
100k
  if (ParseMDNodeTail(N))
5102
3
    return true;
5103
100k
  MD = N;
5104
100k
  return false;
5105
100k
}
5106
5107
//===----------------------------------------------------------------------===//
5108
// Function Parsing.
5109
//===----------------------------------------------------------------------===//
5110
5111
bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
5112
4.38M
                                   PerFunctionState *PFS, bool IsCall) {
5113
4.38M
  if (Ty->isFunctionTy())
5114
0
    return Error(ID.Loc, "functions are not values, refer to them as pointers");
5115
4.38M
5116
4.38M
  switch (ID.Kind) {
5117
4.38M
  case ValID::t_LocalID:
5118
281k
    if (!PFS) 
return Error(ID.Loc, "invalid use of function-local name")0
;
5119
281k
    V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc, IsCall);
5120
281k
    return V == nullptr;
5121
1.78M
  case ValID::t_LocalName:
5122
1.78M
    if (!PFS) 
return Error(ID.Loc, "invalid use of function-local name")4
;
5123
1.78M
    V = PFS->GetVal(ID.StrVal, Ty, ID.Loc, IsCall);
5124
1.78M
    return V == nullptr;
5125
1.78M
  case ValID::t_InlineAsm: {
5126
10.5k
    if (!ID.FTy || 
!InlineAsm::Verify(ID.FTy, ID.StrVal2)10.5k
)
5127
3
      return Error(ID.Loc, "invalid type for inline asm constraint string");
5128
10.5k
    V = InlineAsm::get(ID.FTy, ID.StrVal, ID.StrVal2, ID.UIntVal & 1,
5129
10.5k
                       (ID.UIntVal >> 1) & 1,
5130
10.5k
                       (InlineAsm::AsmDialect(ID.UIntVal >> 2)));
5131
10.5k
    return false;
5132
10.5k
  }
5133
297k
  case ValID::t_GlobalName:
5134
297k
    V = GetGlobalVal(ID.StrVal, Ty, ID.Loc, IsCall);
5135
297k
    return V == nullptr;
5136
10.5k
  case ValID::t_GlobalID:
5137
665
    V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc, IsCall);
5138
665
    return V == nullptr;
5139
1.48M
  case ValID::t_APSInt:
5140
1.48M
    if (!Ty->isIntegerTy())
5141
1
      return Error(ID.Loc, "integer constant must have integer type");
5142
1.48M
    ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
5143
1.48M
    V = ConstantInt::get(Context, ID.APSIntVal);
5144
1.48M
    return false;
5145
1.48M
  case ValID::t_APFloat:
5146
67.1k
    if (!Ty->isFloatingPointTy() ||
5147
67.1k
        
!ConstantFP::isValueValidForType(Ty, ID.APFloatVal)67.1k
)
5148
2
      return Error(ID.Loc, "floating point constant invalid for type");
5149
67.1k
5150
67.1k
    // The lexer has no type info, so builds all half, float, and double FP
5151
67.1k
    // constants as double.  Fix this here.  Long double does not need this.
5152
67.1k
    if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) {
5153
65.2k
      bool Ignored;
5154
65.2k
      if (Ty->isHalfTy())
5155
2.55k
        ID.APFloatVal.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
5156
2.55k
                              &Ignored);
5157
62.7k
      else if (Ty->isFloatTy())
5158
44.5k
        ID.APFloatVal.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
5159
44.5k
                              &Ignored);
5160
65.2k
    }
5161
67.1k
    V = ConstantFP::get(Context, ID.APFloatVal);
5162
67.1k
5163
67.1k
    if (V->getType() != Ty)
5164
1
      return Error(ID.Loc, "floating point constant does not have type '" +
5165
1
                   getTypeString(Ty) + "'");
5166
67.1k
5167
67.1k
    return false;
5168
67.1k
  case ValID::t_Null:
5169
8.76k
    if (!Ty->isPointerTy())
5170
0
      return Error(ID.Loc, "null must be a pointer type");
5171
8.76k
    V = ConstantPointerNull::get(cast<PointerType>(Ty));
5172
8.76k
    return false;
5173
215k
  case ValID::t_Undef:
5174
215k
    // FIXME: LabelTy should not be a first-class type.
5175
215k
    if (!Ty->isFirstClassType() || Ty->isLabelTy())
5176
0
      return Error(ID.Loc, "invalid type for undef constant");
5177
215k
    V = UndefValue::get(Ty);
5178
215k
    return false;
5179
215k
  case ValID::t_EmptyArray:
5180
19
    if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
5181
0
      return Error(ID.Loc, "invalid empty array initializer");
5182
19
    V = UndefValue::get(Ty);
5183
19
    return false;
5184
33.5k
  case ValID::t_Zero:
5185
33.5k
    // FIXME: LabelTy should not be a first-class type.
5186
33.5k
    if (!Ty->isFirstClassType() || Ty->isLabelTy())
5187
0
      return Error(ID.Loc, "invalid type for null constant");
5188
33.5k
    V = Constant::getNullValue(Ty);
5189
33.5k
    return false;
5190
33.5k
  case ValID::t_None:
5191
582
    if (!Ty->isTokenTy())
5192
0
      return Error(ID.Loc, "invalid type for none constant");
5193
582
    V = Constant::getNullValue(Ty);
5194
582
    return false;
5195
202k
  case ValID::t_Constant:
5196
202k
    if (ID.ConstantVal->getType() != Ty)
5197
0
      return Error(ID.Loc, "constant expression type mismatch");
5198
202k
5199
202k
    V = ID.ConstantVal;
5200
202k
    return false;
5201
202k
  case ValID::t_ConstantStruct:
5202
1.95k
  case ValID::t_PackedConstantStruct:
5203
1.95k
    if (StructType *ST = dyn_cast<StructType>(Ty)) {
5204
1.95k
      if (ST->getNumElements() != ID.UIntVal)
5205
1
        return Error(ID.Loc,
5206
1
                     "initializer with struct type has wrong # elements");
5207
1.95k
      if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
5208
0
        return Error(ID.Loc, "packed'ness of initializer and type don't match");
5209
1.95k
5210
1.95k
      // Verify that the elements are compatible with the structtype.
5211
7.72k
      
for (unsigned i = 0, e = ID.UIntVal; 1.95k
i != e;
++i5.77k
)
5212
5.77k
        if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
5213
1
          return Error(ID.Loc, "element " + Twine(i) +
5214
1
                    " of struct initializer doesn't match struct element type");
5215
1.95k
5216
1.95k
      V = ConstantStruct::get(
5217
1.95k
          ST, makeArrayRef(ID.ConstantStructElts.get(), ID.UIntVal));
5218
1.95k
    } else
5219
0
      return Error(ID.Loc, "constant expression type mismatch");
5220
1.95k
    return false;
5221
0
  }
5222
0
  llvm_unreachable("Invalid ValID");
5223
0
}
5224
5225
2.19k
bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
5226
2.19k
  C = nullptr;
5227
2.19k
  ValID ID;
5228
2.19k
  auto Loc = Lex.getLoc();
5229
2.19k
  if (ParseValID(ID, /*PFS=*/nullptr))
5230
0
    return true;
5231
2.19k
  switch (ID.Kind) {
5232
2.19k
  case ValID::t_APSInt:
5233
2.18k
  case ValID::t_APFloat:
5234
2.18k
  case ValID::t_Undef:
5235
2.18k
  case ValID::t_Constant:
5236
2.18k
  case ValID::t_ConstantStruct:
5237
2.18k
  case ValID::t_PackedConstantStruct: {
5238
2.18k
    Value *V;
5239
2.18k
    if (ConvertValIDToValue(Ty, ID, V, /*PFS=*/nullptr, /*IsCall=*/false))
5240
2
      return true;
5241
2.18k
    assert(isa<Constant>(V) && "Expected a constant value");
5242
2.18k
    C = cast<Constant>(V);
5243
2.18k
    return false;
5244
2.18k
  }
5245
2.18k
  case ValID::t_Null:
5246
4
    C = Constant::getNullValue(Ty);
5247
4
    return false;
5248
2.18k
  default:
5249
1
    return Error(Loc, "expected a constant value");
5250
2.19k
  }
5251
2.19k
}
5252
5253
2.87M
bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
5254
2.87M
  V = nullptr;
5255
2.87M
  ValID ID;
5256
2.87M
  return ParseValID(ID, PFS) ||
5257
2.87M
         
ConvertValIDToValue(Ty, ID, V, PFS, /*IsCall=*/false)2.87M
;
5258
2.87M
}
5259
5260
1.84M
bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
5261
1.84M
  Type *Ty = nullptr;
5262
1.84M
  return ParseType(Ty) ||
5263
1.84M
         ParseValue(Ty, V, PFS);
5264
1.84M
}
5265
5266
bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
5267
99.4k
                                      PerFunctionState &PFS) {
5268
99.4k
  Value *V;
5269
99.4k
  Loc = Lex.getLoc();
5270
99.4k
  if (ParseTypeAndValue(V, PFS)) 
return true0
;
5271
99.4k
  if (!isa<BasicBlock>(V))
5272
0
    return Error(Loc, "expected a basic block");
5273
99.4k
  BB = cast<BasicBlock>(V);
5274
99.4k
  return false;
5275
99.4k
}
5276
5277
/// FunctionHeader
5278
///   ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
5279
///       OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
5280
///       '(' ArgList ')' OptAddrSpace OptFuncAttrs OptSection OptionalAlign
5281
///       OptGC OptionalPrefix OptionalPrologue OptPersonalityFn
5282
378k
bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
5283
378k
  // Parse the linkage.
5284
378k
  LocTy LinkageLoc = Lex.getLoc();
5285
378k
  unsigned Linkage;
5286
378k
  unsigned Visibility;
5287
378k
  unsigned DLLStorageClass;
5288
378k
  bool DSOLocal;
5289
378k
  AttrBuilder RetAttrs;
5290
378k
  unsigned CC;
5291
378k
  bool HasLinkage;
5292
378k
  Type *RetType = nullptr;
5293
378k
  LocTy RetTypeLoc = Lex.getLoc();
5294
378k
  if (ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
5295
378k
                           DSOLocal) ||
5296
378k
      
ParseOptionalCallingConv(CC)378k
||
ParseOptionalReturnAttrs(RetAttrs)378k
||
5297
378k
      
ParseType(RetType, RetTypeLoc, true /*void allowed*/)378k
)
5298
4
    return true;
5299
378k
5300
378k
  // Verify that the linkage is ok.
5301
378k
  switch ((GlobalValue::LinkageTypes)Linkage) {
5302
378k
  case GlobalValue::ExternalLinkage:
5303
375k
    break; // always ok.
5304
378k
  case GlobalValue::ExternalWeakLinkage:
5305
74
    if (isDefine)
5306
0
      return Error(LinkageLoc, "invalid linkage for function definition");
5307
74
    break;
5308
2.83k
  case GlobalValue::PrivateLinkage:
5309
2.83k
  case GlobalValue::InternalLinkage:
5310
2.83k
  case GlobalValue::AvailableExternallyLinkage:
5311
2.83k
  case GlobalValue::LinkOnceAnyLinkage:
5312
2.83k
  case GlobalValue::LinkOnceODRLinkage:
5313
2.83k
  case GlobalValue::WeakAnyLinkage:
5314
2.83k
  case GlobalValue::WeakODRLinkage:
5315
2.83k
    if (!isDefine)
5316
0
      return Error(LinkageLoc, "invalid linkage for function declaration");
5317
2.83k
    break;
5318
2.83k
  case GlobalValue::AppendingLinkage:
5319
0
  case GlobalValue::CommonLinkage:
5320
0
    return Error(LinkageLoc, "invalid function linkage type");
5321
378k
  }
5322
378k
5323
378k
  if (!isValidVisibilityForLinkage(Visibility, Linkage))
5324
4
    return Error(LinkageLoc,
5325
4
                 "symbol with local linkage must have default visibility");
5326
378k
5327
378k
  if (!FunctionType::isValidReturnType(RetType))
5328
0
    return Error(RetTypeLoc, "invalid function return type");
5329
378k
5330
378k
  LocTy NameLoc = Lex.getLoc();
5331
378k
5332
378k
  std::string FunctionName;
5333
378k
  if (Lex.getKind() == lltok::GlobalVar) {
5334
378k
    FunctionName = Lex.getStrVal();
5335
378k
  } else 
if (44
Lex.getKind() == lltok::GlobalID44
) { // @42 is ok.
5336
44
    unsigned NameID = Lex.getUIntVal();
5337
44
5338
44
    if (NameID != NumberedVals.size())
5339
0
      return TokError("function expected to be numbered '%" +
5340
0
                      Twine(NumberedVals.size()) + "'");
5341
0
  } else {
5342
0
    return TokError("expected function name");
5343
0
  }
5344
378k
5345
378k
  Lex.Lex();
5346
378k
5347
378k
  if (Lex.getKind() != lltok::lparen)
5348
0
    return TokError("expected '(' in function argument list");
5349
378k
5350
378k
  SmallVector<ArgInfo, 8> ArgList;
5351
378k
  bool isVarArg;
5352
378k
  AttrBuilder FuncAttrs;
5353
378k
  std::vector<unsigned> FwdRefAttrGrps;
5354
378k
  LocTy BuiltinLoc;
5355
378k
  std::string Section;
5356
378k
  std::string Partition;
5357
378k
  unsigned Alignment;
5358
378k
  std::string GC;
5359
378k
  GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
5360
378k
  unsigned AddrSpace = 0;
5361
378k
  Constant *Prefix = nullptr;
5362
378k
  Constant *Prologue = nullptr;
5363
378k
  Constant *PersonalityFn = nullptr;
5364
378k
  Comdat *C;
5365
378k
5366
378k
  if (ParseArgumentList(ArgList, isVarArg) ||
5367
378k
      
ParseOptionalUnnamedAddr(UnnamedAddr)378k
||
5368
378k
      
ParseOptionalProgramAddrSpace(AddrSpace)378k
||
5369
378k
      ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
5370
378k
                                 BuiltinLoc) ||
5371
378k
      
(378k
EatIfPresent(lltok::kw_section)378k
&&
5372
378k
       
ParseStringConstant(Section)193
) ||
5373
378k
      
(378k
EatIfPresent(lltok::kw_partition)378k
&&
5374
378k
       
ParseStringConstant(Partition)5
) ||
5375
378k
      
parseOptionalComdat(FunctionName, C)378k
||
5376
378k
      
ParseOptionalAlignment(Alignment)378k
||
5377
378k
      
(378k
EatIfPresent(lltok::kw_gc)378k
&&
5378
378k
       
ParseStringConstant(GC)516
) ||
5379
378k
      
(378k
EatIfPresent(lltok::kw_prefix)378k
&&
5380
378k
       
ParseGlobalTypeAndValue(Prefix)25
) ||
5381
378k
      
(378k
EatIfPresent(lltok::kw_prologue)378k
&&
5382
378k
       
ParseGlobalTypeAndValue(Prologue)51
) ||
5383
378k
      
(378k
EatIfPresent(lltok::kw_personality)378k
&&
5384
378k
       
ParseGlobalTypeAndValue(PersonalityFn)1.24k
))
5385
6
    return true;
5386
378k
5387
378k
  if (FuncAttrs.contains(Attribute::Builtin))
5388
0
    return Error(BuiltinLoc, "'builtin' attribute not valid on function");
5389
378k
5390
378k
  // If the alignment was parsed as an attribute, move to the alignment field.
5391
378k
  if (FuncAttrs.hasAlignmentAttr()) {
5392
697
    Alignment = FuncAttrs.getAlignment();
5393
697
    FuncAttrs.removeAttribute(Attribute::Alignment);
5394
697
  }
5395
378k
5396
378k
  // Okay, if we got here, the function is syntactically valid.  Convert types
5397
378k
  // and do semantic checks.
5398
378k
  std::vector<Type*> ParamTypeList;
5399
378k
  SmallVector<AttributeSet, 8> Attrs;
5400
378k
5401
1.03M
  for (unsigned i = 0, e = ArgList.size(); i != e; 
++i659k
) {
5402
659k
    ParamTypeList.push_back(ArgList[i].Ty);
5403
659k
    Attrs.push_back(ArgList[i].Attrs);
5404
659k
  }
5405
378k
5406
378k
  AttributeList PAL =
5407
378k
      AttributeList::get(Context, AttributeSet::get(Context, FuncAttrs),
5408
378k
                         AttributeSet::get(Context, RetAttrs), Attrs);
5409
378k
5410
378k
  if (PAL.hasAttribute(1, Attribute::StructRet) && 
!RetType->isVoidTy()1.03k
)
5411
0
    return Error(RetTypeLoc, "functions with 'sret' argument must return void");
5412
378k
5413
378k
  FunctionType *FT =
5414
378k
    FunctionType::get(RetType, ParamTypeList, isVarArg);
5415
378k
  PointerType *PFT = PointerType::get(FT, AddrSpace);
5416
378k
5417
378k
  Fn = nullptr;
5418
378k
  if (!FunctionName.empty()) {
5419
378k
    // If this was a definition of a forward reference, remove the definition
5420
378k
    // from the forward reference table and fill in the forward ref.
5421
378k
    auto FRVI = ForwardRefVals.find(FunctionName);
5422
378k
    if (FRVI != ForwardRefVals.end()) {
5423
50.5k
      Fn = M->getFunction(FunctionName);
5424
50.5k
      if (!Fn)
5425
1
        return Error(FRVI->second.second, "invalid forward reference to "
5426
1
                     "function as global value!");
5427
50.5k
      if (Fn->getType() != PFT)
5428
1
        return Error(FRVI->second.second, "invalid forward reference to "
5429
1
                     "function '" + FunctionName + "' with wrong type: "
5430
1
                     "expected '" + getTypeString(PFT) + "' but was '" +
5431
1
                     getTypeString(Fn->getType()) + "'");
5432
50.5k
      ForwardRefVals.erase(FRVI);
5433
328k
    } else if ((Fn = M->getFunction(FunctionName))) {
5434
1
      // Reject redefinitions.
5435
1
      return Error(NameLoc, "invalid redefinition of function '" +
5436
1
                   FunctionName + "'");
5437
328k
    } else if (M->getNamedValue(FunctionName)) {
5438
0
      return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
5439
0
    }
5440
49
5441
49
  } else {
5442
49
    // If this is a definition of a forward referenced function, make sure the
5443
49
    // types agree.
5444
49
    auto I = ForwardRefValIDs.find(NumberedVals.size());
5445
49
    if (I != ForwardRefValIDs.end()) {
5446
21
      Fn = cast<Function>(I->second.first);
5447
21
      if (Fn->getType() != PFT)
5448
0
        return Error(NameLoc, "type of definition and forward reference of '@" +
5449
0
                     Twine(NumberedVals.size()) + "' disagree: "
5450
0
                     "expected '" + getTypeString(PFT) + "' but was '" +
5451
0
                     getTypeString(Fn->getType()) + "'");
5452
21
      ForwardRefValIDs.erase(I);
5453
21
    }
5454
49
  }
5455
378k
5456
378k
  
if (378k
!Fn378k
)
5457
328k
    Fn = Function::Create(FT, GlobalValue::ExternalLinkage, AddrSpace,
5458
328k
                          FunctionName, M);
5459
50.6k
  else // Move the forward-reference to the correct spot in the module.
5460
50.6k
    M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
5461
378k
5462
378k
  assert(Fn->getAddressSpace() == AddrSpace && "Created function in wrong AS");
5463
378k
5464
378k
  if (FunctionName.empty())
5465
49
    NumberedVals.push_back(Fn);
5466
378k
5467
378k
  Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
5468
378k
  maybeSetDSOLocal(DSOLocal, *Fn);
5469
378k
  Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
5470
378k
  Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
5471
378k
  Fn->setCallingConv(CC);
5472
378k
  Fn->setAttributes(PAL);
5473
378k
  Fn->setUnnamedAddr(UnnamedAddr);
5474
378k
  Fn->setAlignment(Alignment);
5475
378k
  Fn->setSection(Section);
5476
378k
  Fn->setPartition(Partition);
5477
378k
  Fn->setComdat(C);
5478
378k
  Fn->setPersonalityFn(PersonalityFn);
5479
378k
  if (!GC.empty()) 
Fn->setGC(GC)516
;
5480
378k
  Fn->setPrefixData(Prefix);
5481
378k
  Fn->setPrologueData(Prologue);
5482
378k
  ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
5483
378k
5484
378k
  // Add all of the arguments we parsed to the function.
5485
378k
  Function::arg_iterator ArgIt = Fn->arg_begin();
5486
1.03M
  for (unsigned i = 0, e = ArgList.size(); i != e; 
++i, ++ArgIt659k
) {
5487
659k
    // If the argument has a name, insert it into the argument symbol table.
5488
659k
    if (ArgList[i].Name.empty()) 
continue159k
;
5489
500k
5490
500k
    // Set the name, if it conflicted, it will be auto-renamed.
5491
500k
    ArgIt->setName(ArgList[i].Name);
5492
500k
5493
500k
    if (ArgIt->getName() != ArgList[i].Name)
5494
0
      return Error(ArgList[i].Loc, "redefinition of argument '%" +
5495
0
                   ArgList[i].Name + "'");
5496
500k
  }
5497
378k
5498
378k
  if (isDefine)
5499
297k
    return false;
5500
81.3k
5501
81.3k
  // Check the declaration has no block address forward references.
5502
81.3k
  ValID ID;
5503
81.3k
  if (FunctionName.empty()) {
5504
1
    ID.Kind = ValID::t_GlobalID;
5505
1
    ID.UIntVal = NumberedVals.size() - 1;
5506
81.3k
  } else {
5507
81.3k
    ID.Kind = ValID::t_GlobalName;
5508
81.3k
    ID.StrVal = FunctionName;
5509
81.3k
  }
5510
81.3k
  auto Blocks = ForwardRefBlockAddresses.find(ID);
5511
81.3k
  if (Blocks != ForwardRefBlockAddresses.end())
5512
0
    return Error(Blocks->first.Loc,
5513
0
                 "cannot take blockaddress inside a declaration");
5514
81.3k
  return false;
5515
81.3k
}
5516
5517
297k
bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
5518
297k
  ValID ID;
5519
297k
  if (FunctionNumber == -1) {
5520
297k
    ID.Kind = ValID::t_GlobalName;
5521
297k
    ID.StrVal = F.getName();
5522
297k
  } else {
5523
48
    ID.Kind = ValID::t_GlobalID;
5524
48
    ID.UIntVal = FunctionNumber;
5525
48
  }
5526
297k
5527
297k
  auto Blocks = P.ForwardRefBlockAddresses.find(ID);
5528
297k
  if (Blocks == P.ForwardRefBlockAddresses.end())
5529
297k
    return false;
5530
130
5531
332
  
for (const auto &I : Blocks->second)130
{
5532
332
    const ValID &BBID = I.first;
5533
332
    GlobalValue *GV = I.second;
5534
332
5535
332
    assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
5536
332
           "Expected local id or name");
5537
332
    BasicBlock *BB;
5538
332
    if (BBID.Kind == ValID::t_LocalName)
5539
319
      BB = GetBB(BBID.StrVal, BBID.Loc);
5540
13
    else
5541
13
      BB = GetBB(BBID.UIntVal, BBID.Loc);
5542
332
    if (!BB)
5543
0
      return P.Error(BBID.Loc, "referenced value is not a basic block");
5544
332
5545
332
    GV->replaceAllUsesWith(BlockAddress::get(&F, BB));
5546
332
    GV->eraseFromParent();
5547
332
  }
5548
130
5549
130
  P.ForwardRefBlockAddresses.erase(Blocks);
5550
130
  return false;
5551
130
}
5552
5553
/// ParseFunctionBody
5554
///   ::= '{' BasicBlock+ UseListOrderDirective* '}'
5555
297k
bool LLParser::ParseFunctionBody(Function &Fn) {
5556
297k
  if (Lex.getKind() != lltok::lbrace)
5557
0
    return TokError("expected '{' in function body");
5558
297k
  Lex.Lex();  // eat the {.
5559
297k
5560
297k
  int FunctionNumber = -1;
5561
297k
  if (!Fn.hasName()) 
FunctionNumber = NumberedVals.size()-148
;
5562
297k
5563
297k
  PerFunctionState PFS(*this, Fn, FunctionNumber);
5564
297k
5565
297k
  // Resolve block addresses and allow basic blocks to be forward-declared
5566
297k
  // within this function.
5567
297k
  if (PFS.resolveForwardRefBlockAddresses())
5568
0
    return true;
5569
297k
  SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS);
5570
297k
5571
297k
  // We need at least one basic block.
5572
297k
  if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
5573
0
    return TokError("function body requires at least one basic block");
5574
297k
5575
693k
  
while (297k
Lex.getKind() != lltok::rbrace &&
5576
693k
         
Lex.getKind() != lltok::kw_uselistorder396k
)
5577
396k
    if (ParseBasicBlock(PFS)) 
return true80
;
5578
297k
5579
298k
  
while (297k
Lex.getKind() != lltok::rbrace)
5580
769
    if (ParseUseListOrder(&PFS))
5581
3
      return true;
5582
297k
5583
297k
  // Eat the }.
5584
297k
  Lex.Lex();
5585
297k
5586
297k
  // Verify function is ok.
5587
297k
  return PFS.FinishFunction();
5588
297k
}
5589
5590
/// ParseBasicBlock
5591
///   ::= (LabelStr|LabelID)? Instruction*
5592
396k
bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
5593
396k
  // If this basic block starts out with a name, remember it.
5594
396k
  std::string Name;
5595
396k
  int NameID = -1;
5596
396k
  LocTy NameLoc = Lex.getLoc();
5597
396k
  if (Lex.getKind() == lltok::LabelStr) {
5598
196k
    Name = Lex.getStrVal();
5599
196k
    Lex.Lex();
5600
200k
  } else if (Lex.getKind() == lltok::LabelID) {
5601
236
    NameID = Lex.getUIntVal();
5602
236
    Lex.Lex();
5603
236
  }
5604
396k
5605
396k
  BasicBlock *BB = PFS.DefineBB(Name, NameID, NameLoc);
5606
396k
  if (!BB)
5607
2
    return true;
5608
396k
5609
396k
  std::string NameStr;
5610
396k
5611
396k
  // Parse the instructions in this block until we get a terminator.
5612
396k
  Instruction *Inst;
5613
1.74M
  do {
5614
1.74M
    // This instruction may have three possibilities for a name: a) none
5615
1.74M
    // specified, b) name specified "%foo =", c) number specified: "%4 =".
5616
1.74M
    LocTy NameLoc = Lex.getLoc();
5617
1.74M
    int NameID = -1;
5618
1.74M
    NameStr = "";
5619
1.74M
5620
1.74M
    if (Lex.getKind() == lltok::LocalVarID) {
5621
246k
      NameID = Lex.getUIntVal();
5622
246k
      Lex.Lex();
5623
246k
      if (ParseToken(lltok::equal, "expected '=' after instruction id"))
5624
0
        return true;
5625
1.50M
    } else if (Lex.getKind() == lltok::LocalVar) {
5626
896k
      NameStr = Lex.getStrVal();
5627
896k
      Lex.Lex();
5628
896k
      if (ParseToken(lltok::equal, "expected '=' after instruction name"))
5629
0
        return true;
5630
1.74M
    }
5631
1.74M
5632
1.74M
    switch (ParseInstruction(Inst, BB, PFS)) {
5633
1.74M
    
default: 0
llvm_unreachable0
("Unknown ParseInstruction result!");
5634
1.74M
    
case InstError: return true73
;
5635
1.74M
    case InstNormal:
5636
1.73M
      BB->getInstList().push_back(Inst);
5637
1.73M
5638
1.73M
      // With a normal result, we check to see if the instruction is followed by
5639
1.73M
      // a comma and metadata.
5640
1.73M
      if (EatIfPresent(lltok::comma))
5641
21.6k
        if (ParseInstructionMetadata(*Inst))
5642
4
          return true;
5643
1.73M
      break;
5644
1.73M
    case InstExtraComma:
5645
11.7k
      BB->getInstList().push_back(Inst);
5646
11.7k
5647
11.7k
      // If the instruction parser ate an extra comma at the end of it, it
5648
11.7k
      // *must* be followed by metadata.
5649
11.7k
      if (ParseInstructionMetadata(*Inst))
5650
0
        return true;
5651
11.7k
      break;
5652
1.74M
    }
5653
1.74M
5654
1.74M
    // Set the name on the instruction.
5655
1.74M
    if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) 
return true1
;
5656
1.74M
  } while (!Inst->isTerminator());
5657
396k
5658
396k
  
return false396k
;
5659
396k
}
5660
5661
//===----------------------------------------------------------------------===//
5662
// Instruction Parsing.
5663
//===----------------------------------------------------------------------===//
5664
5665
/// ParseInstruction - Parse one of the many different instructions.
5666
///
5667
int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
5668
1.74M
                               PerFunctionState &PFS) {
5669
1.74M
  lltok::Kind Token = Lex.getKind();
5670
1.74M
  if (Token == lltok::Eof)
5671
1
    return TokError("found end of file when expecting more instructions");
5672
1.74M
  LocTy Loc = Lex.getLoc();
5673
1.74M
  unsigned KeywordVal = Lex.getUIntVal();
5674
1.74M
  Lex.Lex();  // Eat the keyword.
5675
1.74M
5676
1.74M
  switch (Token) {
5677
1.74M
  
default: return Error(Loc, "expected instruction opcode")2
;
5678
1.74M
  // Terminator Instructions.
5679
1.74M
  
case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false4.16k
;
5680
1.74M
  
case lltok::kw_ret: return ParseRet(Inst, BB, PFS)301k
;
5681
1.74M
  
case lltok::kw_br: return ParseBr(Inst, PFS)85.6k
;
5682
1.74M
  
case lltok::kw_switch: return ParseSwitch(Inst, PFS)1.72k
;
5683
1.74M
  
case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS)369
;
5684
1.74M
  
case lltok::kw_invoke: return ParseInvoke(Inst, PFS)2.17k
;
5685
1.74M
  
case lltok::kw_resume: return ParseResume(Inst, PFS)265
;
5686
1.74M
  
case lltok::kw_cleanupret: return ParseCleanupRet(Inst, PFS)220
;
5687
1.74M
  
case lltok::kw_catchret: return ParseCatchRet(Inst, PFS)291
;
5688
1.74M
  
case lltok::kw_catchswitch: return ParseCatchSwitch(Inst, PFS)346
;
5689
1.74M
  
case lltok::kw_catchpad: return ParseCatchPad(Inst, PFS)371
;
5690
1.74M
  
case lltok::kw_cleanuppad: return ParseCleanupPad(Inst, PFS)312
;
5691
1.74M
  
case lltok::kw_callbr: return ParseCallBr(Inst, PFS)27
;
5692
1.74M
  // Unary Operators.
5693
1.74M
  case lltok::kw_fneg: {
5694
491
    FastMathFlags FMF = EatFastMathFlagsIfPresent();
5695
491
    int Res = ParseUnaryOp(Inst, PFS, KeywordVal, /*IsFP*/true);
5696
491
    if (Res != 0)
5697
0
      return Res;
5698
491
    if (FMF.any())
5699
103
      Inst->setFastMathFlags(FMF);
5700
491
    return false;
5701
491
  }
5702
491
  // Binary Operators.
5703
100k
  case lltok::kw_add:
5704
100k
  case lltok::kw_sub:
5705
100k
  case lltok::kw_mul:
5706
100k
  case lltok::kw_shl: {
5707
100k
    bool NUW = EatIfPresent(lltok::kw_nuw);
5708
100k
    bool NSW = EatIfPresent(lltok::kw_nsw);
5709
100k
    if (!NUW) 
NUW = EatIfPresent(lltok::kw_nuw)90.9k
;
5710
100k
5711
100k
    if (ParseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/false)) 
return true1
;
5712
100k
5713
100k
    if (NUW) 
cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true)9.44k
;
5714
100k
    if (NSW) 
cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true)27.1k
;
5715
100k
    return false;
5716
100k
  }
5717
100k
  case lltok::kw_fadd:
5718
41.0k
  case lltok::kw_fsub:
5719
41.0k
  case lltok::kw_fmul:
5720
41.0k
  case lltok::kw_fdiv:
5721
41.0k
  case lltok::kw_frem: {
5722
41.0k
    FastMathFlags FMF = EatFastMathFlagsIfPresent();
5723
41.0k
    int Res = ParseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/true);
5724
41.0k
    if (Res != 0)
5725
0
      return Res;
5726
41.0k
    if (FMF.any())
5727
4.72k
      Inst->setFastMathFlags(FMF);
5728
41.0k
    return 0;
5729
41.0k
  }
5730
41.0k
5731
41.0k
  case lltok::kw_sdiv:
5732
26.7k
  case lltok::kw_udiv:
5733
26.7k
  case lltok::kw_lshr:
5734
26.7k
  case lltok::kw_ashr: {
5735
26.7k
    bool Exact = EatIfPresent(lltok::kw_exact);
5736
26.7k
5737
26.7k
    if (ParseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/false)) 
return true0
;
5738
26.7k
    if (Exact) 
cast<BinaryOperator>(Inst)->setIsExact(true)998
;
5739
26.7k
    return false;
5740
26.7k
  }
5741
26.7k
5742
26.7k
  case lltok::kw_urem:
5743
4.82k
  case lltok::kw_srem:   return ParseArithmetic(Inst, PFS, KeywordVal,
5744
4.82k
                                                /*IsFP*/false);
5745
45.4k
  case lltok::kw_and:
5746
45.4k
  case lltok::kw_or:
5747
45.4k
  case lltok::kw_xor:    return ParseLogical(Inst, PFS, KeywordVal);
5748
71.4k
  case lltok::kw_icmp:   return ParseCompare(Inst, PFS, KeywordVal);
5749
45.4k
  case lltok::kw_fcmp: {
5750
14.2k
    FastMathFlags FMF = EatFastMathFlagsIfPresent();
5751
14.2k
    int Res = ParseCompare(Inst, PFS, KeywordVal);
5752
14.2k
    if (Res != 0)
5753
0
      return Res;
5754
14.2k
    if (FMF.any())
5755
1.18k
      Inst->setFastMathFlags(FMF);
5756
14.2k
    return 0;
5757
14.2k
  }
5758
14.2k
5759
14.2k
  // Casts.
5760
153k
  case lltok::kw_trunc:
5761
153k
  case lltok::kw_zext:
5762
153k
  case lltok::kw_sext:
5763
153k
  case lltok::kw_fptrunc:
5764
153k
  case lltok::kw_fpext:
5765
153k
  case lltok::kw_bitcast:
5766
153k
  case lltok::kw_addrspacecast:
5767
153k
  case lltok::kw_uitofp:
5768
153k
  case lltok::kw_sitofp:
5769
153k
  case lltok::kw_fptoui:
5770
153k
  case lltok::kw_fptosi:
5771
153k
  case lltok::kw_inttoptr:
5772
153k
  case lltok::kw_ptrtoint:       return ParseCast(Inst, PFS, KeywordVal);
5773
153k
  // Other.
5774
153k
  case lltok::kw_select: {
5775
33.9k
    FastMathFlags FMF = EatFastMathFlagsIfPresent();
5776
33.9k
    int Res = ParseSelect(Inst, PFS);
5777
33.9k
    if (Res != 0)
5778
1
      return Res;
5779
33.9k
    if (FMF.any()) {
5780
38
      if (!Inst->getType()->isFPOrFPVectorTy())
5781
0
        return Error(Loc, "fast-math-flags specified for select without "
5782
0
                          "floating-point scalar or vector return type");
5783
38
      Inst->setFastMathFlags(FMF);
5784
38
    }
5785
33.9k
    return 0;
5786
33.9k
  }
5787
33.9k
  
case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS)306
;
5788
35.1k
  case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
5789
37.7k
  case lltok::kw_insertelement:  return ParseInsertElement(Inst, PFS);
5790
46.1k
  case lltok::kw_shufflevector:  return ParseShuffleVector(Inst, PFS);
5791
33.9k
  
case lltok::kw_phi: return ParsePHI(Inst, PFS)28.5k
;
5792
33.9k
  
case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS)1.21k
;
5793
33.9k
  // Call.
5794
150k
  case lltok::kw_call:     return ParseCall(Inst, PFS, CallInst::TCK_None);
5795
38.2k
  case lltok::kw_tail:     return ParseCall(Inst, PFS, CallInst::TCK_Tail);
5796
33.9k
  
case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail)203
;
5797
33.9k
  
case lltok::kw_notail: return ParseCall(Inst, PFS, CallInst::TCK_NoTail)98
;
5798
33.9k
  // Memory.
5799
51.6k
  case lltok::kw_alloca:         return ParseAlloc(Inst, PFS);
5800
193k
  case lltok::kw_load:           return ParseLoad(Inst, PFS);
5801
149k
  case lltok::kw_store:          return ParseStore(Inst, PFS);
5802
33.9k
  
case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS)1.89k
;
5803
33.9k
  
case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS)7.93k
;
5804
33.9k
  
case lltok::kw_fence: return ParseFence(Inst, PFS)1.35k
;
5805
99.9k
  case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
5806
33.9k
  
case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS)13.9k
;
5807
33.9k
  
case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS)1.73k
;
5808
1.74M
  }
5809
1.74M
}
5810
5811
/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
5812
86.0k
bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
5813
86.0k
  if (Opc == Instruction::FCmp) {
5814
14.2k
    switch (Lex.getKind()) {
5815
14.2k
    
default: return TokError("expected fcmp predicate (e.g. 'oeq')")0
;
5816
14.2k
    
case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break2.99k
;
5817
14.2k
    
case lltok::kw_one: P = CmpInst::FCMP_ONE; break504
;
5818
14.2k
    
case lltok::kw_olt: P = CmpInst::FCMP_OLT; break1.64k
;
5819
14.2k
    
case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break2.39k
;
5820
14.2k
    
case lltok::kw_ole: P = CmpInst::FCMP_OLE; break717
;
5821
14.2k
    
case lltok::kw_oge: P = CmpInst::FCMP_OGE; break808
;
5822
14.2k
    
case lltok::kw_ord: P = CmpInst::FCMP_ORD; break502
;
5823
14.2k
    
case lltok::kw_uno: P = CmpInst::FCMP_UNO; break527
;
5824
14.2k
    
case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break467
;
5825
14.2k
    
case lltok::kw_une: P = CmpInst::FCMP_UNE; break859
;
5826
14.2k
    
case lltok::kw_ult: P = CmpInst::FCMP_ULT; break639
;
5827
14.2k
    
case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break634
;
5828
14.2k
    
case lltok::kw_ule: P = CmpInst::FCMP_ULE; break545
;
5829
14.2k
    
case lltok::kw_uge: P = CmpInst::FCMP_UGE; break620
;
5830
14.2k
    
case lltok::kw_true: P = CmpInst::FCMP_TRUE; break215
;
5831
14.2k
    
case lltok::kw_false: P = CmpInst::FCMP_FALSE; break217
;
5832
71.7k
    }
5833
71.7k
  } else {
5834
71.7k
    switch (Lex.getKind()) {
5835
71.7k
    
default: return TokError("expected icmp predicate (e.g. 'eq')")0
;
5836
71.7k
    
case lltok::kw_eq: P = CmpInst::ICMP_EQ; break24.7k
;
5837
71.7k
    
case lltok::kw_ne: P = CmpInst::ICMP_NE; break8.88k
;
5838
71.7k
    
case lltok::kw_slt: P = CmpInst::ICMP_SLT; break10.0k
;
5839
71.7k
    
case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break9.87k
;
5840
71.7k
    
case lltok::kw_sle: P = CmpInst::ICMP_SLE; break2.00k
;
5841
71.7k
    
case lltok::kw_sge: P = CmpInst::ICMP_SGE; break2.04k
;
5842
71.7k
    
case lltok::kw_ult: P = CmpInst::ICMP_ULT; break6.45k
;
5843
71.7k
    
case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break4.33k
;
5844
71.7k
    
case lltok::kw_ule: P = CmpInst::ICMP_ULE; break1.60k
;
5845
71.7k
    
case lltok::kw_uge: P = CmpInst::ICMP_UGE; break1.81k
;
5846
86.0k
    }
5847
86.0k
  }
5848
86.0k
  Lex.Lex();
5849
86.0k
  return false;
5850
86.0k
}
5851
5852
//===----------------------------------------------------------------------===//
5853
// Terminator Instructions.
5854
//===----------------------------------------------------------------------===//
5855
5856
/// ParseRet - Parse a return instruction.
5857
///   ::= 'ret' void (',' !dbg, !1)*
5858
///   ::= 'ret' TypeAndValue (',' !dbg, !1)*
5859
bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
5860
301k
                        PerFunctionState &PFS) {
5861
301k
  SMLoc TypeLoc = Lex.getLoc();
5862
301k
  Type *Ty = nullptr;
5863
301k
  if (ParseType(Ty, true /*void allowed*/)) 
return true0
;
5864
301k
5865
301k
  Type *ResType = PFS.getFunction().getReturnType();
5866
301k
5867
301k
  if (Ty->isVoidTy()) {
5868
89.8k
    if (!ResType->isVoidTy())
5869
0
      return Error(TypeLoc, "value doesn't match function result type '" +
5870
0
                   getTypeString(ResType) + "'");
5871
89.8k
5872
89.8k
    Inst = ReturnInst::Create(Context);
5873
89.8k
    return false;
5874
89.8k
  }
5875
211k
5876
211k
  Value *RV;
5877
211k
  if (ParseValue(Ty, RV, PFS)) 
return true7
;
5878
211k
5879
211k
  if (ResType != RV->getType())
5880
2
    return Error(TypeLoc, "value doesn't match function result type '" +
5881
2
                 getTypeString(ResType) + "'");
5882
211k
5883
211k
  Inst = ReturnInst::Create(Context, RV);
5884
211k
  return false;
5885
211k
}
5886
5887
/// ParseBr
5888
///   ::= 'br' TypeAndValue
5889
///   ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
5890
85.6k
bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
5891
85.6k
  LocTy Loc, Loc2;
5892
85.6k
  Value *Op0;
5893
85.6k
  BasicBlock *Op1, *Op2;
5894
85.6k
  if (ParseTypeAndValue(Op0, Loc, PFS)) 
return true0
;
5895
85.6k
5896
85.6k
  if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
5897
44.5k
    Inst = BranchInst::Create(BB);
5898
44.5k
    return false;
5899
44.5k
  }
5900
41.0k
5901
41.0k
  if (Op0->getType() != Type::getInt1Ty(Context))
5902
0
    return Error(Loc, "branch condition must have 'i1' type");
5903
41.0k
5904
41.0k
  if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
5905
41.0k
      ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
5906
41.0k
      ParseToken(lltok::comma, "expected ',' after true destination") ||
5907
41.0k
      ParseTypeAndBasicBlock(Op2, Loc2, PFS))
5908
0
    return true;
5909
41.0k
5910
41.0k
  Inst = BranchInst::Create(Op1, Op2, Op0);
5911
41.0k
  return false;
5912
41.0k
}
5913
5914
/// ParseSwitch
5915
///  Instruction
5916
///    ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
5917
///  JumpTable
5918
///    ::= (TypeAndValue ',' TypeAndValue)*
5919
1.72k
bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
5920
1.72k
  LocTy CondLoc, BBLoc;
5921
1.72k
  Value *Cond;
5922
1.72k
  BasicBlock *DefaultBB;
5923
1.72k
  if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
5924
1.72k
      ParseToken(lltok::comma, "expected ',' after switch condition") ||
5925
1.72k
      ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
5926
1.72k
      ParseToken(lltok::lsquare, "expected '[' with switch table"))
5927
0
    return true;
5928
1.72k
5929
1.72k
  if (!Cond->getType()->isIntegerTy())
5930
0
    return Error(CondLoc, "switch condition must have integer type");
5931
1.72k
5932
1.72k
  // Parse the jump table pairs.
5933
1.72k
  SmallPtrSet<Value*, 32> SeenCases;
5934
1.72k
  SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
5935
11.0k
  while (Lex.getKind() != lltok::rsquare) {
5936
9.28k
    Value *Constant;
5937
9.28k
    BasicBlock *DestBB;
5938
9.28k
5939
9.28k
    if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
5940
9.28k
        ParseToken(lltok::comma, "expected ',' after case value") ||
5941
9.28k
        ParseTypeAndBasicBlock(DestBB, PFS))
5942
0
      return true;
5943
9.28k
5944
9.28k
    if (!SeenCases.insert(Constant).second)
5945
0
      return Error(CondLoc, "duplicate case value in switch");
5946
9.28k
    if (!isa<ConstantInt>(Constant))
5947
0
      return Error(CondLoc, "case value is not a constant integer");
5948
9.28k
5949
9.28k
    Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
5950
9.28k
  }
5951
1.72k
5952
1.72k
  Lex.Lex();  // Eat the ']'.
5953
1.72k
5954
1.72k
  SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
5955
11.0k
  for (unsigned i = 0, e = Table.size(); i != e; 
++i9.28k
)
5956
9.28k
    SI->addCase(Table[i].first, Table[i].second);
5957
1.72k
  Inst = SI;
5958
1.72k
  return false;
5959
1.72k
}
5960
5961
/// ParseIndirectBr
5962
///  Instruction
5963
///    ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
5964
369
bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
5965
369
  LocTy AddrLoc;
5966
369
  Value *Address;
5967
369
  if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
5968
369
      ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
5969
369
      ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
5970
0
    return true;
5971
369
5972
369
  if (!Address->getType()->isPointerTy())
5973
0
    return Error(AddrLoc, "indirectbr address must have pointer type");
5974
369
5975
369
  // Parse the destination list.
5976
369
  SmallVector<BasicBlock*, 16> DestList;
5977
369
5978
369
  if (Lex.getKind() != lltok::rsquare) {
5979
365
    BasicBlock *DestBB;
5980
365
    if (ParseTypeAndBasicBlock(DestBB, PFS))
5981
0
      return true;
5982
365
    DestList.push_back(DestBB);
5983
365
5984
1.04k
    while (EatIfPresent(lltok::comma)) {
5985
683
      if (ParseTypeAndBasicBlock(DestBB, PFS))
5986
0
        return true;
5987
683
      DestList.push_back(DestBB);
5988
683
    }
5989
365
  }
5990
369
5991
369
  if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
5992
0
    return true;
5993
369
5994
369
  IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
5995
1.41k
  for (unsigned i = 0, e = DestList.size(); i != e; 
++i1.04k
)
5996
1.04k
    IBI->addDestination(DestList[i]);
5997
369
  Inst = IBI;
5998
369
  return false;
5999
369
}
6000
6001
/// ParseInvoke
6002
///   ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
6003
///       OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
6004
2.17k
bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
6005
2.17k
  LocTy CallLoc = Lex.getLoc();
6006
2.17k
  AttrBuilder RetAttrs, FnAttrs;
6007
2.17k
  std::vector<unsigned> FwdRefAttrGrps;
6008
2.17k
  LocTy NoBuiltinLoc;
6009
2.17k
  unsigned CC;
6010
2.17k
  unsigned InvokeAddrSpace;
6011
2.17k
  Type *RetType = nullptr;
6012
2.17k
  LocTy RetTypeLoc;
6013
2.17k
  ValID CalleeID;
6014
2.17k
  SmallVector<ParamInfo, 16> ArgList;
6015
2.17k
  SmallVector<OperandBundleDef, 2> BundleList;
6016
2.17k
6017
2.17k
  BasicBlock *NormalBB, *UnwindBB;
6018
2.17k
  if (ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
6019
2.17k
      ParseOptionalProgramAddrSpace(InvokeAddrSpace) ||
6020
2.17k
      ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
6021
2.17k
      ParseValID(CalleeID) || ParseParameterList(ArgList, PFS) ||
6022
2.17k
      ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
6023
2.17k
                                 NoBuiltinLoc) ||
6024
2.17k
      ParseOptionalOperandBundles(BundleList, PFS) ||
6025
2.17k
      ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
6026
2.17k
      ParseTypeAndBasicBlock(NormalBB, PFS) ||
6027
2.17k
      ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
6028
2.17k
      ParseTypeAndBasicBlock(UnwindBB, PFS))
6029
0
    return true;
6030
2.17k
6031
2.17k
  // If RetType is a non-function pointer type, then this is the short syntax
6032
2.17k
  // for the call, which means that RetType is just the return type.  Infer the
6033
2.17k
  // rest of the function argument types from the arguments that are present.
6034
2.17k
  FunctionType *Ty = dyn_cast<FunctionType>(RetType);
6035
2.17k
  if (!Ty) {
6036
2.10k
    // Pull out the types of all of the arguments...
6037
2.10k
    std::vector<Type*> ParamTypes;
6038
4.07k
    for (unsigned i = 0, e = ArgList.size(); i != e; 
++i1.97k
)
6039
1.97k
      ParamTypes.push_back(ArgList[i].V->getType());
6040
2.10k
6041
2.10k
    if (!FunctionType::isValidReturnType(RetType))
6042
0
      return Error(RetTypeLoc, "Invalid result type for LLVM function");
6043
2.10k
6044
2.10k
    Ty = FunctionType::get(RetType, ParamTypes, false);
6045
2.10k
  }
6046
2.17k
6047
2.17k
  CalleeID.FTy = Ty;
6048
2.17k
6049
2.17k
  // Look up the callee.
6050
2.17k
  Value *Callee;
6051
2.17k
  if (ConvertValIDToValue(PointerType::get(Ty, InvokeAddrSpace), CalleeID,
6052
2.17k
                          Callee, &PFS, /*IsCall=*/true))
6053
1
    return true;
6054
2.17k
6055
2.17k
  // Set up the Attribute for the function.
6056
2.17k
  SmallVector<Value *, 8> Args;
6057
2.17k
  SmallVector<AttributeSet, 8> ArgAttrs;
6058
2.17k
6059
2.17k
  // Loop through FunctionType's arguments and ensure they are specified
6060
2.17k
  // correctly.  Also, gather any parameter attributes.
6061
2.17k
  FunctionType::param_iterator I = Ty->param_begin();
6062
2.17k
  FunctionType::param_iterator E = Ty->param_end();
6063
4.44k
  for (unsigned i = 0, e = ArgList.size(); i != e; 
++i2.27k
) {
6064
2.27k
    Type *ExpectedTy = nullptr;
6065
2.27k
    if (I != E) {
6066
2.08k
      ExpectedTy = *I++;
6067
2.08k
    } else 
if (186
!Ty->isVarArg()186
) {
6068
0
      return Error(ArgList[i].Loc, "too many arguments specified");
6069
0
    }
6070
2.27k
6071
2.27k
    if (ExpectedTy && 
ExpectedTy != ArgList[i].V->getType()2.08k
)
6072
0
      return Error(ArgList[i].Loc, "argument is not of expected type '" +
6073
0
                   getTypeString(ExpectedTy) + "'");
6074
2.27k
    Args.push_back(ArgList[i].V);
6075
2.27k
    ArgAttrs.push_back(ArgList[i].Attrs);
6076
2.27k
  }
6077
2.17k
6078
2.17k
  if (I != E)
6079
0
    return Error(CallLoc, "not enough parameters specified for call");
6080
2.17k
6081
2.17k
  if (FnAttrs.hasAlignmentAttr())
6082
0
    return Error(CallLoc, "invoke instructions may not have an alignment");
6083
2.17k
6084
2.17k
  // Finish off the Attribute and check them
6085
2.17k
  AttributeList PAL =
6086
2.17k
      AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
6087
2.17k
                         AttributeSet::get(Context, RetAttrs), ArgAttrs);
6088
2.17k
6089
2.17k
  InvokeInst *II =
6090
2.17k
      InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args, BundleList);
6091
2.17k
  II->setCallingConv(CC);
6092
2.17k
  II->setAttributes(PAL);
6093
2.17k
  ForwardRefAttrGroups[II] = FwdRefAttrGrps;
6094
2.17k
  Inst = II;
6095
2.17k
  return false;
6096
2.17k
}
6097
6098
/// ParseResume
6099
///   ::= 'resume' TypeAndValue
6100
265
bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
6101
265
  Value *Exn; LocTy ExnLoc;
6102
265
  if (ParseTypeAndValue(Exn, ExnLoc, PFS))
6103
0
    return true;
6104
265
6105
265
  ResumeInst *RI = ResumeInst::Create(Exn);
6106
265
  Inst = RI;
6107
265
  return false;
6108
265
}
6109
6110
bool LLParser::ParseExceptionArgs(SmallVectorImpl<Value *> &Args,
6111
683
                                  PerFunctionState &PFS) {
6112
683
  if (ParseToken(lltok::lsquare, "expected '[' in catchpad/cleanuppad"))
6113
0
    return true;
6114
683
6115
1.29k
  
while (683
Lex.getKind() != lltok::rsquare) {
6116
608
    // If this isn't the first argument, we need a comma.
6117
608
    if (!Args.empty() &&
6118
608
        
ParseToken(lltok::comma, "expected ',' in argument list")290
)
6119
0
      return true;
6120
608
6121
608
    // Parse the argument.
6122
608
    LocTy ArgLoc;
6123
608
    Type *ArgTy = nullptr;
6124
608
    if (ParseType(ArgTy, ArgLoc))
6125
0
      return true;
6126
608
6127
608
    Value *V;
6128
608
    if (ArgTy->isMetadataTy()) {
6129
0
      if (ParseMetadataAsValue(V, PFS))
6130
0
        return true;
6131
608
    } else {
6132
608
      if (ParseValue(ArgTy, V, PFS))
6133
0
        return true;
6134
608
    }
6135
608
    Args.push_back(V);
6136
608
  }
6137
683
6138
683
  Lex.Lex();  // Lex the ']'.
6139
683
  return false;
6140
683
}
6141
6142
/// ParseCleanupRet
6143
///   ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue)
6144
220
bool LLParser::ParseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) {
6145
220
  Value *CleanupPad = nullptr;
6146
220
6147
220
  if (ParseToken(lltok::kw_from, "expected 'from' after cleanupret"))
6148
0
    return true;
6149
220
6150
220
  if (ParseValue(Type::getTokenTy(Context), CleanupPad, PFS))
6151
0
    return true;
6152
220
6153
220
  if (ParseToken(lltok::kw_unwind, "expected 'unwind' in cleanupret"))
6154
0
    return true;
6155
220
6156
220
  BasicBlock *UnwindBB = nullptr;
6157
220
  if (Lex.getKind() == lltok::kw_to) {
6158
154
    Lex.Lex();
6159
154
    if (ParseToken(lltok::kw_caller, "expected 'caller' in cleanupret"))
6160
0
      return true;
6161
66
  } else {
6162
66
    if (ParseTypeAndBasicBlock(UnwindBB, PFS)) {
6163
0
      return true;
6164
0
    }
6165
220
  }
6166
220
6167
220
  Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB);
6168
220
  return false;
6169
220
}
6170
6171
/// ParseCatchRet
6172
///   ::= 'catchret' from Parent Value 'to' TypeAndValue
6173
291
bool LLParser::ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS) {
6174
291
  Value *CatchPad = nullptr;
6175
291
6176
291
  if (ParseToken(lltok::kw_from, "expected 'from' after catchret"))
6177
0
    return true;
6178
291
6179
291
  if (ParseValue(Type::getTokenTy(Context), CatchPad, PFS))
6180
0
    return true;
6181
291
6182
291
  BasicBlock *BB;
6183
291
  if (ParseToken(lltok::kw_to, "expected 'to' in catchret") ||
6184
291
      ParseTypeAndBasicBlock(BB, PFS))
6185
0
      return true;
6186
291
6187
291
  Inst = CatchReturnInst::Create(CatchPad, BB);
6188
291
  return false;
6189
291
}
6190
6191
/// ParseCatchSwitch
6192
///   ::= 'catchswitch' within Parent
6193
346
bool LLParser::ParseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) {
6194
346
  Value *ParentPad;
6195
346
6196
346
  if (ParseToken(lltok::kw_within, "expected 'within' after catchswitch"))
6197
0
    return true;
6198
346
6199
346
  if (Lex.getKind() != lltok::kw_none && 
Lex.getKind() != lltok::LocalVar38
&&
6200
346
      
Lex.getKind() != lltok::LocalVarID8
)
6201
0
    return TokError("expected scope value for catchswitch");
6202
346
6203
346
  if (ParseValue(Type::getTokenTy(Context), ParentPad, PFS))
6204
0
    return true;
6205
346
6206
346
  if (ParseToken(lltok::lsquare, "expected '[' with catchswitch labels"))
6207
0
    return true;
6208
346
6209
346
  SmallVector<BasicBlock *, 32> Table;
6210
371
  do {
6211
371
    BasicBlock *DestBB;
6212
371
    if (ParseTypeAndBasicBlock(DestBB, PFS))
6213
0
      return true;
6214
371
    Table.push_back(DestBB);
6215
371
  } while (EatIfPresent(lltok::comma));
6216
346
6217
346
  if (ParseToken(lltok::rsquare, "expected ']' after catchswitch labels"))
6218
0
    return true;
6219
346
6220
346
  if (ParseToken(lltok::kw_unwind,
6221
346
                 "expected 'unwind' after catchswitch scope"))
6222
0
    return true;
6223
346
6224
346
  BasicBlock *UnwindBB = nullptr;
6225
346
  if (EatIfPresent(lltok::kw_to)) {
6226
273
    if (ParseToken(lltok::kw_caller, "expected 'caller' in catchswitch"))
6227
0
      return true;
6228
73
  } else {
6229
73
    if (ParseTypeAndBasicBlock(UnwindBB, PFS))
6230
0
      return true;
6231
346
  }
6232
346
6233
346
  auto *CatchSwitch =
6234
346
      CatchSwitchInst::Create(ParentPad, UnwindBB, Table.size());
6235
346
  for (BasicBlock *DestBB : Table)
6236
371
    CatchSwitch->addHandler(DestBB);
6237
346
  Inst = CatchSwitch;
6238
346
  return false;
6239
346
}
6240
6241
/// ParseCatchPad
6242
///   ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue
6243
371
bool LLParser::ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS) {
6244
371
  Value *CatchSwitch = nullptr;
6245
371
6246
371
  if (ParseToken(lltok::kw_within, "expected 'within' after catchpad"))
6247
0
    return true;
6248
371
6249
371
  if (Lex.getKind() != lltok::LocalVar && 
Lex.getKind() != lltok::LocalVarID119
)
6250
0
    return TokError("expected scope value for catchpad");
6251
371
6252
371
  if (ParseValue(Type::getTokenTy(Context), CatchSwitch, PFS))
6253
0
    return true;
6254
371
6255
371
  SmallVector<Value *, 8> Args;
6256
371
  if (ParseExceptionArgs(Args, PFS))
6257
0
    return true;
6258
371
6259
371
  Inst = CatchPadInst::Create(CatchSwitch, Args);
6260
371
  return false;
6261
371
}
6262
6263
/// ParseCleanupPad
6264
///   ::= 'cleanuppad' within Parent ParamList
6265
312
bool LLParser::ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
6266
312
  Value *ParentPad = nullptr;
6267
312
6268
312
  if (ParseToken(lltok::kw_within, "expected 'within' after cleanuppad"))
6269
0
    return true;
6270
312
6271
312
  if (Lex.getKind() != lltok::kw_none && 
Lex.getKind() != lltok::LocalVar74
&&
6272
312
      
Lex.getKind() != lltok::LocalVarID28
)
6273
0
    return TokError("expected scope value for cleanuppad");
6274
312
6275
312
  if (ParseValue(Type::getTokenTy(Context), ParentPad, PFS))
6276
0
    return true;
6277
312
6278
312
  SmallVector<Value *, 8> Args;
6279
312
  if (ParseExceptionArgs(Args, PFS))
6280
0
    return true;
6281
312
6282
312
  Inst = CleanupPadInst::Create(ParentPad, Args);
6283
312
  return false;
6284
312
}
6285
6286
//===----------------------------------------------------------------------===//
6287
// Unary Operators.
6288
//===----------------------------------------------------------------------===//
6289
6290
/// ParseUnaryOp
6291
///  ::= UnaryOp TypeAndValue ',' Value
6292
///
6293
/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
6294
/// operand is allowed.
6295
bool LLParser::ParseUnaryOp(Instruction *&Inst, PerFunctionState &PFS,
6296
491
                            unsigned Opc, bool IsFP) {
6297
491
  LocTy Loc; Value *LHS;
6298
491
  if (ParseTypeAndValue(LHS, Loc, PFS))
6299
0
    return true;
6300
491
6301
491
  bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
6302
491
                    : 
LHS->getType()->isIntOrIntVectorTy()0
;
6303
491
6304
491
  if (!Valid)
6305
0
    return Error(Loc, "invalid operand type for instruction");
6306
491
6307
491
  Inst = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS);
6308
491
  return false;
6309
491
}
6310
6311
/// ParseCallBr
6312
///   ::= 'callbr' OptionalCallingConv OptionalAttrs Type Value ParamList
6313
///       OptionalAttrs OptionalOperandBundles 'to' TypeAndValue
6314
///       '[' LabelList ']'
6315
27
bool LLParser::ParseCallBr(Instruction *&Inst, PerFunctionState &PFS) {
6316
27
  LocTy CallLoc = Lex.getLoc();
6317
27
  AttrBuilder RetAttrs, FnAttrs;
6318
27
  std::vector<unsigned> FwdRefAttrGrps;
6319
27
  LocTy NoBuiltinLoc;
6320
27
  unsigned CC;
6321
27
  Type *RetType = nullptr;
6322
27
  LocTy RetTypeLoc;
6323
27
  ValID CalleeID;
6324
27
  SmallVector<ParamInfo, 16> ArgList;
6325
27
  SmallVector<OperandBundleDef, 2> BundleList;
6326
27
6327
27
  BasicBlock *DefaultDest;
6328
27
  if (ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
6329
27
      ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
6330
27
      ParseValID(CalleeID) || ParseParameterList(ArgList, PFS) ||
6331
27
      ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
6332
27
                                 NoBuiltinLoc) ||
6333
27
      ParseOptionalOperandBundles(BundleList, PFS) ||
6334
27
      ParseToken(lltok::kw_to, "expected 'to' in callbr") ||
6335
27
      ParseTypeAndBasicBlock(DefaultDest, PFS) ||
6336
27
      ParseToken(lltok::lsquare, "expected '[' in callbr"))
6337
0
    return true;
6338
27
6339
27
  // Parse the destination list.
6340
27
  SmallVector<BasicBlock *, 16> IndirectDests;
6341
27
6342
27
  if (Lex.getKind() != lltok::rsquare) {
6343
27
    BasicBlock *DestBB;
6344
27
    if (ParseTypeAndBasicBlock(DestBB, PFS))
6345
0
      return true;
6346
27
    IndirectDests.push_back(DestBB);
6347
27
6348
41
    while (EatIfPresent(lltok::comma)) {
6349
14
      if (ParseTypeAndBasicBlock(DestBB, PFS))
6350
0
        return true;
6351
14
      IndirectDests.push_back(DestBB);
6352
14
    }
6353
27
  }
6354
27
6355
27
  if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
6356
0
    return true;
6357
27
6358
27
  // If RetType is a non-function pointer type, then this is the short syntax
6359
27
  // for the call, which means that RetType is just the return type.  Infer the
6360
27
  // rest of the function argument types from the arguments that are present.
6361
27
  FunctionType *Ty = dyn_cast<FunctionType>(RetType);
6362
27
  if (!Ty) {
6363
27
    // Pull out the types of all of the arguments...
6364
27
    std::vector<Type *> ParamTypes;
6365
87
    for (unsigned i = 0, e = ArgList.size(); i != e; 
++i60
)
6366
60
      ParamTypes.push_back(ArgList[i].V->getType());
6367
27
6368
27
    if (!FunctionType::isValidReturnType(RetType))
6369
0
      return Error(RetTypeLoc, "Invalid result type for LLVM function");
6370
27
6371
27
    Ty = FunctionType::get(RetType, ParamTypes, false);
6372
27
  }
6373
27
6374
27
  CalleeID.FTy = Ty;
6375
27
6376
27
  // Look up the callee.
6377
27
  Value *Callee;
6378
27
  if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS,
6379
27
                          /*IsCall=*/true))
6380
0
    return true;
6381
27
6382
27
  if (isa<InlineAsm>(Callee) && !Ty->getReturnType()->isVoidTy())
6383
1
    return Error(RetTypeLoc, "asm-goto outputs not supported");
6384
26
6385
26
  // Set up the Attribute for the function.
6386
26
  SmallVector<Value *, 8> Args;
6387
26
  SmallVector<AttributeSet, 8> ArgAttrs;
6388
26
6389
26
  // Loop through FunctionType's arguments and ensure they are specified
6390
26
  // correctly.  Also, gather any parameter attributes.
6391
26
  FunctionType::param_iterator I = Ty->param_begin();
6392
26
  FunctionType::param_iterator E = Ty->param_end();
6393
84
  for (unsigned i = 0, e = ArgList.size(); i != e; 
++i58
) {
6394
58
    Type *ExpectedTy = nullptr;
6395
58
    if (I != E) {
6396
58
      ExpectedTy = *I++;
6397
58
    } else 
if (0
!Ty->isVarArg()0
) {
6398
0
      return Error(ArgList[i].Loc, "too many arguments specified");
6399
0
    }
6400
58
6401
58
    if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
6402
0
      return Error(ArgList[i].Loc, "argument is not of expected type '" +
6403
0
                                       getTypeString(ExpectedTy) + "'");
6404
58
    Args.push_back(ArgList[i].V);
6405
58
    ArgAttrs.push_back(ArgList[i].Attrs);
6406
58
  }
6407
26
6408
26
  if (I != E)
6409
0
    return Error(CallLoc, "not enough parameters specified for call");
6410
26
6411
26
  if (FnAttrs.hasAlignmentAttr())
6412
0
    return Error(CallLoc, "callbr instructions may not have an alignment");
6413
26
6414
26
  // Finish off the Attribute and check them
6415
26
  AttributeList PAL =
6416
26
      AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
6417
26
                         AttributeSet::get(Context, RetAttrs), ArgAttrs);
6418
26
6419
26
  CallBrInst *CBI =
6420
26
      CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
6421
26
                         BundleList);
6422
26
  CBI->setCallingConv(CC);
6423
26
  CBI->setAttributes(PAL);
6424
26
  ForwardRefAttrGroups[CBI] = FwdRefAttrGrps;
6425
26
  Inst = CBI;
6426
26
  return false;
6427
26
}
6428
6429
//===----------------------------------------------------------------------===//
6430
// Binary Operators.
6431
//===----------------------------------------------------------------------===//
6432
6433
/// ParseArithmetic
6434
///  ::= ArithmeticOps TypeAndValue ',' Value
6435
///
6436
/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
6437
/// operand is allowed.
6438
bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
6439
172k
                               unsigned Opc, bool IsFP) {
6440
172k
  LocTy Loc; Value *LHS, *RHS;
6441
172k
  if (ParseTypeAndValue(LHS, Loc, PFS) ||
6442
172k
      ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
6443
172k
      ParseValue(LHS->getType(), RHS, PFS))
6444
1
    return true;
6445
172k
6446
172k
  bool Valid = IsFP ? 
LHS->getType()->isFPOrFPVectorTy()41.0k
6447
172k
                    : 
LHS->getType()->isIntOrIntVectorTy()131k
;
6448
172k
6449
172k
  if (!Valid)
6450
0
    return Error(Loc, "invalid operand type for instruction");
6451
172k
6452
172k
  Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
6453
172k
  return false;
6454
172k
}
6455
6456
/// ParseLogical
6457
///  ::= ArithmeticOps TypeAndValue ',' Value {
6458
bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
6459
45.4k
                            unsigned Opc) {
6460
45.4k
  LocTy Loc; Value *LHS, *RHS;
6461
45.4k
  if (ParseTypeAndValue(LHS, Loc, PFS) ||
6462
45.4k
      ParseToken(lltok::comma, "expected ',' in logical operation") ||
6463
45.4k
      ParseValue(LHS->getType(), RHS, PFS))
6464
0
    return true;
6465
45.4k
6466
45.4k
  if (!LHS->getType()->isIntOrIntVectorTy())
6467
0
    return Error(Loc,"instruction requires integer or integer vector operands");
6468
45.4k
6469
45.4k
  Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
6470
45.4k
  return false;
6471
45.4k
}
6472
6473
/// ParseCompare
6474
///  ::= 'icmp' IPredicates TypeAndValue ',' Value
6475
///  ::= 'fcmp' FPredicates TypeAndValue ',' Value
6476
bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
6477
85.7k
                            unsigned Opc) {
6478
85.7k
  // Parse the integer/fp comparison predicate.
6479
85.7k
  LocTy Loc;
6480
85.7k
  unsigned Pred;
6481
85.7k
  Value *LHS, *RHS;
6482
85.7k
  if (ParseCmpPredicate(Pred, Opc) ||
6483
85.7k
      ParseTypeAndValue(LHS, Loc, PFS) ||
6484
85.7k
      ParseToken(lltok::comma, "expected ',' after compare value") ||
6485
85.7k
      ParseValue(LHS->getType(), RHS, PFS))
6486
0
    return true;
6487
85.7k
6488
85.7k
  if (Opc == Instruction::FCmp) {
6489
14.2k
    if (!LHS->getType()->isFPOrFPVectorTy())
6490
0
      return Error(Loc, "fcmp requires floating point operands");
6491
14.2k
    Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
6492
71.4k
  } else {
6493
71.4k
    assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
6494
71.4k
    if (!LHS->getType()->isIntOrIntVectorTy() &&
6495
71.4k
        
!LHS->getType()->isPtrOrPtrVectorTy()1.98k
)
6496
0
      return Error(Loc, "icmp requires integer operands");
6497
71.4k
    Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
6498
71.4k
  }
6499
85.7k
  return false;
6500
85.7k
}
6501
6502
//===----------------------------------------------------------------------===//
6503
// Other Instructions.
6504
//===----------------------------------------------------------------------===//
6505
6506
6507
/// ParseCast
6508
///   ::= CastOpc TypeAndValue 'to' Type
6509
bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
6510
153k
                         unsigned Opc) {
6511
153k
  LocTy Loc;
6512
153k
  Value *Op;
6513
153k
  Type *DestTy = nullptr;
6514
153k
  if (ParseTypeAndValue(Op, Loc, PFS) ||
6515
153k
      ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
6516
153k
      ParseType(DestTy))
6517
1
    return true;
6518
153k
6519
153k
  if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
6520
10
    CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
6521
10
    return Error(Loc, "invalid cast opcode for cast from '" +
6522
10
                 getTypeString(Op->getType()) + "' to '" +
6523
10
                 getTypeString(DestTy) + "'");
6524
10
  }
6525
153k
  Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
6526
153k
  return false;
6527
153k
}
6528
6529
/// ParseSelect
6530
///   ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
6531
33.9k
bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
6532
33.9k
  LocTy Loc;
6533
33.9k
  Value *Op0, *Op1, *Op2;
6534
33.9k
  if (ParseTypeAndValue(Op0, Loc, PFS) ||
6535
33.9k
      ParseToken(lltok::comma, "expected ',' after select condition") ||
6536
33.9k
      ParseTypeAndValue(Op1, PFS) ||
6537
33.9k
      ParseToken(lltok::comma, "expected ',' after select value") ||
6538
33.9k
      ParseTypeAndValue(Op2, PFS))
6539
0
    return true;
6540
33.9k
6541
33.9k
  if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
6542
1
    return Error(Loc, Reason);
6543
33.9k
6544
33.9k
  Inst = SelectInst::Create(Op0, Op1, Op2);
6545
33.9k
  return false;
6546
33.9k
}
6547
6548
/// ParseVA_Arg
6549
///   ::= 'va_arg' TypeAndValue ',' Type
6550
306
bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
6551
306
  Value *Op;
6552
306
  Type *EltTy = nullptr;
6553
306
  LocTy TypeLoc;
6554
306
  if (ParseTypeAndValue(Op, PFS) ||
6555
306
      ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
6556
306
      ParseType(EltTy, TypeLoc))
6557
0
    return true;
6558
306
6559
306
  if (!EltTy->isFirstClassType())
6560
0
    return Error(TypeLoc, "va_arg requires operand with first class type");
6561
306
6562
306
  Inst = new VAArgInst(Op, EltTy);
6563
306
  return false;
6564
306
}
6565
6566
/// ParseExtractElement
6567
///   ::= 'extractelement' TypeAndValue ',' TypeAndValue
6568
35.1k
bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
6569
35.1k
  LocTy Loc;
6570
35.1k
  Value *Op0, *Op1;
6571
35.1k
  if (ParseTypeAndValue(Op0, Loc, PFS) ||
6572
35.1k
      ParseToken(lltok::comma, "expected ',' after extract value") ||
6573
35.1k
      ParseTypeAndValue(Op1, PFS))
6574
0
    return true;
6575
35.1k
6576
35.1k
  if (!ExtractElementInst::isValidOperands(Op0, Op1))
6577
0
    return Error(Loc, "invalid extractelement operands");
6578
35.1k
6579
35.1k
  Inst = ExtractElementInst::Create(Op0, Op1);
6580
35.1k
  return false;
6581
35.1k
}
6582
6583
/// ParseInsertElement
6584
///   ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
6585
37.7k
bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
6586
37.7k
  LocTy Loc;
6587
37.7k
  Value *Op0, *Op1, *Op2;
6588
37.7k
  if (ParseTypeAndValue(Op0, Loc, PFS) ||
6589
37.7k
      ParseToken(lltok::comma, "expected ',' after insertelement value") ||
6590
37.7k
      ParseTypeAndValue(Op1, PFS) ||
6591
37.7k
      ParseToken(lltok::comma, "expected ',' after insertelement value") ||
6592
37.7k
      ParseTypeAndValue(Op2, PFS))
6593
0
    return true;
6594
37.7k
6595
37.7k
  if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
6596
0
    return Error(Loc, "invalid insertelement operands");
6597
37.7k
6598
37.7k
  Inst = InsertElementInst::Create(Op0, Op1, Op2);
6599
37.7k
  return false;
6600
37.7k
}
6601
6602
/// ParseShuffleVector
6603
///   ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
6604
46.1k
bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
6605
46.1k
  LocTy Loc;
6606
46.1k
  Value *Op0, *Op1, *Op2;
6607
46.1k
  if (ParseTypeAndValue(Op0, Loc, PFS) ||
6608
46.1k
      ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
6609
46.1k
      ParseTypeAndValue(Op1, PFS) ||
6610
46.1k
      ParseToken(lltok::comma, "expected ',' after shuffle value") ||
6611
46.1k
      ParseTypeAndValue(Op2, PFS))
6612
0
    return true;
6613
46.1k
6614
46.1k
  if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
6615
0
    return Error(Loc, "invalid shufflevector operands");
6616
46.1k
6617
46.1k
  Inst = new ShuffleVectorInst(Op0, Op1, Op2);
6618
46.1k
  return false;
6619
46.1k
}
6620
6621
/// ParsePHI
6622
///   ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
6623
28.5k
int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
6624
28.5k
  Type *Ty = nullptr;  LocTy TypeLoc;
6625
28.5k
  Value *Op0, *Op1;
6626
28.5k
6627
28.5k
  if (ParseType(Ty, TypeLoc) ||
6628
28.5k
      ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
6629
28.5k
      ParseValue(Ty, Op0, PFS) ||
6630
28.5k
      ParseToken(lltok::comma, "expected ',' after insertelement value") ||
6631
28.5k
      ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
6632
28.5k
      ParseToken(lltok::rsquare, "expected ']' in phi value list"))
6633
0
    return true;
6634
28.5k
6635
28.5k
  bool AteExtraComma = false;
6636
28.5k
  SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
6637
28.5k
6638
59.6k
  while (true) {
6639
59.6k
    PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
6640
59.6k
6641
59.6k
    if (!EatIfPresent(lltok::comma))
6642
28.4k
      break;
6643
31.2k
6644
31.2k
    if (Lex.getKind() == lltok::MetadataVar) {
6645
77
      AteExtraComma = true;
6646
77
      break;
6647
77
    }
6648
31.1k
6649
31.1k
    if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
6650
31.1k
        ParseValue(Ty, Op0, PFS) ||
6651
31.1k
        ParseToken(lltok::comma, "expected ',' after insertelement value") ||
6652
31.1k
        ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
6653
31.1k
        ParseToken(lltok::rsquare, "expected ']' in phi value list"))
6654
0
      return true;
6655
31.1k
  }
6656
28.5k
6657
28.5k
  if (!Ty->isFirstClassType())
6658
0
    return Error(TypeLoc, "phi node must have first class type");
6659
28.5k
6660
28.5k
  PHINode *PN = PHINode::Create(Ty, PHIVals.size());
6661
88.1k
  for (unsigned i = 0, e = PHIVals.size(); i != e; 
++i59.6k
)
6662
59.6k
    PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
6663
28.5k
  Inst = PN;
6664
28.5k
  return AteExtraComma ? 
InstExtraComma77
:
InstNormal28.4k
;
6665
28.5k
}
6666
6667
/// ParseLandingPad
6668
///   ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
6669
/// Clause
6670
///   ::= 'catch' TypeAndValue
6671
///   ::= 'filter'
6672
///   ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
6673
1.21k
bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
6674
1.21k
  Type *Ty = nullptr; LocTy TyLoc;
6675
1.21k
6676
1.21k
  if (ParseType(Ty, TyLoc))
6677
0
    return true;
6678
1.21k
6679
1.21k
  std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, 0));
6680
1.21k
  LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
6681
1.21k
6682
1.80k
  while (Lex.getKind() == lltok::kw_catch || 
Lex.getKind() == lltok::kw_filter1.27k
){
6683
597
    LandingPadInst::ClauseType CT;
6684
597
    if (EatIfPresent(lltok::kw_catch))
6685
539
      CT = LandingPadInst::Catch;
6686
58
    else if (EatIfPresent(lltok::kw_filter))
6687
58
      CT = LandingPadInst::Filter;
6688
0
    else
6689
0
      return TokError("expected 'catch' or 'filter' clause type");
6690
597
6691
597
    Value *V;
6692
597
    LocTy VLoc;
6693
597
    if (ParseTypeAndValue(V, VLoc, PFS))
6694
0
      return true;
6695
597
6696
597
    // A 'catch' type expects a non-array constant. A filter clause expects an
6697
597
    // array constant.
6698
597
    if (CT == LandingPadInst::Catch) {
6699
539
      if (isa<ArrayType>(V->getType()))
6700
0
        Error(VLoc, "'catch' clause has an invalid type");
6701
539
    } else {
6702
58
      if (!isa<ArrayType>(V->getType()))
6703
1
        Error(VLoc, "'filter' clause has an invalid type");
6704
58
    }
6705
597
6706
597
    Constant *CV = dyn_cast<Constant>(V);
6707
597
    if (!CV)
6708
1
      return Error(VLoc, "clause argument must be a constant");
6709
596
    LP->addClause(CV);
6710
596
  }
6711
1.21k
6712
1.21k
  Inst = LP.release();
6713
1.21k
  return false;
6714
1.21k
}
6715
6716
/// ParseCall
6717
///   ::= 'call' OptionalFastMathFlags OptionalCallingConv
6718
///           OptionalAttrs Type Value ParameterList OptionalAttrs
6719
///   ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv
6720
///           OptionalAttrs Type Value ParameterList OptionalAttrs
6721
///   ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv
6722
///           OptionalAttrs Type Value ParameterList OptionalAttrs
6723
///   ::= 'notail' 'call'  OptionalFastMathFlags OptionalCallingConv
6724
///           OptionalAttrs Type Value ParameterList OptionalAttrs
6725
bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
6726
188k
                         CallInst::TailCallKind TCK) {
6727
188k
  AttrBuilder RetAttrs, FnAttrs;
6728
188k
  std::vector<unsigned> FwdRefAttrGrps;
6729
188k
  LocTy BuiltinLoc;
6730
188k
  unsigned CallAddrSpace;
6731
188k
  unsigned CC;
6732
188k
  Type *RetType = nullptr;
6733
188k
  LocTy RetTypeLoc;
6734
188k
  ValID CalleeID;
6735
188k
  SmallVector<ParamInfo, 16> ArgList;
6736
188k
  SmallVector<OperandBundleDef, 2> BundleList;
6737
188k
  LocTy CallLoc = Lex.getLoc();
6738
188k
6739
188k
  if (TCK != CallInst::TCK_None &&
6740
188k
      ParseToken(lltok::kw_call,
6741
38.5k
                 "expected 'tail call', 'musttail call', or 'notail call'"))
6742
0
    return true;
6743
188k
6744
188k
  FastMathFlags FMF = EatFastMathFlagsIfPresent();
6745
188k
6746
188k
  if (ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
6747
188k
      ParseOptionalProgramAddrSpace(CallAddrSpace) ||
6748
188k
      ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
6749
188k
      ParseValID(CalleeID) ||
6750
188k
      ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
6751
188k
                         PFS.getFunction().isVarArg()) ||
6752
188k
      
ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, BuiltinLoc)188k
||
6753
188k
      
ParseOptionalOperandBundles(BundleList, PFS)188k
)
6754
8
    return true;
6755
188k
6756
188k
  if (FMF.any() && 
!RetType->isFPOrFPVectorTy()1.72k
)
6757
0
    return Error(CallLoc, "fast-math-flags specified for call without "
6758
0
                          "floating-point scalar or vector return type");
6759
188k
6760
188k
  // If RetType is a non-function pointer type, then this is the short syntax
6761
188k
  // for the call, which means that RetType is just the return type.  Infer the
6762
188k
  // rest of the function argument types from the arguments that are present.
6763
188k
  FunctionType *Ty = dyn_cast<FunctionType>(RetType);
6764
188k
  if (!Ty) {
6765
183k
    // Pull out the types of all of the arguments...
6766
183k
    std::vector<Type*> ParamTypes;
6767
557k
    for (unsigned i = 0, e = ArgList.size(); i != e; 
++i373k
)
6768
373k
      ParamTypes.push_back(ArgList[i].V->getType());
6769
183k
6770
183k
    if (!FunctionType::isValidReturnType(RetType))
6771
0
      return Error(RetTypeLoc, "Invalid result type for LLVM function");
6772
183k
6773
183k
    Ty = FunctionType::get(RetType, ParamTypes, false);
6774
183k
  }
6775
188k
6776
188k
  CalleeID.FTy = Ty;
6777
188k
6778
188k
  // Look up the callee.
6779
188k
  Value *Callee;
6780
188k
  if (ConvertValIDToValue(PointerType::get(Ty, CallAddrSpace), CalleeID, Callee,
6781
188k
                          &PFS, /*IsCall=*/true))
6782
9
    return true;
6783
188k
6784
188k
  // Set up the Attribute for the function.
6785
188k
  SmallVector<AttributeSet, 8> Attrs;
6786
188k
6787
188k
  SmallVector<Value*, 8> Args;
6788
188k
6789
188k
  // Loop through FunctionType's arguments and ensure they are specified
6790
188k
  // correctly.  Also, gather any parameter attributes.
6791
188k
  FunctionType::param_iterator I = Ty->param_begin();
6792
188k
  FunctionType::param_iterator E = Ty->param_end();
6793
575k
  for (unsigned i = 0, e = ArgList.size(); i != e; 
++i386k
) {
6794
386k
    Type *ExpectedTy = nullptr;
6795
386k
    if (I != E) {
6796
378k
      ExpectedTy = *I++;
6797
378k
    } else 
if (7.60k
!Ty->isVarArg()7.60k
) {
6798
0
      return Error(ArgList[i].Loc, "too many arguments specified");
6799
0
    }
6800
386k
6801
386k
    if (ExpectedTy && 
ExpectedTy != ArgList[i].V->getType()378k
)
6802
0
      return Error(ArgList[i].Loc, "argument is not of expected type '" +
6803
0
                   getTypeString(ExpectedTy) + "'");
6804
386k
    Args.push_back(ArgList[i].V);
6805
386k
    Attrs.push_back(ArgList[i].Attrs);
6806
386k
  }
6807
188k
6808
188k
  if (I != E)
6809
0
    return Error(CallLoc, "not enough parameters specified for call");
6810
188k
6811
188k
  if (FnAttrs.hasAlignmentAttr())
6812
1
    return Error(CallLoc, "call instructions may not have an alignment");
6813
188k
6814
188k
  // Finish off the Attribute and check them
6815
188k
  AttributeList PAL =
6816
188k
      AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
6817
188k
                         AttributeSet::get(Context, RetAttrs), Attrs);
6818
188k
6819
188k
  CallInst *CI = CallInst::Create(Ty, Callee, Args, BundleList);
6820
188k
  CI->setTailCallKind(TCK);
6821
188k
  CI->setCallingConv(CC);
6822
188k
  if (FMF.any())
6823
1.72k
    CI->setFastMathFlags(FMF);
6824
188k
  CI->setAttributes(PAL);
6825
188k
  ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
6826
188k
  Inst = CI;
6827
188k
  return false;
6828
188k
}
6829
6830
//===----------------------------------------------------------------------===//
6831
// Memory Instructions.
6832
//===----------------------------------------------------------------------===//
6833
6834
/// ParseAlloc
6835
///   ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)?
6836
///       (',' 'align' i32)? (',', 'addrspace(n))?
6837
51.6k
int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
6838
51.6k
  Value *Size = nullptr;
6839
51.6k
  LocTy SizeLoc, TyLoc, ASLoc;
6840
51.6k
  unsigned Alignment = 0;
6841
51.6k
  unsigned AddrSpace = 0;
6842
51.6k
  Type *Ty = nullptr;
6843
51.6k
6844
51.6k
  bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
6845
51.6k
  bool IsSwiftError = EatIfPresent(lltok::kw_swifterror);
6846
51.6k
6847
51.6k
  if (ParseType(Ty, TyLoc)) 
return true0
;
6848
51.6k
6849
51.6k
  if (Ty->isFunctionTy() || 
!PointerType::isValidElementType(Ty)51.6k
)
6850
4
    return Error(TyLoc, "invalid type for alloca");
6851
51.6k
6852
51.6k
  bool AteExtraComma = false;
6853
51.6k
  if (EatIfPresent(lltok::comma)) {
6854
48.3k
    if (Lex.getKind() == lltok::kw_align) {
6855
46.6k
      if (ParseOptionalAlignment(Alignment))
6856
1
        return true;
6857
46.6k
      if (ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
6858
0
        return true;
6859
1.72k
    } else if (Lex.getKind() == lltok::kw_addrspace) {
6860
454
      ASLoc = Lex.getLoc();
6861
454
      if (ParseOptionalAddrSpace(AddrSpace))
6862
0
        return true;
6863
1.27k
    } else if (Lex.getKind() == lltok::MetadataVar) {
6864
0
      AteExtraComma = true;
6865
1.27k
    } else {
6866
1.27k
      if (ParseTypeAndValue(Size, SizeLoc, PFS))
6867
0
        return true;
6868
1.27k
      if (EatIfPresent(lltok::comma)) {
6869
508
        if (Lex.getKind() == lltok::kw_align) {
6870
490
          if (ParseOptionalAlignment(Alignment))
6871
0
            return true;
6872
490
          if (ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
6873
0
            return true;
6874
18
        } else if (Lex.getKind() == lltok::kw_addrspace) {
6875
14
          ASLoc = Lex.getLoc();
6876
14
          if (ParseOptionalAddrSpace(AddrSpace))
6877
0
            return true;
6878
4
        } else if (Lex.getKind() == lltok::MetadataVar) {
6879
4
          AteExtraComma = true;
6880
4
        }
6881
508
      }
6882
1.27k
    }
6883
48.3k
  }
6884
51.6k
6885
51.6k
  
if (51.6k
Size51.6k
&&
!Size->getType()->isIntegerTy()1.27k
)
6886
0
    return Error(SizeLoc, "element count must have integer type");
6887
51.6k
6888
51.6k
  AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, Alignment);
6889
51.6k
  AI->setUsedWithInAlloca(IsInAlloca);
6890
51.6k
  AI->setSwiftError(IsSwiftError);
6891
51.6k
  Inst = AI;
6892
51.6k
  return AteExtraComma ? 
InstExtraComma55
:
InstNormal51.5k
;
6893
51.6k
}
6894
6895
/// ParseLoad
6896
///   ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
6897
///   ::= 'load' 'atomic' 'volatile'? TypeAndValue
6898
///       'singlethread'? AtomicOrdering (',' 'align' i32)?
6899
193k
int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
6900
193k
  Value *Val; LocTy Loc;
6901
193k
  unsigned Alignment = 0;
6902
193k
  bool AteExtraComma = false;
6903
193k
  bool isAtomic = false;
6904
193k
  AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
6905
193k
  SyncScope::ID SSID = SyncScope::System;
6906
193k
6907
193k
  if (Lex.getKind() == lltok::kw_atomic) {
6908
1.82k
    isAtomic = true;
6909
1.82k
    Lex.Lex();
6910
1.82k
  }
6911
193k
6912
193k
  bool isVolatile = false;
6913
193k
  if (Lex.getKind() == lltok::kw_volatile) {
6914
11.4k
    isVolatile = true;
6915
11.4k
    Lex.Lex();
6916
11.4k
  }
6917
193k
6918
193k
  Type *Ty;
6919
193k
  LocTy ExplicitTypeLoc = Lex.getLoc();
6920
193k
  if (ParseType(Ty) ||
6921
193k
      ParseToken(lltok::comma, "expected comma after load's type") ||
6922
193k
      
ParseTypeAndValue(Val, Loc, PFS)193k
||
6923
193k
      
ParseScopeAndOrdering(isAtomic, SSID, Ordering)193k
||
6924
193k
      
ParseOptionalCommaAlign(Alignment, AteExtraComma)193k
)
6925
2
    return true;
6926
193k
6927
193k
  if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
6928
0
    return Error(Loc, "load operand must be a pointer to a first class type");
6929
193k
  if (isAtomic && 
!Alignment1.82k
)
6930
0
    return Error(Loc, "atomic load must have explicit non-zero alignment");
6931
193k
  if (Ordering == AtomicOrdering::Release ||
6932
193k
      Ordering == AtomicOrdering::AcquireRelease)
6933
0
    return Error(Loc, "atomic load cannot use Release ordering");
6934
193k
6935
193k
  if (Ty != cast<PointerType>(Val->getType())->getElementType())
6936
1
    return Error(ExplicitTypeLoc,
6937
1
                 "explicit pointee type doesn't match operand's pointee type");
6938
193k
6939
193k
  Inst = new LoadInst(Ty, Val, "", isVolatile, Alignment, Ordering, SSID);
6940
193k
  return AteExtraComma ? 
InstExtraComma6.09k
:
InstNormal187k
;
6941
193k
}
6942
6943
/// ParseStore
6944
6945
///   ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
6946
///   ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
6947
///       'singlethread'? AtomicOrdering (',' 'align' i32)?
6948
149k
int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
6949
149k
  Value *Val, *Ptr; LocTy Loc, PtrLoc;
6950
149k
  unsigned Alignment = 0;
6951
149k
  bool AteExtraComma = false;
6952
149k
  bool isAtomic = false;
6953
149k
  AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
6954
149k
  SyncScope::ID SSID = SyncScope::System;
6955
149k
6956
149k
  if (Lex.getKind() == lltok::kw_atomic) {
6957
1.68k
    isAtomic = true;
6958
1.68k
    Lex.Lex();
6959
1.68k
  }
6960
149k
6961
149k
  bool isVolatile = false;
6962
149k
  if (Lex.getKind() == lltok::kw_volatile) {
6963
12.7k
    isVolatile = true;
6964
12.7k
    Lex.Lex();
6965
12.7k
  }
6966
149k
6967
149k
  if (ParseTypeAndValue(Val, Loc, PFS) ||
6968
149k
      
ParseToken(lltok::comma, "expected ',' after store operand")149k
||
6969
149k
      
ParseTypeAndValue(Ptr, PtrLoc, PFS)149k
||
6970
149k
      
ParseScopeAndOrdering(isAtomic, SSID, Ordering)149k
||
6971
149k
      
ParseOptionalCommaAlign(Alignment, AteExtraComma)149k
)
6972
2
    return true;
6973
149k
6974
149k
  if (!Ptr->getType()->isPointerTy())
6975
0
    return Error(PtrLoc, "store operand must be a pointer");
6976
149k
  if (!Val->getType()->isFirstClassType())
6977
0
    return Error(Loc, "store operand must be a first class value");
6978
149k
  if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
6979
0
    return Error(Loc, "stored value and pointer type do not match");
6980
149k
  if (isAtomic && 
!Alignment1.68k
)
6981
0
    return Error(Loc, "atomic store must have explicit non-zero alignment");
6982
149k
  if (Ordering == AtomicOrdering::Acquire ||
6983
149k
      Ordering == AtomicOrdering::AcquireRelease)
6984
0
    return Error(Loc, "atomic store cannot use Acquire ordering");
6985
149k
6986
149k
  Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, SSID);
6987
149k
  return AteExtraComma ? 
InstExtraComma4.36k
:
InstNormal145k
;
6988
149k
}
6989
6990
/// ParseCmpXchg
6991
///   ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
6992
///       TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
6993
1.89k
int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
6994
1.89k
  Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
6995
1.89k
  bool AteExtraComma = false;
6996
1.89k
  AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic;
6997
1.89k
  AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
6998
1.89k
  SyncScope::ID SSID = SyncScope::System;
6999
1.89k
  bool isVolatile = false;
7000
1.89k
  bool isWeak = false;
7001
1.89k
7002
1.89k
  if (EatIfPresent(lltok::kw_weak))
7003
36
    isWeak = true;
7004
1.89k
7005
1.89k
  if (EatIfPresent(lltok::kw_volatile))
7006
763
    isVolatile = true;
7007
1.89k
7008
1.89k
  if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
7009
1.89k
      ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
7010
1.89k
      ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
7011
1.89k
      ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
7012
1.89k
      ParseTypeAndValue(New, NewLoc, PFS) ||
7013
1.89k
      ParseScopeAndOrdering(true /*Always atomic*/, SSID, SuccessOrdering) ||
7014
1.89k
      ParseOrdering(FailureOrdering))
7015
0
    return true;
7016
1.89k
7017
1.89k
  if (SuccessOrdering == AtomicOrdering::Unordered ||
7018
1.89k
      FailureOrdering == AtomicOrdering::Unordered)
7019
0
    return TokError("cmpxchg cannot be unordered");
7020
1.89k
  if (isStrongerThan(FailureOrdering, SuccessOrdering))
7021
0
    return TokError("cmpxchg failure argument shall be no stronger than the "
7022
0
                    "success argument");
7023
1.89k
  if (FailureOrdering == AtomicOrdering::Release ||
7024
1.89k
      FailureOrdering == AtomicOrdering::AcquireRelease)
7025
0
    return TokError(
7026
0
        "cmpxchg failure ordering cannot include release semantics");
7027
1.89k
  if (!Ptr->getType()->isPointerTy())
7028
0
    return Error(PtrLoc, "cmpxchg operand must be a pointer");
7029
1.89k
  if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
7030
0
    return Error(CmpLoc, "compare value and pointer type do not match");
7031
1.89k
  if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
7032
0
    return Error(NewLoc, "new value and pointer type do not match");
7033
1.89k
  if (!New->getType()->isFirstClassType())
7034
0
    return Error(NewLoc, "cmpxchg operand must be a first class value");
7035
1.89k
  AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
7036
1.89k
      Ptr, Cmp, New, SuccessOrdering, FailureOrdering, SSID);
7037
1.89k
  CXI->setVolatile(isVolatile);
7038
1.89k
  CXI->setWeak(isWeak);
7039
1.89k
  Inst = CXI;
7040
1.89k
  return AteExtraComma ? 
InstExtraComma0
: InstNormal;
7041
1.89k
}
7042
7043
/// ParseAtomicRMW
7044
///   ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
7045
///       'singlethread'? AtomicOrdering
7046
7.93k
int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
7047
7.93k
  Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
7048
7.93k
  bool AteExtraComma = false;
7049
7.93k
  AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
7050
7.93k
  SyncScope::ID SSID = SyncScope::System;
7051
7.93k
  bool isVolatile = false;
7052
7.93k
  bool IsFP = false;
7053
7.93k
  AtomicRMWInst::BinOp Operation;
7054
7.93k
7055
7.93k
  if (EatIfPresent(lltok::kw_volatile))
7056
1.34k
    isVolatile = true;
7057
7.93k
7058
7.93k
  switch (Lex.getKind()) {
7059
7.93k
  
default: return TokError("expected binary operation in atomicrmw")0
;
7060
7.93k
  
case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break946
;
7061
7.93k
  
case lltok::kw_add: Operation = AtomicRMWInst::Add; break1.17k
;
7062
7.93k
  
case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break929
;
7063
7.93k
  
case lltok::kw_and: Operation = AtomicRMWInst::And; break766
;
7064
7.93k
  
case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break409
;
7065
7.93k
  
case lltok::kw_or: Operation = AtomicRMWInst::Or; break760
;
7066
7.93k
  
case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break716
;
7067
7.93k
  
case lltok::kw_max: Operation = AtomicRMWInst::Max; break513
;
7068
7.93k
  
case lltok::kw_min: Operation = AtomicRMWInst::Min; break535
;
7069
7.93k
  
case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break522
;
7070
7.93k
  
case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break523
;
7071
7.93k
  case lltok::kw_fadd:
7072
95
    Operation = AtomicRMWInst::FAdd;
7073
95
    IsFP = true;
7074
95
    break;
7075
7.93k
  case lltok::kw_fsub:
7076
50
    Operation = AtomicRMWInst::FSub;
7077
50
    IsFP = true;
7078
50
    break;
7079
7.93k
  }
7080
7.93k
  Lex.Lex();  // Eat the operation.
7081
7.93k
7082
7.93k
  if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
7083
7.93k
      ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
7084
7.93k
      ParseTypeAndValue(Val, ValLoc, PFS) ||
7085
7.93k
      ParseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
7086
0
    return true;
7087
7.93k
7088
7.93k
  if (Ordering == AtomicOrdering::Unordered)
7089
0
    return TokError("atomicrmw cannot be unordered");
7090
7.93k
  if (!Ptr->getType()->isPointerTy())
7091
0
    return Error(PtrLoc, "atomicrmw operand must be a pointer");
7092
7.93k
  if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
7093
0
    return Error(ValLoc, "atomicrmw value and pointer type do not match");
7094
7.93k
7095
7.93k
  if (Operation == AtomicRMWInst::Xchg) {
7096
946
    if (!Val->getType()->isIntegerTy() &&
7097
946
        
!Val->getType()->isFloatingPointTy()35
) {
7098
1
      return Error(ValLoc, "atomicrmw " +
7099
1
                   AtomicRMWInst::getOperationName(Operation) +
7100
1
                   " operand must be an integer or floating point type");
7101
1
    }
7102
6.98k
  } else if (IsFP) {
7103
145
    if (!Val->getType()->isFloatingPointTy()) {
7104
2
      return Error(ValLoc, "atomicrmw " +
7105
2
                   AtomicRMWInst::getOperationName(Operation) +
7106
2
                   " operand must be a floating point type");
7107
2
    }
7108
6.84k
  } else {
7109
6.84k
    if (!Val->getType()->isIntegerTy()) {
7110
1
      return Error(ValLoc, "atomicrmw " +
7111
1
                   AtomicRMWInst::getOperationName(Operation) +
7112
1
                   " operand must be an integer");
7113
1
    }
7114
7.93k
  }
7115
7.93k
7116
7.93k
  unsigned Size = Val->getType()->getPrimitiveSizeInBits();
7117
7.93k
  if (Size < 8 || (Size & (Size - 1)))
7118
0
    return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
7119
0
                         " integer");
7120
7.93k
7121
7.93k
  AtomicRMWInst *RMWI =
7122
7.93k
    new AtomicRMWInst(Operation, Ptr, Val, Ordering, SSID);
7123
7.93k
  RMWI->setVolatile(isVolatile);
7124
7.93k
  Inst = RMWI;
7125
7.93k
  return AteExtraComma ? 
InstExtraComma0
: InstNormal;
7126
7.93k
}
7127
7128
/// ParseFence
7129
///   ::= 'fence' 'singlethread'? AtomicOrdering
7130
1.35k
int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
7131
1.35k
  AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
7132
1.35k
  SyncScope::ID SSID = SyncScope::System;
7133
1.35k
  if (ParseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
7134
0
    return true;
7135
1.35k
7136
1.35k
  if (Ordering == AtomicOrdering::Unordered)
7137
0
    return TokError("fence cannot be unordered");
7138
1.35k
  if (Ordering == AtomicOrdering::Monotonic)
7139
0
    return TokError("fence cannot be monotonic");
7140
1.35k
7141
1.35k
  Inst = new FenceInst(Context, Ordering, SSID);
7142
1.35k
  return InstNormal;
7143
1.35k
}
7144
7145
/// ParseGetElementPtr
7146
///   ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
7147
99.9k
int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
7148
99.9k
  Value *Ptr = nullptr;
7149
99.9k
  Value *Val = nullptr;
7150
99.9k
  LocTy Loc, EltLoc;
7151
99.9k
7152
99.9k
  bool InBounds = EatIfPresent(lltok::kw_inbounds);
7153
99.9k
7154
99.9k
  Type *Ty = nullptr;
7155
99.9k
  LocTy ExplicitTypeLoc = Lex.getLoc();
7156
99.9k
  if (ParseType(Ty) ||
7157
99.9k
      ParseToken(lltok::comma, "expected comma after getelementptr's type") ||
7158
99.9k
      
ParseTypeAndValue(Ptr, Loc, PFS)99.9k
)
7159
2
    return true;
7160
99.9k
7161
99.9k
  Type *BaseType = Ptr->getType();
7162
99.9k
  PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
7163
99.9k
  if (!BasePointerType)
7164
1
    return Error(Loc, "base of getelementptr must be a pointer");
7165
99.9k
7166
99.9k
  if (Ty != BasePointerType->getElementType())
7167
1
    return Error(ExplicitTypeLoc,
7168
1
                 "explicit pointee type doesn't match operand's pointee type");
7169
99.9k
7170
99.9k
  SmallVector<Value*, 16> Indices;
7171
99.9k
  bool AteExtraComma = false;
7172
99.9k
  // GEP returns a vector of pointers if at least one of parameters is a vector.
7173
99.9k
  // All vector parameters should have the same vector width.
7174
99.9k
  unsigned GEPWidth = BaseType->isVectorTy() ?
7175
99.7k
    
BaseType->getVectorNumElements()276
: 0;
7176
99.9k
7177
227k
  while (EatIfPresent(lltok::comma)) {
7178
128k
    if (Lex.getKind() == lltok::MetadataVar) {
7179
1.07k
      AteExtraComma = true;
7180
1.07k
      break;
7181
1.07k
    }
7182
127k
    if (ParseTypeAndValue(Val, EltLoc, PFS)) 
return true0
;
7183
127k
    if (!Val->getType()->isIntOrIntVectorTy())
7184
0
      return Error(EltLoc, "getelementptr index must be an integer");
7185
127k
7186
127k
    if (Val->getType()->isVectorTy()) {
7187
613
      unsigned ValNumEl = Val->getType()->getVectorNumElements();
7188
613
      if (GEPWidth && 
GEPWidth != ValNumEl323
)
7189
1
        return Error(EltLoc,
7190
1
          "getelementptr vector index has a wrong number of elements");
7191
612
      GEPWidth = ValNumEl;
7192
612
    }
7193
127k
    Indices.push_back(Val);
7194
127k
  }
7195
99.9k
7196
99.9k
  SmallPtrSet<Type*, 4> Visited;
7197
99.9k
  if (!Indices.empty() && 
!Ty->isSized(&Visited)99.8k
)
7198
1
    return Error(Loc, "base element of getelementptr must be sized");
7199
99.9k
7200
99.9k
  if (!GetElementPtrInst::getIndexedType(Ty, Indices))
7201
3
    return Error(Loc, "invalid getelementptr indices");
7202
99.9k
  Inst = GetElementPtrInst::Create(Ty, Ptr, Indices);
7203
99.9k
  if (InBounds)
7204
74.5k
    cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
7205
99.9k
  return AteExtraComma ? 
InstExtraComma1.07k
:
InstNormal98.8k
;
7206
99.9k
}
7207
7208
/// ParseExtractValue
7209
///   ::= 'extractvalue' TypeAndValue (',' uint32)+
7210
13.9k
int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
7211
13.9k
  Value *Val; LocTy Loc;
7212
13.9k
  SmallVector<unsigned, 4> Indices;
7213
13.9k
  bool AteExtraComma;
7214
13.9k
  if (ParseTypeAndValue(Val, Loc, PFS) ||
7215
13.9k
      ParseIndexList(Indices, AteExtraComma))
7216
1
    return true;
7217
13.9k
7218
13.9k
  if (!Val->getType()->isAggregateType())
7219
0
    return Error(Loc, "extractvalue operand must be aggregate type");
7220
13.9k
7221
13.9k
  if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
7222
1
    return Error(Loc, "invalid indices for extractvalue");
7223
13.9k
  Inst = ExtractValueInst::Create(Val, Indices);
7224
13.9k
  return AteExtraComma ? 
InstExtraComma54
:
InstNormal13.9k
;
7225
13.9k
}
7226
7227
/// ParseInsertValue
7228
///   ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
7229
1.73k
int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
7230
1.73k
  Value *Val0, *Val1; LocTy Loc0, Loc1;
7231
1.73k
  SmallVector<unsigned, 4> Indices;
7232
1.73k
  bool AteExtraComma;
7233
1.73k
  if (ParseTypeAndValue(Val0, Loc0, PFS) ||
7234
1.73k
      ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
7235
1.73k
      ParseTypeAndValue(Val1, Loc1, PFS) ||
7236
1.73k
      ParseIndexList(Indices, AteExtraComma))
7237
0
    return true;
7238
1.73k
7239
1.73k
  if (!Val0->getType()->isAggregateType())
7240
0
    return Error(Loc0, "insertvalue operand must be aggregate type");
7241
1.73k
7242
1.73k
  Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
7243
1.73k
  if (!IndexedType)
7244
1
    return Error(Loc0, "invalid indices for insertvalue");
7245
1.73k
  if (IndexedType != Val1->getType())
7246
1
    return Error(Loc1, "insertvalue operand and field disagree in type: '" +
7247
1
                           getTypeString(Val1->getType()) + "' instead of '" +
7248
1
                           getTypeString(IndexedType) + "'");
7249
1.73k
  Inst = InsertValueInst::Create(Val0, Val1, Indices);
7250
1.73k
  return AteExtraComma ? 
InstExtraComma21
:
InstNormal1.71k
;
7251
1.73k
}
7252
7253
//===----------------------------------------------------------------------===//
7254
// Embedded metadata.
7255
//===----------------------------------------------------------------------===//
7256
7257
/// ParseMDNodeVector
7258
///   ::= { Element (',' Element)* }
7259
/// Element
7260
///   ::= 'null' | TypeAndValue
7261
24.8k
bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
7262
24.8k
  if (ParseToken(lltok::lbrace, "expected '{' here"))
7263
1
    return true;
7264
24.8k
7265
24.8k
  // Check for an empty list.
7266
24.8k
  if (EatIfPresent(lltok::rbrace))
7267
2.43k
    return false;
7268
22.4k
7269
68.1k
  
do 22.4k
{
7270
68.1k
    // Null is a special case since it is typeless.
7271
68.1k
    if (EatIfPresent(lltok::kw_null)) {
7272
1.11k
      Elts.push_back(nullptr);
7273
1.11k
      continue;
7274
1.11k
    }
7275
67.0k
7276
67.0k
    Metadata *MD;
7277
67.0k
    if (ParseMetadata(MD, nullptr))
7278
7
      return true;
7279
67.0k
    Elts.push_back(MD);
7280
68.1k
  } while (EatIfPresent(lltok::comma));
7281
22.4k
7282
22.4k
  
return ParseToken(lltok::rbrace, "expected end of metadata node")22.4k
;
7283
22.4k
}
7284
7285
//===----------------------------------------------------------------------===//
7286
// Use-list order directives.
7287
//===----------------------------------------------------------------------===//
7288
bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
7289
897
                                SMLoc Loc) {
7290
897
  if (V->use_empty())
7291
4
    return Error(Loc, "value has no uses");
7292
893
7293
893
  unsigned NumUses = 0;
7294
893
  SmallDenseMap<const Use *, unsigned, 16> Order;
7295
3.28k
  for (const Use &U : V->uses()) {
7296
3.28k
    if (++NumUses > Indexes.size())
7297
1
      break;
7298
3.28k
    Order[&U] = Indexes[NumUses - 1];
7299
3.28k
  }
7300
893
  if (NumUses < 2)
7301
1
    return Error(Loc, "value only has one use");
7302
892
  if (Order.size() != Indexes.size() || 
NumUses > Indexes.size()890
)
7303
3
    return Error(Loc,
7304
3
                 "wrong number of indexes, expected " + Twine(V->getNumUses()));
7305
889
7306
4.49k
  
V->sortUseList([&](const Use &L, const Use &R) 889
{
7307
4.49k
    return Order.lookup(&L) < Order.lookup(&R);
7308
4.49k
  });
7309
889
  return false;
7310
889
}
7311
7312
/// ParseUseListOrderIndexes
7313
///   ::= '{' uint32 (',' uint32)+ '}'
7314
906
bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
7315
906
  SMLoc Loc = Lex.getLoc();
7316
906
  if (ParseToken(lltok::lbrace, "expected '{' here"))
7317
0
    return true;
7318
906
  if (Lex.getKind() == lltok::rbrace)
7319
0
    return Lex.Error("expected non-empty list of uselistorder indexes");
7320
906
7321
906
  // Use Offset, Max, and IsOrdered to check consistency of indexes.  The
7322
906
  // indexes should be distinct numbers in the range [0, size-1], and should
7323
906
  // not be in order.
7324
906
  unsigned Offset = 0;
7325
906
  unsigned Max = 0;
7326
906
  bool IsOrdered = true;
7327
906
  assert(Indexes.empty() && "Expected empty order vector");
7328
3.31k
  do {
7329
3.31k
    unsigned Index;
7330
3.31k
    if (ParseUInt32(Index))
7331
0
      return true;
7332
3.31k
7333
3.31k
    // Update consistency checks.
7334
3.31k
    Offset += Index - Indexes.size();
7335
3.31k
    Max = std::max(Max, Index);
7336
3.31k
    IsOrdered &= Index == Indexes.size();
7337
3.31k
7338
3.31k
    Indexes.push_back(Index);
7339
3.31k
  } while (EatIfPresent(lltok::comma));
7340
906
7341
906
  if (ParseToken(lltok::rbrace, "expected '}' here"))
7342
0
    return true;
7343
906
7344
906
  if (Indexes.size() < 2)
7345
0
    return Error(Loc, "expected >= 2 uselistorder indexes");
7346
906
  if (Offset != 0 || 
Max >= Indexes.size()904
)
7347
2
    return Error(Loc, "expected distinct uselistorder indexes in range [0, size)");
7348
904
  if (IsOrdered)
7349
1
    return Error(Loc, "expected uselistorder indexes to change the order");
7350
903
7351
903
  return false;
7352
903
}
7353
7354
/// ParseUseListOrder
7355
///   ::= 'uselistorder' Type Value ',' UseListOrderIndexes
7356
896
bool LLParser::ParseUseListOrder(PerFunctionState *PFS) {
7357
896
  SMLoc Loc = Lex.getLoc();
7358
896
  if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
7359
1
    return true;
7360
895
7361
895
  Value *V;
7362
895
  SmallVector<unsigned, 16> Indexes;
7363
895
  if (ParseTypeAndValue(V, PFS) ||
7364
895
      
ParseToken(lltok::comma, "expected comma in uselistorder directive")894
||
7365
895
      
ParseUseListOrderIndexes(Indexes)894
)
7366
4
    return true;
7367
891
7368
891
  return sortUseListOrder(V, Indexes, Loc);
7369
891
}
7370
7371
/// ParseUseListOrderBB
7372
///   ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
7373
12
bool LLParser::ParseUseListOrderBB() {
7374
12
  assert(Lex.getKind() == lltok::kw_uselistorder_bb);
7375
12
  SMLoc Loc = Lex.getLoc();
7376
12
  Lex.Lex();
7377
12
7378
12
  ValID Fn, Label;
7379
12
  SmallVector<unsigned, 16> Indexes;
7380
12
  if (ParseValID(Fn) ||
7381
12
      ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
7382
12
      ParseValID(Label) ||
7383
12
      ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
7384
12
      ParseUseListOrderIndexes(Indexes))
7385
0
    return true;
7386
12
7387
12
  // Check the function.
7388
12
  GlobalValue *GV;
7389
12
  if (Fn.Kind == ValID::t_GlobalName)
7390
12
    GV = M->getNamedValue(Fn.StrVal);
7391
0
  else if (Fn.Kind == ValID::t_GlobalID)
7392
0
    GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr;
7393
0
  else
7394
0
    return Error(Fn.Loc, "expected function name in uselistorder_bb");
7395
12
  if (!GV)
7396
1
    return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb");
7397
11
  auto *F = dyn_cast<Function>(GV);
7398
11
  if (!F)
7399
1
    return Error(Fn.Loc, "expected function name in uselistorder_bb");
7400
10
  if (F->isDeclaration())
7401
1
    return Error(Fn.Loc, "invalid declaration in uselistorder_bb");
7402
9
7403
9
  // Check the basic block.
7404
9
  if (Label.Kind == ValID::t_LocalID)
7405
1
    return Error(Label.Loc, "invalid numeric label in uselistorder_bb");
7406
8
  if (Label.Kind != ValID::t_LocalName)
7407
0
    return Error(Label.Loc, "expected basic block name in uselistorder_bb");
7408
8
  Value *V = F->getValueSymbolTable()->lookup(Label.StrVal);
7409
8
  if (!V)
7410
1
    return Error(Label.Loc, "invalid basic block in uselistorder_bb");
7411
7
  if (!isa<BasicBlock>(V))
7412
1
    return Error(Label.Loc, "expected basic block in uselistorder_bb");
7413
6
7414
6
  return sortUseListOrder(V, Indexes, Loc);
7415
6
}
7416
7417
/// ModuleEntry
7418
///   ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')'
7419
/// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')'
7420
24
bool LLParser::ParseModuleEntry(unsigned ID) {
7421
24
  assert(Lex.getKind() == lltok::kw_module);
7422
24
  Lex.Lex();
7423
24
7424
24
  std::string Path;
7425
24
  if (ParseToken(lltok::colon, "expected ':' here") ||
7426
24
      ParseToken(lltok::lparen, "expected '(' here") ||
7427
24
      ParseToken(lltok::kw_path, "expected 'path' here") ||
7428
24
      ParseToken(lltok::colon, "expected ':' here") ||
7429
24
      ParseStringConstant(Path) ||
7430
24
      ParseToken(lltok::comma, "expected ',' here") ||
7431
24
      ParseToken(lltok::kw_hash, "expected 'hash' here") ||
7432
24
      ParseToken(lltok::colon, "expected ':' here") ||
7433
24
      ParseToken(lltok::lparen, "expected '(' here"))
7434
0
    return true;
7435
24
7436
24
  ModuleHash Hash;
7437
24
  if (ParseUInt32(Hash[0]) || ParseToken(lltok::comma, "expected ',' here") ||
7438
24
      ParseUInt32(Hash[1]) || ParseToken(lltok::comma, "expected ',' here") ||
7439
24
      ParseUInt32(Hash[2]) || ParseToken(lltok::comma, "expected ',' here") ||
7440
24
      ParseUInt32(Hash[3]) || ParseToken(lltok::comma, "expected ',' here") ||
7441
24
      ParseUInt32(Hash[4]))
7442
0
    return true;
7443
24
7444
24
  if (ParseToken(lltok::rparen, "expected ')' here") ||
7445
24
      ParseToken(lltok::rparen, "expected ')' here"))
7446
0
    return true;
7447
24
7448
24
  auto ModuleEntry = Index->addModule(Path, ID, Hash);
7449
24
  ModuleIdMap[ID] = ModuleEntry->first();
7450
24
7451
24
  return false;
7452
24
}
7453
7454
/// TypeIdEntry
7455
///   ::= 'typeid' ':' '(' 'name' ':' STRINGCONSTANT ',' TypeIdSummary ')'
7456
7
bool LLParser::ParseTypeIdEntry(unsigned ID) {
7457
7
  assert(Lex.getKind() == lltok::kw_typeid);
7458
7
  Lex.Lex();
7459
7
7460
7
  std::string Name;
7461
7
  if (ParseToken(lltok::colon, "expected ':' here") ||
7462
7
      ParseToken(lltok::lparen, "expected '(' here") ||
7463
7
      ParseToken(lltok::kw_name, "expected 'name' here") ||
7464
7
      ParseToken(lltok::colon, "expected ':' here") ||
7465
7
      ParseStringConstant(Name))
7466
0
    return true;
7467
7
7468
7
  TypeIdSummary &TIS = Index->getOrInsertTypeIdSummary(Name);
7469
7
  if (ParseToken(lltok::comma, "expected ',' here") ||
7470
7
      ParseTypeIdSummary(TIS) || ParseToken(lltok::rparen, "expected ')' here"))
7471
0
    return true;
7472
7
7473
7
  // Check if this ID was forward referenced, and if so, update the
7474
7
  // corresponding GUIDs.
7475
7
  auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
7476
7
  if (FwdRefTIDs != ForwardRefTypeIds.end()) {
7477
11
    for (auto TIDRef : FwdRefTIDs->second) {
7478
11
      assert(!*TIDRef.first &&
7479
11
             "Forward referenced type id GUID expected to be 0");
7480
11
      *TIDRef.first = GlobalValue::getGUID(Name);
7481
11
    }
7482
7
    ForwardRefTypeIds.erase(FwdRefTIDs);
7483
7
  }
7484
7
7485
7
  return false;
7486
7
}
7487
7488
/// TypeIdSummary
7489
///   ::= 'summary' ':' '(' TypeTestResolution [',' OptionalWpdResolutions]? ')'
7490
7
bool LLParser::ParseTypeIdSummary(TypeIdSummary &TIS) {
7491
7
  if (ParseToken(lltok::kw_summary, "expected 'summary' here") ||
7492
7
      ParseToken(lltok::colon, "expected ':' here") ||
7493
7
      ParseToken(lltok::lparen, "expected '(' here") ||
7494
7
      ParseTypeTestResolution(TIS.TTRes))
7495
0
    return true;
7496
7
7497
7
  if (EatIfPresent(lltok::comma)) {
7498
2
    // Expect optional wpdResolutions field
7499
2
    if (ParseOptionalWpdResolutions(TIS.WPDRes))
7500
0
      return true;
7501
7
  }
7502
7
7503
7
  if (ParseToken(lltok::rparen, "expected ')' here"))
7504
0
    return true;
7505
7
7506
7
  return false;
7507
7
}
7508
7509
static ValueInfo EmptyVI =
7510
    ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
7511
7512
/// TypeIdCompatibleVtableEntry
7513
///   ::= 'typeidCompatibleVTable' ':' '(' 'name' ':' STRINGCONSTANT ','
7514
///   TypeIdCompatibleVtableInfo
7515
///   ')'
7516
4
bool LLParser::ParseTypeIdCompatibleVtableEntry(unsigned ID) {
7517
4
  assert(Lex.getKind() == lltok::kw_typeidCompatibleVTable);
7518
4
  Lex.Lex();
7519
4
7520
4
  std::string Name;
7521
4
  if (ParseToken(lltok::colon, "expected ':' here") ||
7522
4
      ParseToken(lltok::lparen, "expected '(' here") ||
7523
4
      ParseToken(lltok::kw_name, "expected 'name' here") ||
7524
4
      ParseToken(lltok::colon, "expected ':' here") ||
7525
4
      ParseStringConstant(Name))
7526
0
    return true;
7527
4
7528
4
  TypeIdCompatibleVtableInfo &TI =
7529
4
      Index->getOrInsertTypeIdCompatibleVtableSummary(Name);
7530
4
  if (ParseToken(lltok::comma, "expected ',' here") ||
7531
4
      ParseToken(lltok::kw_summary, "expected 'summary' here") ||
7532
4
      ParseToken(lltok::colon, "expected ':' here") ||
7533
4
      ParseToken(lltok::lparen, "expected '(' here"))
7534
0
    return true;
7535
4
7536
4
  IdToIndexMapType IdToIndexMap;
7537
4
  // Parse each call edge
7538
5
  do {
7539
5
    uint64_t Offset;
7540
5
    if (ParseToken(lltok::lparen, "expected '(' here") ||
7541
5
        ParseToken(lltok::kw_offset, "expected 'offset' here") ||
7542
5
        ParseToken(lltok::colon, "expected ':' here") || ParseUInt64(Offset) ||
7543
5
        ParseToken(lltok::comma, "expected ',' here"))
7544
0
      return true;
7545
5
7546
5
    LocTy Loc = Lex.getLoc();
7547
5
    unsigned GVId;
7548
5
    ValueInfo VI;
7549
5
    if (ParseGVReference(VI, GVId))
7550
0
      return true;
7551
5
7552
5
    // Keep track of the TypeIdCompatibleVtableInfo array index needing a
7553
5
    // forward reference. We will save the location of the ValueInfo needing an
7554
5
    // update, but can only do so once the std::vector is finalized.
7555
5
    if (VI == EmptyVI)
7556
0
      IdToIndexMap[GVId].push_back(std::make_pair(TI.size(), Loc));
7557
5
    TI.push_back({Offset, VI});
7558
5
7559
5
    if (ParseToken(lltok::rparen, "expected ')' in call"))
7560
0
      return true;
7561
5
  } while (EatIfPresent(lltok::comma));
7562
4
7563
4
  // Now that the TI vector is finalized, it is safe to save the locations
7564
4
  // of any forward GV references that need updating later.
7565
4
  for (auto I : IdToIndexMap) {
7566
0
    for (auto P : I.second) {
7567
0
      assert(TI[P.first].VTableVI == EmptyVI &&
7568
0
             "Forward referenced ValueInfo expected to be empty");
7569
0
      auto FwdRef = ForwardRefValueInfos.insert(std::make_pair(
7570
0
          I.first, std::vector<std::pair<ValueInfo *, LocTy>>()));
7571
0
      FwdRef.first->second.push_back(
7572
0
          std::make_pair(&TI[P.first].VTableVI, P.second));
7573
0
    }
7574
0
  }
7575
4
7576
4
  if (ParseToken(lltok::rparen, "expected ')' here") ||
7577
4
      ParseToken(lltok::rparen, "expected ')' here"))
7578
0
    return true;
7579
4
7580
4
  // Check if this ID was forward referenced, and if so, update the
7581
4
  // corresponding GUIDs.
7582
4
  auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
7583
4
  if (FwdRefTIDs != ForwardRefTypeIds.end()) {
7584
0
    for (auto TIDRef : FwdRefTIDs->second) {
7585
0
      assert(!*TIDRef.first &&
7586
0
             "Forward referenced type id GUID expected to be 0");
7587
0
      *TIDRef.first = GlobalValue::getGUID(Name);
7588
0
    }
7589
0
    ForwardRefTypeIds.erase(FwdRefTIDs);
7590
0
  }
7591
4
7592
4
  return false;
7593
4
}
7594
7595
/// TypeTestResolution
7596
///   ::= 'typeTestRes' ':' '(' 'kind' ':'
7597
///         ( 'unsat' | 'byteArray' | 'inline' | 'single' | 'allOnes' ) ','
7598
///         'sizeM1BitWidth' ':' SizeM1BitWidth [',' 'alignLog2' ':' UInt64]?
7599
///         [',' 'sizeM1' ':' UInt64]? [',' 'bitMask' ':' UInt8]?
7600
///         [',' 'inlinesBits' ':' UInt64]? ')'
7601
7
bool LLParser::ParseTypeTestResolution(TypeTestResolution &TTRes) {
7602
7
  if (ParseToken(lltok::kw_typeTestRes, "expected 'typeTestRes' here") ||
7603
7
      ParseToken(lltok::colon, "expected ':' here") ||
7604
7
      ParseToken(lltok::lparen, "expected '(' here") ||
7605
7
      ParseToken(lltok::kw_kind, "expected 'kind' here") ||
7606
7
      ParseToken(lltok::colon, "expected ':' here"))
7607
0
    return true;
7608
7
7609
7
  switch (Lex.getKind()) {
7610
7
  case lltok::kw_unsat:
7611
1
    TTRes.TheKind = TypeTestResolution::Unsat;
7612
1
    break;
7613
7
  case lltok::kw_byteArray:
7614
1
    TTRes.TheKind = TypeTestResolution::ByteArray;
7615
1
    break;
7616
7
  case lltok::kw_inline:
7617
1
    TTRes.TheKind = TypeTestResolution::Inline;
7618
1
    break;
7619
7
  case lltok::kw_single:
7620
2
    TTRes.TheKind = TypeTestResolution::Single;
7621
2
    break;
7622
7
  case lltok::kw_allOnes:
7623
2
    TTRes.TheKind = TypeTestResolution::AllOnes;
7624
2
    break;
7625
7
  default:
7626
0
    return Error(Lex.getLoc(), "unexpected TypeTestResolution kind");
7627
7
  }
7628
7
  Lex.Lex();
7629
7
7630
7
  if (ParseToken(lltok::comma, "expected ',' here") ||
7631
7
      ParseToken(lltok::kw_sizeM1BitWidth, "expected 'sizeM1BitWidth' here") ||
7632
7
      ParseToken(lltok::colon, "expected ':' here") ||
7633
7
      ParseUInt32(TTRes.SizeM1BitWidth))
7634
0
    return true;
7635
7
7636
7
  // Parse optional fields
7637
11
  
while (7
EatIfPresent(lltok::comma)) {
7638
4
    switch (Lex.getKind()) {
7639
4
    case lltok::kw_alignLog2:
7640
1
      Lex.Lex();
7641
1
      if (ParseToken(lltok::colon, "expected ':'") ||
7642
1
          ParseUInt64(TTRes.AlignLog2))
7643
0
        return true;
7644
1
      break;
7645
1
    case lltok::kw_sizeM1:
7646
1
      Lex.Lex();
7647
1
      if (ParseToken(lltok::colon, "expected ':'") || ParseUInt64(TTRes.SizeM1))
7648
0
        return true;
7649
1
      break;
7650
1
    case lltok::kw_bitMask: {
7651
1
      unsigned Val;
7652
1
      Lex.Lex();
7653
1
      if (ParseToken(lltok::colon, "expected ':'") || ParseUInt32(Val))
7654
0
        return true;
7655
1
      assert(Val <= 0xff);
7656
1
      TTRes.BitMask = (uint8_t)Val;
7657
1
      break;
7658
1
    }
7659
1
    case lltok::kw_inlineBits:
7660
1
      Lex.Lex();
7661
1
      if (ParseToken(lltok::colon, "expected ':'") ||
7662
1
          ParseUInt64(TTRes.InlineBits))
7663
0
        return true;
7664
1
      break;
7665
1
    default:
7666
0
      return Error(Lex.getLoc(), "expected optional TypeTestResolution field");
7667
4
    }
7668
4
  }
7669
7
7670
7
  if (ParseToken(lltok::rparen, "expected ')' here"))
7671
0
    return true;
7672
7
7673
7
  return false;
7674
7
}
7675
7676
/// OptionalWpdResolutions
7677
///   ::= 'wpsResolutions' ':' '(' WpdResolution [',' WpdResolution]* ')'
7678
/// WpdResolution ::= '(' 'offset' ':' UInt64 ',' WpdRes ')'
7679
bool LLParser::ParseOptionalWpdResolutions(
7680
2
    std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap) {
7681
2
  if (ParseToken(lltok::kw_wpdResolutions, "expected 'wpdResolutions' here") ||
7682
2
      ParseToken(lltok::colon, "expected ':' here") ||
7683
2
      ParseToken(lltok::lparen, "expected '(' here"))
7684
0
    return true;
7685
2
7686
5
  
do 2
{
7687
5
    uint64_t Offset;
7688
5
    WholeProgramDevirtResolution WPDRes;
7689
5
    if (ParseToken(lltok::lparen, "expected '(' here") ||
7690
5
        ParseToken(lltok::kw_offset, "expected 'offset' here") ||
7691
5
        ParseToken(lltok::colon, "expected ':' here") || ParseUInt64(Offset) ||
7692
5
        ParseToken(lltok::comma, "expected ',' here") || ParseWpdRes(WPDRes) ||
7693
5
        ParseToken(lltok::rparen, "expected ')' here"))
7694
0
      return true;
7695
5
    WPDResMap[Offset] = WPDRes;
7696
5
  } while (EatIfPresent(lltok::comma));
7697
2
7698
2
  if (ParseToken(lltok::rparen, "expected ')' here"))
7699
0
    return true;
7700
2
7701
2
  return false;
7702
2
}
7703
7704
/// WpdRes
7705
///   ::= 'wpdRes' ':' '(' 'kind' ':' 'indir'
7706
///         [',' OptionalResByArg]? ')'
7707
///   ::= 'wpdRes' ':' '(' 'kind' ':' 'singleImpl'
7708
///         ',' 'singleImplName' ':' STRINGCONSTANT ','
7709
///         [',' OptionalResByArg]? ')'
7710
///   ::= 'wpdRes' ':' '(' 'kind' ':' 'branchFunnel'
7711
///         [',' OptionalResByArg]? ')'
7712
5
bool LLParser::ParseWpdRes(WholeProgramDevirtResolution &WPDRes) {
7713
5
  if (ParseToken(lltok::kw_wpdRes, "expected 'wpdRes' here") ||
7714
5
      ParseToken(lltok::colon, "expected ':' here") ||
7715
5
      ParseToken(lltok::lparen, "expected '(' here") ||
7716
5
      ParseToken(lltok::kw_kind, "expected 'kind' here") ||
7717
5
      ParseToken(lltok::colon, "expected ':' here"))
7718
0
    return true;
7719
5
7720
5
  switch (Lex.getKind()) {
7721
5
  case lltok::kw_indir:
7722
1
    WPDRes.TheKind = WholeProgramDevirtResolution::Indir;
7723
1
    break;
7724
5
  case lltok::kw_singleImpl:
7725
2
    WPDRes.TheKind = WholeProgramDevirtResolution::SingleImpl;
7726
2
    break;
7727
5
  case lltok::kw_branchFunnel:
7728
2
    WPDRes.TheKind = WholeProgramDevirtResolution::BranchFunnel;
7729
2
    break;
7730
5
  default:
7731
0
    return Error(Lex.getLoc(), "unexpected WholeProgramDevirtResolution kind");
7732
5
  }
7733
5
  Lex.Lex();
7734
5
7735
5
  // Parse optional fields
7736
8
  while (EatIfPresent(lltok::comma)) {
7737
3
    switch (Lex.getKind()) {
7738
3
    case lltok::kw_singleImplName:
7739
2
      Lex.Lex();
7740
2
      if (ParseToken(lltok::colon, "expected ':' here") ||
7741
2
          ParseStringConstant(WPDRes.SingleImplName))
7742
0
        return true;
7743
2
      break;
7744
2
    case lltok::kw_resByArg:
7745
1
      if (ParseOptionalResByArg(WPDRes.ResByArg))
7746
0
        return true;
7747
1
      break;
7748
1
    default:
7749
0
      return Error(Lex.getLoc(),
7750
0
                   "expected optional WholeProgramDevirtResolution field");
7751
3
    }
7752
3
  }
7753
5
7754
5
  if (ParseToken(lltok::rparen, "expected ')' here"))
7755
0
    return true;
7756
5
7757
5
  return false;
7758
5
}
7759
7760
/// OptionalResByArg
7761
///   ::= 'wpdRes' ':' '(' ResByArg[, ResByArg]* ')'
7762
/// ResByArg ::= Args ',' 'byArg' ':' '(' 'kind' ':'
7763
///                ( 'indir' | 'uniformRetVal' | 'UniqueRetVal' |
7764
///                  'virtualConstProp' )
7765
///                [',' 'info' ':' UInt64]? [',' 'byte' ':' UInt32]?
7766
///                [',' 'bit' ':' UInt32]? ')'
7767
bool LLParser::ParseOptionalResByArg(
7768
    std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
7769
1
        &ResByArg) {
7770
1
  if (ParseToken(lltok::kw_resByArg, "expected 'resByArg' here") ||
7771
1
      ParseToken(lltok::colon, "expected ':' here") ||
7772
1
      ParseToken(lltok::lparen, "expected '(' here"))
7773
0
    return true;
7774
1
7775
4
  
do 1
{
7776
4
    std::vector<uint64_t> Args;
7777
4
    if (ParseArgs(Args) || ParseToken(lltok::comma, "expected ',' here") ||
7778
4
        ParseToken(lltok::kw_byArg, "expected 'byArg here") ||
7779
4
        ParseToken(lltok::colon, "expected ':' here") ||
7780
4
        ParseToken(lltok::lparen, "expected '(' here") ||
7781
4
        ParseToken(lltok::kw_kind, "expected 'kind' here") ||
7782
4
        ParseToken(lltok::colon, "expected ':' here"))
7783
0
      return true;
7784
4
7785
4
    WholeProgramDevirtResolution::ByArg ByArg;
7786
4
    switch (Lex.getKind()) {
7787
4
    case lltok::kw_indir:
7788
1
      ByArg.TheKind = WholeProgramDevirtResolution::ByArg::Indir;
7789
1
      break;
7790
4
    case lltok::kw_uniformRetVal:
7791
1
      ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniformRetVal;
7792
1
      break;
7793
4
    case lltok::kw_uniqueRetVal:
7794
1
      ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniqueRetVal;
7795
1
      break;
7796
4
    case lltok::kw_virtualConstProp:
7797
1
      ByArg.TheKind = WholeProgramDevirtResolution::ByArg::VirtualConstProp;
7798
1
      break;
7799
4
    default:
7800
0
      return Error(Lex.getLoc(),
7801
0
                   "unexpected WholeProgramDevirtResolution::ByArg kind");
7802
4
    }
7803
4
    Lex.Lex();
7804
4
7805
4
    // Parse optional fields
7806
8
    while (EatIfPresent(lltok::comma)) {
7807
4
      switch (Lex.getKind()) {
7808
4
      case lltok::kw_info:
7809
2
        Lex.Lex();
7810
2
        if (ParseToken(lltok::colon, "expected ':' here") ||
7811
2
            ParseUInt64(ByArg.Info))
7812
0
          return true;
7813
2
        break;
7814
2
      case lltok::kw_byte:
7815
1
        Lex.Lex();
7816
1
        if (ParseToken(lltok::colon, "expected ':' here") ||
7817
1
            ParseUInt32(ByArg.Byte))
7818
0
          return true;
7819
1
        break;
7820
1
      case lltok::kw_bit:
7821
1
        Lex.Lex();
7822
1
        if (ParseToken(lltok::colon, "expected ':' here") ||
7823
1
            ParseUInt32(ByArg.Bit))
7824
0
          return true;
7825
1
        break;
7826
1
      default:
7827
0
        return Error(Lex.getLoc(),
7828
0
                     "expected optional whole program devirt field");
7829
4
      }
7830
4
    }
7831
4
7832
4
    if (ParseToken(lltok::rparen, "expected ')' here"))
7833
0
      return true;
7834
4
7835
4
    ResByArg[Args] = ByArg;
7836
4
  } while (EatIfPresent(lltok::comma));
7837
1
7838
1
  if (ParseToken(lltok::rparen, "expected ')' here"))
7839
0
    return true;
7840
1
7841
1
  return false;
7842
1
}
7843
7844
/// OptionalResByArg
7845
///   ::= 'args' ':' '(' UInt64[, UInt64]* ')'
7846
12
bool LLParser::ParseArgs(std::vector<uint64_t> &Args) {
7847
12
  if (ParseToken(lltok::kw_args, "expected 'args' here") ||
7848
12
      ParseToken(lltok::colon, "expected ':' here") ||
7849
12
      ParseToken(lltok::lparen, "expected '(' here"))
7850
0
    return true;
7851
12
7852
13
  
do 12
{
7853
13
    uint64_t Val;
7854
13
    if (ParseUInt64(Val))
7855
0
      return true;
7856
13
    Args.push_back(Val);
7857
13
  } while (EatIfPresent(lltok::comma));
7858
12
7859
12
  if (ParseToken(lltok::rparen, "expected ')' here"))
7860
0
    return true;
7861
12
7862
12
  return false;
7863
12
}
7864
7865
static const auto FwdVIRef = (GlobalValueSummaryMapTy::value_type *)-8;
7866
7867
27
static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) {
7868
27
  bool ReadOnly = Fwd->isReadOnly();
7869
27
  bool WriteOnly = Fwd->isWriteOnly();
7870
27
  assert(!(ReadOnly && WriteOnly));
7871
27
  *Fwd = Resolved;
7872
27
  if (ReadOnly)
7873
2
    Fwd->setReadOnly();
7874
27
  if (WriteOnly)
7875
1
    Fwd->setWriteOnly();
7876
27
}
7877
7878
/// Stores the given Name/GUID and associated summary into the Index.
7879
/// Also updates any forward references to the associated entry ID.
7880
void LLParser::AddGlobalValueToIndex(
7881
    std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage,
7882
116
    unsigned ID, std::unique_ptr<GlobalValueSummary> Summary) {
7883
116
  // First create the ValueInfo utilizing the Name or GUID.
7884
116
  ValueInfo VI;
7885
116
  if (GUID != 0) {
7886
54
    assert(Name.empty());
7887
54
    VI = Index->getOrInsertValueInfo(GUID);
7888
62
  } else {
7889
62
    assert(!Name.empty());
7890
62
    if (M) {
7891
62
      auto *GV = M->getNamedValue(Name);
7892
62
      assert(GV);
7893
62
      VI = Index->getOrInsertValueInfo(GV);
7894
62
    } else {
7895
0
      assert(
7896
0
          (!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
7897
0
          "Need a source_filename to compute GUID for local");
7898
0
      GUID = GlobalValue::getGUID(
7899
0
          GlobalValue::getGlobalIdentifier(Name, Linkage, SourceFileName));
7900
0
      VI = Index->getOrInsertValueInfo(GUID, Index->saveString(Name));
7901
0
    }
7902
62
  }
7903
116
7904
116
  // Resolve forward references from calls/refs
7905
116
  auto FwdRefVIs = ForwardRefValueInfos.find(ID);
7906
116
  if (FwdRefVIs != ForwardRefValueInfos.end()) {
7907
27
    for (auto VIRef : FwdRefVIs->second) {
7908
27
      assert(VIRef.first->getRef() == FwdVIRef &&
7909
27
             "Forward referenced ValueInfo expected to be empty");
7910
27
      resolveFwdRef(VIRef.first, VI);
7911
27
    }
7912
21
    ForwardRefValueInfos.erase(FwdRefVIs);
7913
21
  }
7914
116
7915
116
  // Resolve forward references from aliases
7916
116
  auto FwdRefAliasees = ForwardRefAliasees.find(ID);
7917
116
  if (FwdRefAliasees != ForwardRefAliasees.end()) {
7918
4
    for (auto AliaseeRef : FwdRefAliasees->second) {
7919
4
      assert(!AliaseeRef.first->hasAliasee() &&
7920
4
             "Forward referencing alias already has aliasee");
7921
4
      assert(Summary && "Aliasee must be a definition");
7922
4
      AliaseeRef.first->setAliasee(VI, Summary.get());
7923
4
    }
7924
4
    ForwardRefAliasees.erase(FwdRefAliasees);
7925
4
  }
7926
116
7927
116
  // Add the summary if one was provided.
7928
116
  if (Summary)
7929
88
    Index->addGlobalValueSummary(VI, std::move(Summary));
7930
116
7931
116
  // Save the associated ValueInfo for use in later references by ID.
7932
116
  if (ID == NumberedValueInfos.size())
7933
96
    NumberedValueInfos.push_back(VI);
7934
20
  else {
7935
20
    // Handle non-continuous numbers (to make test simplification easier).
7936
20
    if (ID > NumberedValueInfos.size())
7937
20
      NumberedValueInfos.resize(ID + 1);
7938
20
    NumberedValueInfos[ID] = VI;
7939
20
  }
7940
116
}
7941
7942
/// ParseGVEntry
7943
///   ::= 'gv' ':' '(' ('name' ':' STRINGCONSTANT | 'guid' ':' UInt64)
7944
///         [',' 'summaries' ':' Summary[',' Summary]* ]? ')'
7945
/// Summary ::= '(' (FunctionSummary | VariableSummary | AliasSummary) ')'
7946
116
bool LLParser::ParseGVEntry(unsigned ID) {
7947
116
  assert(Lex.getKind() == lltok::kw_gv);
7948
116
  Lex.Lex();
7949
116
7950
116
  if (ParseToken(lltok::colon, "expected ':' here") ||
7951
116
      ParseToken(lltok::lparen, "expected '(' here"))
7952
0
    return true;
7953
116
7954
116
  std::string Name;
7955
116
  GlobalValue::GUID GUID = 0;
7956
116
  switch (Lex.getKind()) {
7957
116
  case lltok::kw_name:
7958
62
    Lex.Lex();
7959
62
    if (ParseToken(lltok::colon, "expected ':' here") ||
7960
62
        ParseStringConstant(Name))
7961
0
      return true;
7962
62
    // Can't create GUID/ValueInfo until we have the linkage.
7963
62
    break;
7964
62
  case lltok::kw_guid:
7965
54
    Lex.Lex();
7966
54
    if (ParseToken(lltok::colon, "expected ':' here") || ParseUInt64(GUID))
7967
0
      return true;
7968
54
    break;
7969
54
  default:
7970
0
    return Error(Lex.getLoc(), "expected name or guid tag");
7971
116
  }
7972
116
7973
116
  if (!EatIfPresent(lltok::comma)) {
7974
28
    // No summaries. Wrap up.
7975
28
    if (ParseToken(lltok::rparen, "expected ')' here"))
7976
0
      return true;
7977
28
    // This was created for a call to an external or indirect target.
7978
28
    // A GUID with no summary came from a VALUE_GUID record, dummy GUID
7979
28
    // created for indirect calls with VP. A Name with no GUID came from
7980
28
    // an external definition. We pass ExternalLinkage since that is only
7981
28
    // used when the GUID must be computed from Name, and in that case
7982
28
    // the symbol must have external linkage.
7983
28
    AddGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
7984
28
                          nullptr);
7985
28
    return false;
7986
28
  }
7987
88
7988
88
  // Have a list of summaries
7989
88
  if (ParseToken(lltok::kw_summaries, "expected 'summaries' here") ||
7990
88
      ParseToken(lltok::colon, "expected ':' here"))
7991
0
    return true;
7992
88
7993
88
  do {
7994
88
    if (ParseToken(lltok::lparen, "expected '(' here"))
7995
0
      return true;
7996
88
    switch (Lex.getKind()) {
7997
88
    case lltok::kw_function:
7998
67
      if (ParseFunctionSummary(Name, GUID, ID))
7999
0
        return true;
8000
67
      break;
8001
67
    case lltok::kw_variable:
8002
16
      if (ParseVariableSummary(Name, GUID, ID))
8003
0
        return true;
8004
16
      break;
8005
16
    case lltok::kw_alias:
8006
5
      if (ParseAliasSummary(Name, GUID, ID))
8007
0
        return true;
8008
5
      break;
8009
5
    default:
8010
0
      return Error(Lex.getLoc(), "expected summary type");
8011
88
    }
8012
88
    if (ParseToken(lltok::rparen, "expected ')' here"))
8013
0
      return true;
8014
88
  } while (EatIfPresent(lltok::comma));
8015
88
8016
88
  if (ParseToken(lltok::rparen, "expected ')' here"))
8017
0
    return true;
8018
88
8019
88
  return false;
8020
88
}
8021
8022
/// FunctionSummary
8023
///   ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags
8024
///         ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]?
8025
///         [',' OptionalTypeIdInfo]? [',' OptionalRefs]? ')'
8026
bool LLParser::ParseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
8027
67
                                    unsigned ID) {
8028
67
  assert(Lex.getKind() == lltok::kw_function);
8029
67
  Lex.Lex();
8030
67
8031
67
  StringRef ModulePath;
8032
67
  GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
8033
67
      /*Linkage=*/GlobalValue::ExternalLinkage, /*NotEligibleToImport=*/false,
8034
67
      /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false);
8035
67
  unsigned InstCount;
8036
67
  std::vector<FunctionSummary::EdgeTy> Calls;
8037
67
  FunctionSummary::TypeIdInfo TypeIdInfo;
8038
67
  std::vector<ValueInfo> Refs;
8039
67
  // Default is all-zeros (conservative values).
8040
67
  FunctionSummary::FFlags FFlags = {};
8041
67
  if (ParseToken(lltok::colon, "expected ':' here") ||
8042
67
      ParseToken(lltok::lparen, "expected '(' here") ||
8043
67
      ParseModuleReference(ModulePath) ||
8044
67
      ParseToken(lltok::comma, "expected ',' here") || ParseGVFlags(GVFlags) ||
8045
67
      ParseToken(lltok::comma, "expected ',' here") ||
8046
67
      ParseToken(lltok::kw_insts, "expected 'insts' here") ||
8047
67
      ParseToken(lltok::colon, "expected ':' here") || ParseUInt32(InstCount))
8048
0
    return true;
8049
67
8050
67
  // Parse optional fields
8051
118
  
while (67
EatIfPresent(lltok::comma)) {
8052
51
    switch (Lex.getKind()) {
8053
51
    case lltok::kw_funcFlags:
8054
5
      if (ParseOptionalFFlags(FFlags))
8055
0
        return true;
8056
5
      break;
8057
15
    case lltok::kw_calls:
8058
15
      if (ParseOptionalCalls(Calls))
8059
0
        return true;
8060
15
      break;
8061
25
    case lltok::kw_typeIdInfo:
8062
25
      if (ParseOptionalTypeIdInfo(TypeIdInfo))
8063
0
        return true;
8064
25
      break;
8065
25
    case lltok::kw_refs:
8066
6
      if (ParseOptionalRefs(Refs))
8067
0
        return true;
8068
6
      break;
8069
6
    default:
8070
0
      return Error(Lex.getLoc(), "expected optional function summary field");
8071
51
    }
8072
51
  }
8073
67
8074
67
  if (ParseToken(lltok::rparen, "expected ')' here"))
8075
0
    return true;
8076
67
8077
67
  auto FS = llvm::make_unique<FunctionSummary>(
8078
67
      GVFlags, InstCount, FFlags, /*EntryCount=*/0, std::move(Refs),
8079
67
      std::move(Calls), std::move(TypeIdInfo.TypeTests),
8080
67
      std::move(TypeIdInfo.TypeTestAssumeVCalls),
8081
67
      std::move(TypeIdInfo.TypeCheckedLoadVCalls),
8082
67
      std::move(TypeIdInfo.TypeTestAssumeConstVCalls),
8083
67
      std::move(TypeIdInfo.TypeCheckedLoadConstVCalls));
8084
67
8085
67
  FS->setModulePath(ModulePath);
8086
67
8087
67
  AddGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
8088
67
                        ID, std::move(FS));
8089
67
8090
67
  return false;
8091
67
}
8092
8093
/// VariableSummary
8094
///   ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags
8095
///         [',' OptionalRefs]? ')'
8096
bool LLParser::ParseVariableSummary(std::string Name, GlobalValue::GUID GUID,
8097
16
                                    unsigned ID) {
8098
16
  assert(Lex.getKind() == lltok::kw_variable);
8099
16
  Lex.Lex();
8100
16
8101
16
  StringRef ModulePath;
8102
16
  GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
8103
16
      /*Linkage=*/GlobalValue::ExternalLinkage, /*NotEligibleToImport=*/false,
8104
16
      /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false);
8105
16
  GlobalVarSummary::GVarFlags GVarFlags(/*ReadOnly*/ false,
8106
16
                                        /* WriteOnly */ false);
8107
16
  std::vector<ValueInfo> Refs;
8108
16
  VTableFuncList VTableFuncs;
8109
16
  if (ParseToken(lltok::colon, "expected ':' here") ||
8110
16
      ParseToken(lltok::lparen, "expected '(' here") ||
8111
16
      ParseModuleReference(ModulePath) ||
8112
16
      ParseToken(lltok::comma, "expected ',' here") || ParseGVFlags(GVFlags) ||
8113
16
      ParseToken(lltok::comma, "expected ',' here") ||
8114
16
      ParseGVarFlags(GVarFlags))
8115
0
    return true;
8116
16
8117
16
  // Parse optional fields
8118
30
  
while (16
EatIfPresent(lltok::comma)) {
8119
14
    switch (Lex.getKind()) {
8120
14
    case lltok::kw_vTableFuncs:
8121
3
      if (ParseOptionalVTableFuncs(VTableFuncs))
8122
0
        return true;
8123
3
      break;
8124
11
    case lltok::kw_refs:
8125
11
      if (ParseOptionalRefs(Refs))
8126
0
        return true;
8127
11
      break;
8128
11
    default:
8129
0
      return Error(Lex.getLoc(), "expected optional variable summary field");
8130
14
    }
8131
14
  }
8132
16
8133
16
  if (ParseToken(lltok::rparen, "expected ')' here"))
8134
0
    return true;
8135
16
8136
16
  auto GS =
8137
16
      llvm::make_unique<GlobalVarSummary>(GVFlags, GVarFlags, std::move(Refs));
8138
16
8139
16
  GS->setModulePath(ModulePath);
8140
16
  GS->setVTableFuncs(std::move(VTableFuncs));
8141
16
8142
16
  AddGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
8143
16
                        ID, std::move(GS));
8144
16
8145
16
  return false;
8146
16
}
8147
8148
/// AliasSummary
8149
///   ::= 'alias' ':' '(' 'module' ':' ModuleReference ',' GVFlags ','
8150
///         'aliasee' ':' GVReference ')'
8151
bool LLParser::ParseAliasSummary(std::string Name, GlobalValue::GUID GUID,
8152
5
                                 unsigned ID) {
8153
5
  assert(Lex.getKind() == lltok::kw_alias);
8154
5
  LocTy Loc = Lex.getLoc();
8155
5
  Lex.Lex();
8156
5
8157
5
  StringRef ModulePath;
8158
5
  GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
8159
5
      /*Linkage=*/GlobalValue::ExternalLinkage, /*NotEligibleToImport=*/false,
8160
5
      /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false);
8161
5
  if (ParseToken(lltok::colon, "expected ':' here") ||
8162
5
      ParseToken(lltok::lparen, "expected '(' here") ||
8163
5
      ParseModuleReference(ModulePath) ||
8164
5
      ParseToken(lltok::comma, "expected ',' here") || ParseGVFlags(GVFlags) ||
8165
5
      ParseToken(lltok::comma, "expected ',' here") ||
8166
5
      ParseToken(lltok::kw_aliasee, "expected 'aliasee' here") ||
8167
5
      ParseToken(lltok::colon, "expected ':' here"))
8168
0
    return true;
8169
5
8170
5
  ValueInfo AliaseeVI;
8171
5
  unsigned GVId;
8172
5
  if (ParseGVReference(AliaseeVI, GVId))
8173
0
    return true;
8174
5
8175
5
  if (ParseToken(lltok::rparen, "expected ')' here"))
8176
0
    return true;
8177
5
8178
5
  auto AS = llvm::make_unique<AliasSummary>(GVFlags);
8179
5
8180
5
  AS->setModulePath(ModulePath);
8181
5
8182
5
  // Record forward reference if the aliasee is not parsed yet.
8183
5
  if (AliaseeVI.getRef() == FwdVIRef) {
8184
4
    auto FwdRef = ForwardRefAliasees.insert(
8185
4
        std::make_pair(GVId, std::vector<std::pair<AliasSummary *, LocTy>>()));
8186
4
    FwdRef.first->second.push_back(std::make_pair(AS.get(), Loc));
8187
4
  } else {
8188
1
    auto Summary = Index->findSummaryInModule(AliaseeVI, ModulePath);
8189
1
    assert(Summary && "Aliasee must be a definition");
8190
1
    AS->setAliasee(AliaseeVI, Summary);
8191
1
  }
8192
5
8193
5
  AddGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
8194
5
                        ID, std::move(AS));
8195
5
8196
5
  return false;
8197
5
}
8198
8199
/// Flag
8200
///   ::= [0|1]
8201
373
bool LLParser::ParseFlag(unsigned &Val) {
8202
373
  if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
8203
0
    return TokError("expected integer");
8204
373
  Val = (unsigned)Lex.getAPSIntVal().getBoolValue();
8205
373
  Lex.Lex();
8206
373
  return false;
8207
373
}
8208
8209
/// OptionalFFlags
8210
///   := 'funcFlags' ':' '(' ['readNone' ':' Flag]?
8211
///        [',' 'readOnly' ':' Flag]? [',' 'noRecurse' ':' Flag]?
8212
///        [',' 'returnDoesNotAlias' ':' Flag]? ')'
8213
///        [',' 'noInline' ':' Flag]? ')'
8214
5
bool LLParser::ParseOptionalFFlags(FunctionSummary::FFlags &FFlags) {
8215
5
  assert(Lex.getKind() == lltok::kw_funcFlags);
8216
5
  Lex.Lex();
8217
5
8218
5
  if (ParseToken(lltok::colon, "expected ':' in funcFlags") |
8219
5
      ParseToken(lltok::lparen, "expected '(' in funcFlags"))
8220
0
    return true;
8221
5
8222
17
  
do 5
{
8223
17
    unsigned Val = 0;
8224
17
    switch (Lex.getKind()) {
8225
17
    case lltok::kw_readNone:
8226
4
      Lex.Lex();
8227
4
      if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Val))
8228
0
        return true;
8229
4
      FFlags.ReadNone = Val;
8230
4
      break;
8231
4
    case lltok::kw_readOnly:
8232
4
      Lex.Lex();
8233
4
      if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Val))
8234
0
        return true;
8235
4
      FFlags.ReadOnly = Val;
8236
4
      break;
8237
4
    case lltok::kw_noRecurse:
8238
4
      Lex.Lex();
8239
4
      if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Val))
8240
0
        return true;
8241
4
      FFlags.NoRecurse = Val;
8242
4
      break;
8243
4
    case lltok::kw_returnDoesNotAlias:
8244
4
      Lex.Lex();
8245
4
      if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Val))
8246
0
        return true;
8247
4
      FFlags.ReturnDoesNotAlias = Val;
8248
4
      break;
8249
4
    case lltok::kw_noInline:
8250
1
      Lex.Lex();
8251
1
      if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Val))
8252
0
        return true;
8253
1
      FFlags.NoInline = Val;
8254
1
      break;
8255
1
    default:
8256
0
      return Error(Lex.getLoc(), "expected function flag type");
8257
17
    }
8258
17
  } while (EatIfPresent(lltok::comma));
8259
5
8260
5
  if (ParseToken(lltok::rparen, "expected ')' in funcFlags"))
8261
0
    return true;
8262
5
8263
5
  return false;
8264
5
}
8265
8266
/// OptionalCalls
8267
///   := 'calls' ':' '(' Call [',' Call]* ')'
8268
/// Call ::= '(' 'callee' ':' GVReference
8269
///            [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]? ')'
8270
15
bool LLParser::ParseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls) {
8271
15
  assert(Lex.getKind() == lltok::kw_calls);
8272
15
  Lex.Lex();
8273
15
8274
15
  if (ParseToken(lltok::colon, "expected ':' in calls") |
8275
15
      ParseToken(lltok::lparen, "expected '(' in calls"))
8276
0
    return true;
8277
15
8278
15
  IdToIndexMapType IdToIndexMap;
8279
15
  // Parse each call edge
8280
37
  do {
8281
37
    ValueInfo VI;
8282
37
    if (ParseToken(lltok::lparen, "expected '(' in call") ||
8283
37
        ParseToken(lltok::kw_callee, "expected 'callee' in call") ||
8284
37
        ParseToken(lltok::colon, "expected ':'"))
8285
0
      return true;
8286
37
8287
37
    LocTy Loc = Lex.getLoc();
8288
37
    unsigned GVId;
8289
37
    if (ParseGVReference(VI, GVId))
8290
0
      return true;
8291
37
8292
37
    CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown;
8293
37
    unsigned RelBF = 0;
8294
37
    if (EatIfPresent(lltok::comma)) {
8295
28
      // Expect either hotness or relbf
8296
28
      if (EatIfPresent(lltok::kw_hotness)) {
8297
26
        if (ParseToken(lltok::colon, "expected ':'") || ParseHotness(Hotness))
8298
0
          return true;
8299
2
      } else {
8300
2
        if (ParseToken(lltok::kw_relbf, "expected relbf") ||
8301
2
            ParseToken(lltok::colon, "expected ':'") || ParseUInt32(RelBF))
8302
0
          return true;
8303
37
      }
8304
28
    }
8305
37
    // Keep track of the Call array index needing a forward reference.
8306
37
    // We will save the location of the ValueInfo needing an update, but
8307
37
    // can only do so once the std::vector is finalized.
8308
37
    if (VI.getRef() == FwdVIRef)
8309
11
      IdToIndexMap[GVId].push_back(std::make_pair(Calls.size(), Loc));
8310
37
    Calls.push_back(FunctionSummary::EdgeTy{VI, CalleeInfo(Hotness, RelBF)});
8311
37
8312
37
    if (ParseToken(lltok::rparen, "expected ')' in call"))
8313
0
      return true;
8314
37
  } while (EatIfPresent(lltok::comma));
8315
15
8316
15
  // Now that the Calls vector is finalized, it is safe to save the locations
8317
15
  // of any forward GV references that need updating later.
8318
15
  for (auto I : IdToIndexMap) {
8319
11
    for (auto P : I.second) {
8320
11
      assert(Calls[P.first].first.getRef() == FwdVIRef &&
8321
11
             "Forward referenced ValueInfo expected to be empty");
8322
11
      auto FwdRef = ForwardRefValueInfos.insert(std::make_pair(
8323
11
          I.first, std::vector<std::pair<ValueInfo *, LocTy>>()));
8324
11
      FwdRef.first->second.push_back(
8325
11
          std::make_pair(&Calls[P.first].first, P.second));
8326
11
    }
8327
11
  }
8328
15
8329
15
  if (ParseToken(lltok::rparen, "expected ')' in calls"))
8330
0
    return true;
8331
15
8332
15
  return false;
8333
15
}
8334
8335
/// Hotness
8336
///   := ('unknown'|'cold'|'none'|'hot'|'critical')
8337
26
bool LLParser::ParseHotness(CalleeInfo::HotnessType &Hotness) {
8338
26
  switch (Lex.getKind()) {
8339
26
  case lltok::kw_unknown:
8340
0
    Hotness = CalleeInfo::HotnessType::Unknown;
8341
0
    break;
8342
26
  case lltok::kw_cold:
8343
5
    Hotness = CalleeInfo::HotnessType::Cold;
8344
5
    break;
8345
26
  case lltok::kw_none:
8346
10
    Hotness = CalleeInfo::HotnessType::None;
8347
10
    break;
8348
26
  case lltok::kw_hot:
8349
10
    Hotness = CalleeInfo::HotnessType::Hot;
8350
10
    break;
8351
26
  case lltok::kw_critical:
8352
1
    Hotness = CalleeInfo::HotnessType::Critical;
8353
1
    break;
8354
26
  default:
8355
0
    return Error(Lex.getLoc(), "invalid call edge hotness");
8356
26
  }
8357
26
  Lex.Lex();
8358
26
  return false;
8359
26
}
8360
8361
/// OptionalVTableFuncs
8362
///   := 'vTableFuncs' ':' '(' VTableFunc [',' VTableFunc]* ')'
8363
/// VTableFunc ::= '(' 'virtFunc' ':' GVReference ',' 'offset' ':' UInt64 ')'
8364
3
bool LLParser::ParseOptionalVTableFuncs(VTableFuncList &VTableFuncs) {
8365
3
  assert(Lex.getKind() == lltok::kw_vTableFuncs);
8366
3
  Lex.Lex();
8367
3
8368
3
  if (ParseToken(lltok::colon, "expected ':' in vTableFuncs") |
8369
3
      ParseToken(lltok::lparen, "expected '(' in vTableFuncs"))
8370
0
    return true;
8371
3
8372
3
  IdToIndexMapType IdToIndexMap;
8373
3
  // Parse each virtual function pair
8374
5
  do {
8375
5
    ValueInfo VI;
8376
5
    if (ParseToken(lltok::lparen, "expected '(' in vTableFunc") ||
8377
5
        ParseToken(lltok::kw_virtFunc, "expected 'callee' in vTableFunc") ||
8378
5
        ParseToken(lltok::colon, "expected ':'"))
8379
0
      return true;
8380
5
8381
5
    LocTy Loc = Lex.getLoc();
8382
5
    unsigned GVId;
8383
5
    if (ParseGVReference(VI, GVId))
8384
0
      return true;
8385
5
8386
5
    uint64_t Offset;
8387
5
    if (ParseToken(lltok::comma, "expected comma") ||
8388
5
        ParseToken(lltok::kw_offset, "expected offset") ||
8389
5
        ParseToken(lltok::colon, "expected ':'") || ParseUInt64(Offset))
8390
0
      return true;
8391
5
8392
5
    // Keep track of the VTableFuncs array index needing a forward reference.
8393
5
    // We will save the location of the ValueInfo needing an update, but
8394
5
    // can only do so once the std::vector is finalized.
8395
5
    if (VI == EmptyVI)
8396
2
      IdToIndexMap[GVId].push_back(std::make_pair(VTableFuncs.size(), Loc));
8397
5
    VTableFuncs.push_back({VI, Offset});
8398
5
8399
5
    if (ParseToken(lltok::rparen, "expected ')' in vTableFunc"))
8400
0
      return true;
8401
5
  } while (EatIfPresent(lltok::comma));
8402
3
8403
3
  // Now that the VTableFuncs vector is finalized, it is safe to save the
8404
3
  // locations of any forward GV references that need updating later.
8405
3
  for (auto I : IdToIndexMap) {
8406
2
    for (auto P : I.second) {
8407
2
      assert(VTableFuncs[P.first].FuncVI == EmptyVI &&
8408
2
             "Forward referenced ValueInfo expected to be empty");
8409
2
      auto FwdRef = ForwardRefValueInfos.insert(std::make_pair(
8410
2
          I.first, std::vector<std::pair<ValueInfo *, LocTy>>()));
8411
2
      FwdRef.first->second.push_back(
8412
2
          std::make_pair(&VTableFuncs[P.first].FuncVI, P.second));
8413
2
    }
8414
2
  }
8415
3
8416
3
  if (ParseToken(lltok::rparen, "expected ')' in vTableFuncs"))
8417
0
    return true;
8418
3
8419
3
  return false;
8420
3
}
8421
8422
/// OptionalRefs
8423
///   := 'refs' ':' '(' GVReference [',' GVReference]* ')'
8424
17
bool LLParser::ParseOptionalRefs(std::vector<ValueInfo> &Refs) {
8425
17
  assert(Lex.getKind() == lltok::kw_refs);
8426
17
  Lex.Lex();
8427
17
8428
17
  if (ParseToken(lltok::colon, "expected ':' in refs") |
8429
17
      ParseToken(lltok::lparen, "expected '(' in refs"))
8430
0
    return true;
8431
17
8432
17
  struct ValueContext {
8433
17
    ValueInfo VI;
8434
17
    unsigned GVId;
8435
17
    LocTy Loc;
8436
17
  };
8437
17
  std::vector<ValueContext> VContexts;
8438
17
  // Parse each ref edge
8439
30
  do {
8440
30
    ValueContext VC;
8441
30
    VC.Loc = Lex.getLoc();
8442
30
    if (ParseGVReference(VC.VI, VC.GVId))
8443
0
      return true;
8444
30
    VContexts.push_back(VC);
8445
30
  } while (EatIfPresent(lltok::comma));
8446
17
8447
17
  // Sort value contexts so that ones with writeonly
8448
17
  // and readonly ValueInfo  are at the end of VContexts vector.
8449
17
  // See FunctionSummary::specialRefCounts()
8450
17
  llvm::sort(VContexts, [](const ValueContext &VC1, const ValueContext &VC2) {
8451
13
    return VC1.VI.getAccessSpecifier() < VC2.VI.getAccessSpecifier();
8452
13
  });
8453
17
8454
17
  IdToIndexMapType IdToIndexMap;
8455
30
  for (auto &VC : VContexts) {
8456
30
    // Keep track of the Refs array index needing a forward reference.
8457
30
    // We will save the location of the ValueInfo needing an update, but
8458
30
    // can only do so once the std::vector is finalized.
8459
30
    if (VC.VI.getRef() == FwdVIRef)
8460
14
      IdToIndexMap[VC.GVId].push_back(std::make_pair(Refs.size(), VC.Loc));
8461
30
    Refs.push_back(VC.VI);
8462
30
  }
8463
17
8464
17
  // Now that the Refs vector is finalized, it is safe to save the locations
8465
17
  // of any forward GV references that need updating later.
8466
17
  for (auto I : IdToIndexMap) {
8467
14
    for (auto P : I.second) {
8468
14
      assert(Refs[P.first].getRef() == FwdVIRef &&
8469
14
             "Forward referenced ValueInfo expected to be empty");
8470
14
      auto FwdRef = ForwardRefValueInfos.insert(std::make_pair(
8471
14
          I.first, std::vector<std::pair<ValueInfo *, LocTy>>()));
8472
14
      FwdRef.first->second.push_back(std::make_pair(&Refs[P.first], P.second));
8473
14
    }
8474
14
  }
8475
17
8476
17
  if (ParseToken(lltok::rparen, "expected ')' in refs"))
8477
0
    return true;
8478
17
8479
17
  return false;
8480
17
}
8481
8482
/// OptionalTypeIdInfo
8483
///   := 'typeidinfo' ':' '(' [',' TypeTests]? [',' TypeTestAssumeVCalls]?
8484
///         [',' TypeCheckedLoadVCalls]?  [',' TypeTestAssumeConstVCalls]?
8485
///         [',' TypeCheckedLoadConstVCalls]? ')'
8486
bool LLParser::ParseOptionalTypeIdInfo(
8487
25
    FunctionSummary::TypeIdInfo &TypeIdInfo) {
8488
25
  assert(Lex.getKind() == lltok::kw_typeIdInfo);
8489
25
  Lex.Lex();
8490
25
8491
25
  if (ParseToken(lltok::colon, "expected ':' here") ||
8492
25
      ParseToken(lltok::lparen, "expected '(' in typeIdInfo"))
8493
0
    return true;
8494
25
8495
26
  
do 25
{
8496
26
    switch (Lex.getKind()) {
8497
26
    case lltok::kw_typeTests:
8498
11
      if (ParseTypeTests(TypeIdInfo.TypeTests))
8499
0
        return true;
8500
11
      break;
8501
11
    case lltok::kw_typeTestAssumeVCalls:
8502
5
      if (ParseVFuncIdList(lltok::kw_typeTestAssumeVCalls,
8503
5
                           TypeIdInfo.TypeTestAssumeVCalls))
8504
0
        return true;
8505
5
      break;
8506
5
    case lltok::kw_typeCheckedLoadVCalls:
8507
4
      if (ParseVFuncIdList(lltok::kw_typeCheckedLoadVCalls,
8508
4
                           TypeIdInfo.TypeCheckedLoadVCalls))
8509
0
        return true;
8510
4
      break;
8511
4
    case lltok::kw_typeTestAssumeConstVCalls:
8512
3
      if (ParseConstVCallList(lltok::kw_typeTestAssumeConstVCalls,
8513
3
                              TypeIdInfo.TypeTestAssumeConstVCalls))
8514
0
        return true;
8515
3
      break;
8516
3
    case lltok::kw_typeCheckedLoadConstVCalls:
8517
3
      if (ParseConstVCallList(lltok::kw_typeCheckedLoadConstVCalls,
8518
3
                              TypeIdInfo.TypeCheckedLoadConstVCalls))
8519
0
        return true;
8520
3
      break;
8521
3
    default:
8522
0
      return Error(Lex.getLoc(), "invalid typeIdInfo list type");
8523
26
    }
8524
26
  } while (EatIfPresent(lltok::comma));
8525
25
8526
25
  if (ParseToken(lltok::rparen, "expected ')' in typeIdInfo"))
8527
0
    return true;
8528
25
8529
25
  return false;
8530
25
}
8531
8532
/// TypeTests
8533
///   ::= 'typeTests' ':' '(' (SummaryID | UInt64)
8534
///         [',' (SummaryID | UInt64)]* ')'
8535
11
bool LLParser::ParseTypeTests(std::vector<GlobalValue::GUID> &TypeTests) {
8536
11
  assert(Lex.getKind() == lltok::kw_typeTests);
8537
11
  Lex.Lex();
8538
11
8539
11
  if (ParseToken(lltok::colon, "expected ':' here") ||
8540
11
      ParseToken(lltok::lparen, "expected '(' in typeIdInfo"))
8541
0
    return true;
8542
11
8543
11
  IdToIndexMapType IdToIndexMap;
8544
14
  do {
8545
14
    GlobalValue::GUID GUID = 0;
8546
14
    if (Lex.getKind() == lltok::SummaryID) {
8547
4
      unsigned ID = Lex.getUIntVal();
8548
4
      LocTy Loc = Lex.getLoc();
8549
4
      // Keep track of the TypeTests array index needing a forward reference.
8550
4
      // We will save the location of the GUID needing an update, but
8551
4
      // can only do so once the std::vector is finalized.
8552
4
      IdToIndexMap[ID].push_back(std::make_pair(TypeTests.size(), Loc));
8553
4
      Lex.Lex();
8554
10
    } else if (ParseUInt64(GUID))
8555
0
      return true;
8556
14
    TypeTests.push_back(GUID);
8557
14
  } while (EatIfPresent(lltok::comma));
8558
11
8559
11
  // Now that the TypeTests vector is finalized, it is safe to save the
8560
11
  // locations of any forward GV references that need updating later.
8561
11
  for (auto I : IdToIndexMap) {
8562
4
    for (auto P : I.second) {
8563
4
      assert(TypeTests[P.first] == 0 &&
8564
4
             "Forward referenced type id GUID expected to be 0");
8565
4
      auto FwdRef = ForwardRefTypeIds.insert(std::make_pair(
8566
4
          I.first, std::vector<std::pair<GlobalValue::GUID *, LocTy>>()));
8567
4
      FwdRef.first->second.push_back(
8568
4
          std::make_pair(&TypeTests[P.first], P.second));
8569
4
    }
8570
4
  }
8571
11
8572
11
  if (ParseToken(lltok::rparen, "expected ')' in typeIdInfo"))
8573
0
    return true;
8574
11
8575
11
  return false;
8576
11
}
8577
8578
/// VFuncIdList
8579
///   ::= Kind ':' '(' VFuncId [',' VFuncId]* ')'
8580
bool LLParser::ParseVFuncIdList(
8581
9
    lltok::Kind Kind, std::vector<FunctionSummary::VFuncId> &VFuncIdList) {
8582
9
  assert(Lex.getKind() == Kind);
8583
9
  Lex.Lex();
8584
9
8585
9
  if (ParseToken(lltok::colon, "expected ':' here") ||
8586
9
      ParseToken(lltok::lparen, "expected '(' here"))
8587
0
    return true;
8588
9
8589
9
  IdToIndexMapType IdToIndexMap;
8590
12
  do {
8591
12
    FunctionSummary::VFuncId VFuncId;
8592
12
    if (ParseVFuncId(VFuncId, IdToIndexMap, VFuncIdList.size()))
8593
0
      return true;
8594
12
    VFuncIdList.push_back(VFuncId);
8595
12
  } while (EatIfPresent(lltok::comma));
8596
9
8597
9
  if (ParseToken(lltok::rparen, "expected ')' here"))
8598
0
    return true;
8599
9
8600
9
  // Now that the VFuncIdList vector is finalized, it is safe to save the
8601
9
  // locations of any forward GV references that need updating later.
8602
9
  for (auto I : IdToIndexMap) {
8603
4
    for (auto P : I.second) {
8604
4
      assert(VFuncIdList[P.first].GUID == 0 &&
8605
4
             "Forward referenced type id GUID expected to be 0");
8606
4
      auto FwdRef = ForwardRefTypeIds.insert(std::make_pair(
8607
4
          I.first, std::vector<std::pair<GlobalValue::GUID *, LocTy>>()));
8608
4
      FwdRef.first->second.push_back(
8609
4
          std::make_pair(&VFuncIdList[P.first].GUID, P.second));
8610
4
    }
8611
3
  }
8612
9
8613
9
  return false;
8614
9
}
8615
8616
/// ConstVCallList
8617
///   ::= Kind ':' '(' ConstVCall [',' ConstVCall]* ')'
8618
bool LLParser::ParseConstVCallList(
8619
    lltok::Kind Kind,
8620
6
    std::vector<FunctionSummary::ConstVCall> &ConstVCallList) {
8621
6
  assert(Lex.getKind() == Kind);
8622
6
  Lex.Lex();
8623
6
8624
6
  if (ParseToken(lltok::colon, "expected ':' here") ||
8625
6
      ParseToken(lltok::lparen, "expected '(' here"))
8626
0
    return true;
8627
6
8628
6
  IdToIndexMapType IdToIndexMap;
8629
9
  do {
8630
9
    FunctionSummary::ConstVCall ConstVCall;
8631
9
    if (ParseConstVCall(ConstVCall, IdToIndexMap, ConstVCallList.size()))
8632
0
      return true;
8633
9
    ConstVCallList.push_back(ConstVCall);
8634
9
  } while (EatIfPresent(lltok::comma));
8635
6
8636
6
  if (ParseToken(lltok::rparen, "expected ')' here"))
8637
0
    return true;
8638
6
8639
6
  // Now that the ConstVCallList vector is finalized, it is safe to save the
8640
6
  // locations of any forward GV references that need updating later.
8641
6
  for (auto I : IdToIndexMap) {
8642
3
    for (auto P : I.second) {
8643
3
      assert(ConstVCallList[P.first].VFunc.GUID == 0 &&
8644
3
             "Forward referenced type id GUID expected to be 0");
8645
3
      auto FwdRef = ForwardRefTypeIds.insert(std::make_pair(
8646
3
          I.first, std::vector<std::pair<GlobalValue::GUID *, LocTy>>()));
8647
3
      FwdRef.first->second.push_back(
8648
3
          std::make_pair(&ConstVCallList[P.first].VFunc.GUID, P.second));
8649
3
    }
8650
2
  }
8651
6
8652
6
  return false;
8653
6
}
8654
8655
/// ConstVCall
8656
///   ::= '(' VFuncId ',' Args ')'
8657
bool LLParser::ParseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
8658
9
                               IdToIndexMapType &IdToIndexMap, unsigned Index) {
8659
9
  if (ParseToken(lltok::lparen, "expected '(' here") ||
8660
9
      ParseVFuncId(ConstVCall.VFunc, IdToIndexMap, Index))
8661
0
    return true;
8662
9
8663
9
  if (EatIfPresent(lltok::comma))
8664
8
    if (ParseArgs(ConstVCall.Args))
8665
0
      return true;
8666
9
8667
9
  if (ParseToken(lltok::rparen, "expected ')' here"))
8668
0
    return true;
8669
9
8670
9
  return false;
8671
9
}
8672
8673
/// VFuncId
8674
///   ::= 'vFuncId' ':' '(' (SummaryID | 'guid' ':' UInt64) ','
8675
///         'offset' ':' UInt64 ')'
8676
bool LLParser::ParseVFuncId(FunctionSummary::VFuncId &VFuncId,
8677
21
                            IdToIndexMapType &IdToIndexMap, unsigned Index) {
8678
21
  assert(Lex.getKind() == lltok::kw_vFuncId);
8679
21
  Lex.Lex();
8680
21
8681
21
  if (ParseToken(lltok::colon, "expected ':' here") ||
8682
21
      ParseToken(lltok::lparen, "expected '(' here"))
8683
0
    return true;
8684
21
8685
21
  if (Lex.getKind() == lltok::SummaryID) {
8686
7
    VFuncId.GUID = 0;
8687
7
    unsigned ID = Lex.getUIntVal();
8688
7
    LocTy Loc = Lex.getLoc();
8689
7
    // Keep track of the array index needing a forward reference.
8690
7
    // We will save the location of the GUID needing an update, but
8691
7
    // can only do so once the caller's std::vector is finalized.
8692
7
    IdToIndexMap[ID].push_back(std::make_pair(Index, Loc));
8693
7
    Lex.Lex();
8694
14
  } else if (ParseToken(lltok::kw_guid, "expected 'guid' here") ||
8695
14
             ParseToken(lltok::colon, "expected ':' here") ||
8696
14
             ParseUInt64(VFuncId.GUID))
8697
0
    return true;
8698
21
8699
21
  if (ParseToken(lltok::comma, "expected ',' here") ||
8700
21
      ParseToken(lltok::kw_offset, "expected 'offset' here") ||
8701
21
      ParseToken(lltok::colon, "expected ':' here") ||
8702
21
      ParseUInt64(VFuncId.Offset) ||
8703
21
      ParseToken(lltok::rparen, "expected ')' here"))
8704
0
    return true;
8705
21
8706
21
  return false;
8707
21
}
8708
8709
/// GVFlags
8710
///   ::= 'flags' ':' '(' 'linkage' ':' OptionalLinkageAux ','
8711
///         'notEligibleToImport' ':' Flag ',' 'live' ':' Flag ','
8712
///         'dsoLocal' ':' Flag ',' 'canAutoHide' ':' Flag ')'
8713
88
bool LLParser::ParseGVFlags(GlobalValueSummary::GVFlags &GVFlags) {
8714
88
  assert(Lex.getKind() == lltok::kw_flags);
8715
88
  Lex.Lex();
8716
88
8717
88
  if (ParseToken(lltok::colon, "expected ':' here") ||
8718
88
      ParseToken(lltok::lparen, "expected '(' here"))
8719
0
    return true;
8720
88
8721
416
  
do 88
{
8722
416
    unsigned Flag = 0;
8723
416
    switch (Lex.getKind()) {
8724
416
    case lltok::kw_linkage:
8725
88
      Lex.Lex();
8726
88
      if (ParseToken(lltok::colon, "expected ':'"))
8727
0
        return true;
8728
88
      bool HasLinkage;
8729
88
      GVFlags.Linkage = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
8730
88
      assert(HasLinkage && "Linkage not optional in summary entry");
8731
88
      Lex.Lex();
8732
88
      break;
8733
88
    case lltok::kw_notEligibleToImport:
8734
88
      Lex.Lex();
8735
88
      if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Flag))
8736
0
        return true;
8737
88
      GVFlags.NotEligibleToImport = Flag;
8738
88
      break;
8739
88
    case lltok::kw_live:
8740
88
      Lex.Lex();
8741
88
      if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Flag))
8742
0
        return true;
8743
88
      GVFlags.Live = Flag;
8744
88
      break;
8745
88
    case lltok::kw_dsoLocal:
8746
88
      Lex.Lex();
8747
88
      if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Flag))
8748
0
        return true;
8749
88
      GVFlags.DSOLocal = Flag;
8750
88
      break;
8751
88
    case lltok::kw_canAutoHide:
8752
64
      Lex.Lex();
8753
64
      if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Flag))
8754
0
        return true;
8755
64
      GVFlags.CanAutoHide = Flag;
8756
64
      break;
8757
64
    default:
8758
0
      return Error(Lex.getLoc(), "expected gv flag type");
8759
416
    }
8760
416
  } while (EatIfPresent(lltok::comma));
8761
88
8762
88
  if (ParseToken(lltok::rparen, "expected ')' here"))
8763
0
    return true;
8764
88
8765
88
  return false;
8766
88
}
8767
8768
/// GVarFlags
8769
///   ::= 'varFlags' ':' '(' 'readonly' ':' Flag
8770
///                      ',' 'writeonly' ':' Flag ')'
8771
16
bool LLParser::ParseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags) {
8772
16
  assert(Lex.getKind() == lltok::kw_varFlags);
8773
16
  Lex.Lex();
8774
16
8775
16
  if (ParseToken(lltok::colon, "expected ':' here") ||
8776
16
      ParseToken(lltok::lparen, "expected '(' here"))
8777
0
    return true;
8778
16
8779
28
  
auto ParseRest = [this](unsigned int &Val) 16
{
8780
28
    Lex.Lex();
8781
28
    if (ParseToken(lltok::colon, "expected ':'"))
8782
0
      return true;
8783
28
    return ParseFlag(Val);
8784
28
  };
8785
16
8786
28
  do {
8787
28
    unsigned Flag = 0;
8788
28
    switch (Lex.getKind()) {
8789
28
    case lltok::kw_readonly:
8790
15
      if (ParseRest(Flag))
8791
0
        return true;
8792
15
      GVarFlags.MaybeReadOnly = Flag;
8793
15
      break;
8794
15
    case lltok::kw_writeonly:
8795
13
      if (ParseRest(Flag))
8796
0
        return true;
8797
13
      GVarFlags.MaybeWriteOnly = Flag;
8798
13
      break;
8799
13
    default:
8800
0
      return Error(Lex.getLoc(), "expected gvar flag type");
8801
28
    }
8802
28
  } while (EatIfPresent(lltok::comma));
8803
16
  return ParseToken(lltok::rparen, "expected ')' here");
8804
16
}
8805
8806
/// ModuleReference
8807
///   ::= 'module' ':' UInt
8808
88
bool LLParser::ParseModuleReference(StringRef &ModulePath) {
8809
88
  // Parse module id.
8810
88
  if (ParseToken(lltok::kw_module, "expected 'module' here") ||
8811
88
      ParseToken(lltok::colon, "expected ':' here") ||
8812
88
      ParseToken(lltok::SummaryID, "expected module ID"))
8813
0
    return true;
8814
88
8815
88
  unsigned ModuleID = Lex.getUIntVal();
8816
88
  auto I = ModuleIdMap.find(ModuleID);
8817
88
  // We should have already parsed all module IDs
8818
88
  assert(I != ModuleIdMap.end());
8819
88
  ModulePath = I->second;
8820
88
  return false;
8821
88
}
8822
8823
/// GVReference
8824
///   ::= SummaryID
8825
82
bool LLParser::ParseGVReference(ValueInfo &VI, unsigned &GVId) {
8826
82
  bool WriteOnly = false, ReadOnly = EatIfPresent(lltok::kw_readonly);
8827
82
  if (!ReadOnly)
8828
80
    WriteOnly = EatIfPresent(lltok::kw_writeonly);
8829
82
  if (ParseToken(lltok::SummaryID, "expected GV ID"))
8830
0
    return true;
8831
82
8832
82
  GVId = Lex.getUIntVal();
8833
82
  // Check if we already have a VI for this GV
8834
82
  if (GVId < NumberedValueInfos.size()) {
8835
51
    assert(NumberedValueInfos[GVId].getRef() != FwdVIRef);
8836
51
    VI = NumberedValueInfos[GVId];
8837
51
  } else
8838
31
    // We will create a forward reference to the stored location.
8839
31
    VI = ValueInfo(false, FwdVIRef);
8840
82
8841
82
  if (ReadOnly)
8842
2
    VI.setReadOnly();
8843
82
  if (WriteOnly)
8844
1
    VI.setWriteOnly();
8845
82
  return false;
8846
82
}