Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 571600 | Differences between
and this patch

Collapse All | Expand All

(-)llvm-3.8.0.src.orig/tools/clang/lib/AST/ItaniumMangle.cpp (-93 / +472 lines)
Lines 212-217 Link Here
212
class CXXNameMangler {
212
class CXXNameMangler {
213
  ItaniumMangleContextImpl &Context;
213
  ItaniumMangleContextImpl &Context;
214
  raw_ostream &Out;
214
  raw_ostream &Out;
215
  bool NullOut = false;
216
  /// In the "DisableDerivedAbiTags" mode derived ABI tags are not calculated.
217
  /// This mode is used when mangler creates another mangler recursively to
218
  /// calculate ABI tags for the function return value or the variable type.
219
  /// Also it is required to avoid infinite recursion in some cases.
220
  bool DisableDerivedAbiTags = false;
215
221
216
  /// The "structor" is the top-level declaration being mangled, if
222
  /// The "structor" is the top-level declaration being mangled, if
217
  /// that's not a template specialization; otherwise it's the pattern
223
  /// that's not a template specialization; otherwise it's the pattern
Lines 261-275 Link Here
261
267
262
  } FunctionTypeDepth;
268
  } FunctionTypeDepth;
263
269
270
  // abi_tag is a gcc attribute, taking one or more strings called "tags".
271
  // The goal is to annotate against which version of a library an object was
272
  // built and to be able to provide backwards compatibility ("dual abi").
273
  // For more information see docs/ItaniumMangleAbiTags.rst.
274
  typedef SmallVector<StringRef, 4> AbiTagList;
275
  typedef llvm::SmallSetVector<StringRef, 4> AbiTagSet;
276
277
  // State to gather all implicit and explicit tags used in a mangled name.
278
  // Must always have an instance of this while emitting any name to keep
279
  // track.
280
  class AbiTagState final {
281
    //! All abi tags used implicitly or explicitly
282
    AbiTagSet UsedAbiTags;
283
    //! All explicit abi tags (i.e. not from namespace)
284
    AbiTagSet EmittedAbiTags;
285
286
    AbiTagState *&LinkHead;
287
    AbiTagState *Parent = nullptr;
288
289
    bool LinkActive = false;
290
291
  public:
292
    explicit AbiTagState(AbiTagState *&Head) : LinkHead(Head) {
293
      Parent = LinkHead;
294
      LinkHead = this;
295
      LinkActive = true;
296
    }
297
298
    // no copy, no move
299
    AbiTagState(const AbiTagState &) = delete;
300
    AbiTagState &operator=(const AbiTagState &) = delete;
301
302
    ~AbiTagState() { pop(); }
303
304
    void pop() {
305
      if (!LinkActive)
306
        return;
307
308
      assert(LinkHead == this &&
309
             "abi tag link head must point to us on destruction");
310
      LinkActive = false;
311
      if (Parent) {
312
        Parent->UsedAbiTags.insert(UsedAbiTags.begin(), UsedAbiTags.end());
313
        Parent->EmittedAbiTags.insert(EmittedAbiTags.begin(),
314
                                      EmittedAbiTags.end());
315
      }
316
      LinkHead = Parent;
317
    }
318
319
    void write(raw_ostream &Out, const NamedDecl *ND,
320
               const AbiTagList *AdditionalAbiTags) {
321
      ND = cast<NamedDecl>(ND->getCanonicalDecl());
322
323
      if (!isa<FunctionDecl>(ND) && !isa<VarDecl>(ND)) {
324
        assert(
325
            !AdditionalAbiTags &&
326
            "only function and variables need a list of additional abi tags");
327
        if (const auto *NS = dyn_cast<NamespaceDecl>(ND)) {
328
          if (const auto *AbiTag = NS->getAttr<AbiTagAttr>()) {
329
            for (const auto &Tag : AbiTag->tags()) {
330
              UsedAbiTags.insert(Tag);
331
            }
332
          }
333
          // Don't emit abi tags for namespaces.
334
          return;
335
        }
336
      }
337
338
      AbiTagList TagList;
339
      if (const auto *AbiTag = ND->getAttr<AbiTagAttr>()) {
340
        for (const auto &Tag : AbiTag->tags()) {
341
          UsedAbiTags.insert(Tag);
342
          // AbiTag->tags() is sorted and has no duplicates
343
          TagList.push_back(Tag);
344
        }
345
      }
346
347
      if (AdditionalAbiTags) {
348
        for (const auto &Tag : *AdditionalAbiTags) {
349
          UsedAbiTags.insert(Tag);
350
          if (std::find(TagList.begin(), TagList.end(), Tag) == TagList.end()) {
351
            // don't insert duplicates
352
            TagList.push_back(Tag);
353
          }
354
        }
355
        // AbiTag->tags() are already sorted; only add if we had additional tags
356
        std::sort(TagList.begin(), TagList.end());
357
      }
358
359
      writeSortedUniqueAbiTags(Out, TagList);
360
    }
361
362
    const AbiTagSet &getUsedAbiTags() const { return UsedAbiTags; }
363
    void setUsedAbiTags(const AbiTagSet &AbiTags) {
364
      UsedAbiTags = AbiTags;
365
    }
366
367
    const AbiTagSet &getEmittedAbiTags() const {
368
      return EmittedAbiTags;
369
    }
370
371
  private:
372
    template <typename TagList>
373
    void writeSortedUniqueAbiTags(raw_ostream &Out, TagList const &AbiTags) {
374
      for (const auto &Tag : AbiTags) {
375
        EmittedAbiTags.insert(Tag);
376
        Out << "B";
377
        Out << Tag.size();
378
        Out << Tag;
379
      }
380
    }
381
  };
382
383
  AbiTagState *AbiTags = nullptr;
384
  AbiTagState AbiTagsRoot;
385
264
  llvm::DenseMap<uintptr_t, unsigned> Substitutions;
386
  llvm::DenseMap<uintptr_t, unsigned> Substitutions;
265
387
266
  ASTContext &getASTContext() const { return Context.getASTContext(); }
388
  ASTContext &getASTContext() const { return Context.getASTContext(); }
267
389
268
public:
390
public:
269
  CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
391
  CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
270
                 const NamedDecl *D = nullptr)
392
                 const NamedDecl *D = nullptr, bool NullOut_ = false)
271
    : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(0),
393
    : Context(C), Out(Out_), NullOut(NullOut_),  Structor(getStructor(D)),
272
      SeqID(0) {
394
      StructorType(0), SeqID(0), AbiTagsRoot(AbiTags) {
273
    // These can't be mangled without a ctor type or dtor type.
395
    // These can't be mangled without a ctor type or dtor type.
274
    assert(!D || (!isa<CXXDestructorDecl>(D) &&
396
    assert(!D || (!isa<CXXDestructorDecl>(D) &&
275
                  !isa<CXXConstructorDecl>(D)));
397
                  !isa<CXXConstructorDecl>(D)));
Lines 277-287 Link Here
277
  CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
399
  CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
278
                 const CXXConstructorDecl *D, CXXCtorType Type)
400
                 const CXXConstructorDecl *D, CXXCtorType Type)
279
    : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
401
    : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
280
      SeqID(0) { }
402
      SeqID(0), AbiTagsRoot(AbiTags) { }
281
  CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
403
  CXXNameMangler(ItaniumMangleContextImpl &C, raw_ostream &Out_,
282
                 const CXXDestructorDecl *D, CXXDtorType Type)
404
                 const CXXDestructorDecl *D, CXXDtorType Type)
283
    : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
405
    : Context(C), Out(Out_), Structor(getStructor(D)), StructorType(Type),
284
      SeqID(0) { }
406
      SeqID(0), AbiTagsRoot(AbiTags) { }
407
408
  CXXNameMangler(CXXNameMangler &Outer, llvm::raw_null_ostream &Out_)
409
      : Context(Outer.Context), Out(Out_), NullOut(true),
410
        Structor(Outer.Structor), StructorType(Outer.StructorType),
411
        SeqID(Outer.SeqID), AbiTagsRoot(AbiTags) {}
285
412
286
#if MANGLE_CHECKER
413
#if MANGLE_CHECKER
287
  ~CXXNameMangler() {
414
  ~CXXNameMangler() {
Lines 296-309 Link Here
296
#endif
423
#endif
297
  raw_ostream &getStream() { return Out; }
424
  raw_ostream &getStream() { return Out; }
298
425
426
  void disableDerivedAbiTags() { DisableDerivedAbiTags = true; }
427
  static bool shouldHaveAbiTags(ItaniumMangleContextImpl &C, const VarDecl *VD);
428
299
  void mangle(const NamedDecl *D);
429
  void mangle(const NamedDecl *D);
300
  void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
430
  void mangleCallOffset(int64_t NonVirtual, int64_t Virtual);
301
  void mangleNumber(const llvm::APSInt &I);
431
  void mangleNumber(const llvm::APSInt &I);
302
  void mangleNumber(int64_t Number);
432
  void mangleNumber(int64_t Number);
303
  void mangleFloat(const llvm::APFloat &F);
433
  void mangleFloat(const llvm::APFloat &F);
304
  void mangleFunctionEncoding(const FunctionDecl *FD);
434
  void mangleFunctionEncoding(const FunctionDecl *FD,
435
                              bool ExcludeUnqualifiedName = false);
305
  void mangleSeqID(unsigned SeqID);
436
  void mangleSeqID(unsigned SeqID);
306
  void mangleName(const NamedDecl *ND);
437
  void mangleName(const NamedDecl *ND, bool ExcludeUnqualifiedName = false);
307
  void mangleType(QualType T);
438
  void mangleType(QualType T);
308
  void mangleNameOrStandardSubstitution(const NamedDecl *ND);
439
  void mangleNameOrStandardSubstitution(const NamedDecl *ND);
309
  
440
  
Lines 334-364 Link Here
334
                            DeclarationName name,
465
                            DeclarationName name,
335
                            unsigned KnownArity = UnknownArity);
466
                            unsigned KnownArity = UnknownArity);
336
467
337
  void mangleName(const TemplateDecl *TD,
468
  void mangleFunctionEncodingBareType(const FunctionDecl *FD);
338
                  const TemplateArgument *TemplateArgs,
469
339
                  unsigned NumTemplateArgs);
470
  void mangleNameWithAbiTags(const NamedDecl *ND,
340
  void mangleUnqualifiedName(const NamedDecl *ND) {
471
                             const AbiTagList *AdditionalAbiTags,
341
    mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity);
472
                             bool ExcludeUnqualifiedName);
473
  void mangleTemplateName(const TemplateDecl *TD,
474
                          const AbiTagList *AdditionalAbiTags,
475
                          bool ExcludeUnqualifiedName,
476
                          const TemplateArgument *TemplateArgs,
477
                          unsigned NumTemplateArgs);
478
  void mangleUnqualifiedName(const NamedDecl *ND,
479
                             const AbiTagList *AdditionalAbiTags) {
480
    mangleUnqualifiedName(ND, ND->getDeclName(), UnknownArity,
481
                          AdditionalAbiTags);
342
  }
482
  }
343
  void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
483
  void mangleUnqualifiedName(const NamedDecl *ND, DeclarationName Name,
344
                             unsigned KnownArity);
484
                             unsigned KnownArity,
345
  void mangleUnscopedName(const NamedDecl *ND);
485
                             const AbiTagList *AdditionalAbiTags);
346
  void mangleUnscopedTemplateName(const TemplateDecl *ND);
486
  void mangleUnscopedName(const NamedDecl *ND,
347
  void mangleUnscopedTemplateName(TemplateName);
487
                          const AbiTagList *AdditionalAbiTags);
488
  void mangleUnscopedTemplateName(const TemplateDecl *ND,
489
                                  const AbiTagList *AdditionalAbiTags);
490
  void mangleUnscopedTemplateName(TemplateName,
491
                                  const AbiTagList *AdditionalAbiTags);
348
  void mangleSourceName(const IdentifierInfo *II);
492
  void mangleSourceName(const IdentifierInfo *II);
349
  void mangleLocalName(const Decl *D);
493
  void mangleLocalName(const Decl *D,
494
                       const AbiTagList *AdditionalAbiTags,
495
                       bool ExcludeUnqualifiedName);
350
  void mangleBlockForPrefix(const BlockDecl *Block);
496
  void mangleBlockForPrefix(const BlockDecl *Block);
351
  void mangleUnqualifiedBlock(const BlockDecl *Block);
497
  void mangleUnqualifiedBlock(const BlockDecl *Block);
352
  void mangleLambda(const CXXRecordDecl *Lambda);
498
  void mangleLambda(const CXXRecordDecl *Lambda);
353
  void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
499
  void mangleNestedName(const NamedDecl *ND, const DeclContext *DC,
354
                        bool NoFunction=false);
500
                        const AbiTagList *AdditionalAbiTags,
501
                        bool NoFunction,
502
                        bool ExcludeUnqualifiedName);
355
  void mangleNestedName(const TemplateDecl *TD,
503
  void mangleNestedName(const TemplateDecl *TD,
504
                        const AbiTagList *AdditionalAbiTags,
505
                        bool ExcludeUnqualifiedName,
356
                        const TemplateArgument *TemplateArgs,
506
                        const TemplateArgument *TemplateArgs,
357
                        unsigned NumTemplateArgs);
507
                        unsigned NumTemplateArgs);
358
  void manglePrefix(NestedNameSpecifier *qualifier);
508
  void manglePrefix(NestedNameSpecifier *qualifier);
359
  void manglePrefix(const DeclContext *DC, bool NoFunction=false);
509
  void manglePrefix(const DeclContext *DC, bool NoFunction=false);
360
  void manglePrefix(QualType type);
510
  void manglePrefix(QualType type);
361
  void mangleTemplatePrefix(const TemplateDecl *ND, bool NoFunction=false);
511
  void mangleTemplatePrefix(const TemplateDecl *ND,
512
                            const AbiTagList *AdditionalAbiTags,
513
                            bool NoFunction = false,
514
                            bool ExcludeUnqualifiedName = false);
362
  void mangleTemplatePrefix(TemplateName Template);
515
  void mangleTemplatePrefix(TemplateName Template);
363
  bool mangleUnresolvedTypeOrSimpleId(QualType DestroyedType,
516
  bool mangleUnresolvedTypeOrSimpleId(QualType DestroyedType,
364
                                      StringRef Prefix = "");
517
                                      StringRef Prefix = "");
Lines 405-410 Link Here
405
  void mangleTemplateParameter(unsigned Index);
558
  void mangleTemplateParameter(unsigned Index);
406
559
407
  void mangleFunctionParam(const ParmVarDecl *parm);
560
  void mangleFunctionParam(const ParmVarDecl *parm);
561
562
  void writeAbiTags(const NamedDecl *ND,
563
                    const AbiTagList *AdditionalAbiTags = nullptr);
564
565
  AbiTagSet getTagsFromPrefixAndTemplateArguments(const NamedDecl *ND);
566
  AbiTagList makeAdditionalTagsForFunction(const FunctionDecl *FD);
567
  AbiTagList makeAdditionalTagsForVariable(const VarDecl *VD);
408
};
568
};
409
569
410
}
570
}
Lines 448-453 Link Here
448
      while (!DC->isNamespace() && !DC->isTranslationUnit())
608
      while (!DC->isNamespace() && !DC->isTranslationUnit())
449
        DC = getEffectiveParentContext(DC);
609
        DC = getEffectiveParentContext(DC);
450
    if (DC->isTranslationUnit() && D->getFormalLinkage() != InternalLinkage &&
610
    if (DC->isTranslationUnit() && D->getFormalLinkage() != InternalLinkage &&
611
        !CXXNameMangler::shouldHaveAbiTags(*this, VD) &&
451
        !isa<VarTemplateSpecializationDecl>(D))
612
        !isa<VarTemplateSpecializationDecl>(D))
452
      return false;
613
      return false;
453
  }
614
  }
Lines 455-460 Link Here
455
  return true;
616
  return true;
456
}
617
}
457
618
619
void CXXNameMangler::writeAbiTags(const NamedDecl *ND,
620
                                  const AbiTagList *AdditionalAbiTags) {
621
  assert(AbiTags && "require AbiTagState");
622
  AbiTags->write(Out, ND, DisableDerivedAbiTags ? nullptr : AdditionalAbiTags);
623
}
624
458
void CXXNameMangler::mangle(const NamedDecl *D) {
625
void CXXNameMangler::mangle(const NamedDecl *D) {
459
  // <mangled-name> ::= _Z <encoding>
626
  // <mangled-name> ::= _Z <encoding>
460
  //            ::= <data name>
627
  //            ::= <data name>
Lines 470-483 Link Here
470
    mangleName(cast<FieldDecl>(D));
637
    mangleName(cast<FieldDecl>(D));
471
}
638
}
472
639
473
void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD) {
640
void CXXNameMangler::mangleFunctionEncoding(const FunctionDecl *FD,
474
  // <encoding> ::= <function name> <bare-function-type>
641
                                            bool ExcludeUnqualifiedName) {
475
  mangleName(FD);
476
477
  // Don't mangle in the type if this isn't a decl we should typically mangle.
642
  // Don't mangle in the type if this isn't a decl we should typically mangle.
478
  if (!Context.shouldMangleDeclName(FD))
643
  if (!Context.shouldMangleDeclName(FD)) {
644
    mangleNameWithAbiTags(FD, /* AdditionalAbiTags */ nullptr,
645
                          ExcludeUnqualifiedName);
479
    return;
646
    return;
647
  }
648
649
  // <encoding> ::= <function name> <bare-function-type>
650
651
  if (ExcludeUnqualifiedName) {
652
    // running makeAdditionalTagsForFunction would loop, don't need it here
653
    // anyway
654
    mangleNameWithAbiTags(FD, /* AdditionalAbiTags */ nullptr,
655
                          ExcludeUnqualifiedName);
656
  } else {
657
    AbiTagList AdditionalAbiTags = makeAdditionalTagsForFunction(FD);
658
    mangleNameWithAbiTags(FD, &AdditionalAbiTags, ExcludeUnqualifiedName);
659
  }
480
660
661
  mangleFunctionEncodingBareType(FD);
662
}
663
664
void CXXNameMangler::mangleFunctionEncodingBareType(const FunctionDecl *FD) {
481
  if (FD->hasAttr<EnableIfAttr>()) {
665
  if (FD->hasAttr<EnableIfAttr>()) {
482
    FunctionTypeDepthState Saved = FunctionTypeDepth.push();
666
    FunctionTypeDepthState Saved = FunctionTypeDepth.push();
483
    Out << "Ua9enable_ifI";
667
    Out << "Ua9enable_ifI";
Lines 581-587 Link Here
581
  return nullptr;
765
  return nullptr;
582
}
766
}
583
767
584
void CXXNameMangler::mangleName(const NamedDecl *ND) {
768
// Must not be run from mangleLocalName for the <entity name> as it would loop
769
// otherwise.
770
void CXXNameMangler::mangleName(const NamedDecl *ND,
771
                                bool ExcludeUnqualifiedName) {
772
  if (!ExcludeUnqualifiedName) {
773
    if (const auto *VD = dyn_cast<VarDecl>(ND)) {
774
      AbiTagList VariableAdditionalAbiTags = makeAdditionalTagsForVariable(VD);
775
      mangleNameWithAbiTags(VD, &VariableAdditionalAbiTags,
776
                            ExcludeUnqualifiedName);
777
      return;
778
    }
779
  }
780
  mangleNameWithAbiTags(ND, nullptr, ExcludeUnqualifiedName);
781
}
782
783
void CXXNameMangler::mangleNameWithAbiTags(const NamedDecl *ND,
784
                                           const AbiTagList *AdditionalAbiTags,
785
                                           bool ExcludeUnqualifiedName) {
585
  //  <name> ::= <nested-name>
786
  //  <name> ::= <nested-name>
586
  //         ::= <unscoped-name>
787
  //         ::= <unscoped-name>
587
  //         ::= <unscoped-template-name> <template-args>
788
  //         ::= <unscoped-template-name> <template-args>
Lines 597-603 Link Here
597
    while (!DC->isNamespace() && !DC->isTranslationUnit())
798
    while (!DC->isNamespace() && !DC->isTranslationUnit())
598
      DC = getEffectiveParentContext(DC);
799
      DC = getEffectiveParentContext(DC);
599
  else if (GetLocalClassDecl(ND)) {
800
  else if (GetLocalClassDecl(ND)) {
600
    mangleLocalName(ND);
801
    mangleLocalName(ND, AdditionalAbiTags, ExcludeUnqualifiedName);
601
    return;
802
    return;
602
  }
803
  }
603
804
Lines 607-682 Link Here
607
    // Check if we have a template.
808
    // Check if we have a template.
608
    const TemplateArgumentList *TemplateArgs = nullptr;
809
    const TemplateArgumentList *TemplateArgs = nullptr;
609
    if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
810
    if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
610
      mangleUnscopedTemplateName(TD);
811
      if (!ExcludeUnqualifiedName)
812
        mangleUnscopedTemplateName(TD, AdditionalAbiTags);
611
      mangleTemplateArgs(*TemplateArgs);
813
      mangleTemplateArgs(*TemplateArgs);
612
      return;
814
      return;
613
    }
815
    }
614
816
615
    mangleUnscopedName(ND);
817
    if (!ExcludeUnqualifiedName)
818
      mangleUnscopedName(ND, AdditionalAbiTags);
616
    return;
819
    return;
617
  }
820
  }
618
821
619
  if (isLocalContainerContext(DC)) {
822
  if (isLocalContainerContext(DC)) {
620
    mangleLocalName(ND);
823
    mangleLocalName(ND, AdditionalAbiTags, ExcludeUnqualifiedName);
621
    return;
824
    return;
622
  }
825
  }
623
826
624
  mangleNestedName(ND, DC);
827
  mangleNestedName(ND, DC, AdditionalAbiTags, /* NoFunction */ false,
828
                   ExcludeUnqualifiedName);
625
}
829
}
626
void CXXNameMangler::mangleName(const TemplateDecl *TD,
830
627
                                const TemplateArgument *TemplateArgs,
831
void CXXNameMangler::mangleTemplateName(const TemplateDecl *TD,
628
                                unsigned NumTemplateArgs) {
832
                                        const AbiTagList *AdditionalAbiTags,
833
                                        bool ExcludeUnqualifiedName,
834
                                        const TemplateArgument *TemplateArgs,
835
                                        unsigned NumTemplateArgs) {
629
  const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
836
  const DeclContext *DC = IgnoreLinkageSpecDecls(getEffectiveDeclContext(TD));
630
837
631
  if (DC->isTranslationUnit() || isStdNamespace(DC)) {
838
  if (DC->isTranslationUnit() || isStdNamespace(DC)) {
632
    mangleUnscopedTemplateName(TD);
839
    if (!ExcludeUnqualifiedName)
840
      mangleUnscopedTemplateName(TD, AdditionalAbiTags);
633
    mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
841
    mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
634
  } else {
842
  } else {
635
    mangleNestedName(TD, TemplateArgs, NumTemplateArgs);
843
    mangleNestedName(TD, AdditionalAbiTags, ExcludeUnqualifiedName,
844
                     TemplateArgs, NumTemplateArgs);
636
  }
845
  }
637
}
846
}
638
847
639
void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND) {
848
void CXXNameMangler::mangleUnscopedName(const NamedDecl *ND,
849
                                        const AbiTagList *AdditionalAbiTags) {
640
  //  <unscoped-name> ::= <unqualified-name>
850
  //  <unscoped-name> ::= <unqualified-name>
641
  //                  ::= St <unqualified-name>   # ::std::
851
  //                  ::= St <unqualified-name>   # ::std::
642
852
643
  if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND))))
853
  if (isStdNamespace(IgnoreLinkageSpecDecls(getEffectiveDeclContext(ND))))
644
    Out << "St";
854
    Out << "St";
645
855
646
  mangleUnqualifiedName(ND);
856
  mangleUnqualifiedName(ND, AdditionalAbiTags);
647
}
857
}
648
858
649
void CXXNameMangler::mangleUnscopedTemplateName(const TemplateDecl *ND) {
859
void CXXNameMangler::mangleUnscopedTemplateName(
860
    const TemplateDecl *ND, const AbiTagList *AdditionalAbiTags) {
650
  //     <unscoped-template-name> ::= <unscoped-name>
861
  //     <unscoped-template-name> ::= <unscoped-name>
651
  //                              ::= <substitution>
862
  //                              ::= <substitution>
652
  if (mangleSubstitution(ND))
863
  if (mangleSubstitution(ND))
653
    return;
864
    return;
654
865
655
  // <template-template-param> ::= <template-param>
866
  // <template-template-param> ::= <template-param>
656
  if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(ND))
867
  if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(ND)) {
868
    assert(!AdditionalAbiTags &&
869
           "template template param cannot have abi tags");
657
    mangleTemplateParameter(TTP->getIndex());
870
    mangleTemplateParameter(TTP->getIndex());
658
  else
871
  } else {
659
    mangleUnscopedName(ND->getTemplatedDecl());
872
    mangleUnscopedName(ND->getTemplatedDecl(), AdditionalAbiTags);
873
  }
660
874
661
  addSubstitution(ND);
875
  addSubstitution(ND);
662
}
876
}
663
877
664
void CXXNameMangler::mangleUnscopedTemplateName(TemplateName Template) {
878
void CXXNameMangler::mangleUnscopedTemplateName(
879
    TemplateName Template, const AbiTagList *AdditionalAbiTags) {
665
  //     <unscoped-template-name> ::= <unscoped-name>
880
  //     <unscoped-template-name> ::= <unscoped-name>
666
  //                              ::= <substitution>
881
  //                              ::= <substitution>
667
  if (TemplateDecl *TD = Template.getAsTemplateDecl())
882
  if (TemplateDecl *TD = Template.getAsTemplateDecl())
668
    return mangleUnscopedTemplateName(TD);
883
    return mangleUnscopedTemplateName(TD, AdditionalAbiTags);
669
  
884
  
670
  if (mangleSubstitution(Template))
885
  if (mangleSubstitution(Template))
671
    return;
886
    return;
672
887
888
  assert(!AdditionalAbiTags &&
889
         "dependent template name cannot have abi tags");
890
673
  DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
891
  DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
674
  assert(Dependent && "Not a dependent template name?");
892
  assert(Dependent && "Not a dependent template name?");
675
  if (const IdentifierInfo *Id = Dependent->getIdentifier())
893
  if (const IdentifierInfo *Id = Dependent->getIdentifier())
676
    mangleSourceName(Id);
894
    mangleSourceName(Id);
677
  else
895
  else
678
    mangleOperatorName(Dependent->getOperator(), UnknownArity);
896
    mangleOperatorName(Dependent->getOperator(), UnknownArity);
679
  
897
680
  addSubstitution(Template);
898
  addSubstitution(Template);
681
}
899
}
682
900
Lines 835-840 Link Here
835
    else
1053
    else
836
      Out << "sr";
1054
      Out << "sr";
837
    mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
1055
    mangleSourceName(qualifier->getAsNamespace()->getIdentifier());
1056
    writeAbiTags(qualifier->getAsNamespace());
838
    break;
1057
    break;
839
  case NestedNameSpecifier::NamespaceAlias:
1058
  case NestedNameSpecifier::NamespaceAlias:
840
    if (qualifier->getPrefix())
1059
    if (qualifier->getPrefix())
Lines 843-848 Link Here
843
    else
1062
    else
844
      Out << "sr";
1063
      Out << "sr";
845
    mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
1064
    mangleSourceName(qualifier->getAsNamespaceAlias()->getIdentifier());
1065
    writeAbiTags(qualifier->getAsNamespaceAlias());
846
    break;
1066
    break;
847
1067
848
  case NestedNameSpecifier::TypeSpec:
1068
  case NestedNameSpecifier::TypeSpec:
Lines 877-882 Link Here
877
      Out << "sr";
1097
      Out << "sr";
878
1098
879
    mangleSourceName(qualifier->getAsIdentifier());
1099
    mangleSourceName(qualifier->getAsIdentifier());
1100
    // an Identifier has no type information, so we can't emit abi tags for it
880
    break;
1101
    break;
881
  }
1102
  }
882
1103
Lines 922-928 Link Here
922
1143
923
void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
1144
void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
924
                                           DeclarationName Name,
1145
                                           DeclarationName Name,
925
                                           unsigned KnownArity) {
1146
                                           unsigned KnownArity,
1147
                                           const AbiTagList *AdditionalAbiTags) {
926
  unsigned Arity = KnownArity;
1148
  unsigned Arity = KnownArity;
927
  //  <unqualified-name> ::= <operator-name>
1149
  //  <unqualified-name> ::= <operator-name>
928
  //                     ::= <ctor-dtor-name>
1150
  //                     ::= <ctor-dtor-name>
Lines 941-946 Link Here
941
        Out << 'L';
1163
        Out << 'L';
942
1164
943
      mangleSourceName(II);
1165
      mangleSourceName(II);
1166
      writeAbiTags(ND, AdditionalAbiTags);
944
      break;
1167
      break;
945
    }
1168
    }
946
1169
Lines 980-985 Link Here
980
      assert(FD->getIdentifier() && "Data member name isn't an identifier!");
1203
      assert(FD->getIdentifier() && "Data member name isn't an identifier!");
981
1204
982
      mangleSourceName(FD->getIdentifier());
1205
      mangleSourceName(FD->getIdentifier());
1206
      // Not emitting abi tags: internal name anyway
983
      break;
1207
      break;
984
    }
1208
    }
985
1209
Lines 1000-1005 Link Here
1000
      assert(D->getDeclName().getAsIdentifierInfo() &&
1224
      assert(D->getDeclName().getAsIdentifierInfo() &&
1001
             "Typedef was not named!");
1225
             "Typedef was not named!");
1002
      mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1226
      mangleSourceName(D->getDeclName().getAsIdentifierInfo());
1227
      assert(!AdditionalAbiTags && "Type cannot have additional abi tags");
1228
      // explicit abi tags are still possible; take from underlying type, not
1229
      // from typedef.
1230
      writeAbiTags(TD, nullptr);
1003
      break;
1231
      break;
1004
    }
1232
    }
1005
1233
Lines 1009-1014 Link Here
1009
    // <lambda-sig> ::= <parameter-type>+   # Parameter types or 'v' for 'void'.
1237
    // <lambda-sig> ::= <parameter-type>+   # Parameter types or 'v' for 'void'.
1010
    if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
1238
    if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(TD)) {
1011
      if (Record->isLambda() && Record->getLambdaManglingNumber()) {
1239
      if (Record->isLambda() && Record->getLambdaManglingNumber()) {
1240
        assert(!AdditionalAbiTags &&
1241
               "Lambda type cannot have additional abi tags");
1012
        mangleLambda(Record);
1242
        mangleLambda(Record);
1013
        break;
1243
        break;
1014
      }
1244
      }
Lines 1020-1030 Link Here
1020
      if (UnnamedMangle > 1)
1250
      if (UnnamedMangle > 1)
1021
        Out << UnnamedMangle - 2;
1251
        Out << UnnamedMangle - 2;
1022
      Out << '_';
1252
      Out << '_';
1253
      writeAbiTags(TD, AdditionalAbiTags);
1023
      break;
1254
      break;
1024
    }
1255
    }
1025
1256
1026
    // Get a unique id for the anonymous struct.
1257
    // Get a unique id for the anonymous struct. If it is not a real output
1027
    unsigned AnonStructId = Context.getAnonymousStructId(TD);
1258
    // ID doesn't matter so use fake one.
1259
    unsigned AnonStructId = NullOut ? 0 : Context.getAnonymousStructId(TD);
1028
1260
1029
    // Mangle it as a source name in the form
1261
    // Mangle it as a source name in the form
1030
    // [n] $_<id>
1262
    // [n] $_<id>
Lines 1052-1057 Link Here
1052
      // Otherwise, use the complete constructor name. This is relevant if a
1284
      // Otherwise, use the complete constructor name. This is relevant if a
1053
      // class with a constructor is declared within a constructor.
1285
      // class with a constructor is declared within a constructor.
1054
      mangleCXXCtorType(Ctor_Complete);
1286
      mangleCXXCtorType(Ctor_Complete);
1287
    writeAbiTags(ND, AdditionalAbiTags);
1055
    break;
1288
    break;
1056
1289
1057
  case DeclarationName::CXXDestructorName:
1290
  case DeclarationName::CXXDestructorName:
Lines 1063-1068 Link Here
1063
      // Otherwise, use the complete destructor name. This is relevant if a
1296
      // Otherwise, use the complete destructor name. This is relevant if a
1064
      // class with a destructor is declared within a destructor.
1297
      // class with a destructor is declared within a destructor.
1065
      mangleCXXDtorType(Dtor_Complete);
1298
      mangleCXXDtorType(Dtor_Complete);
1299
    writeAbiTags(ND, AdditionalAbiTags);
1066
    break;
1300
    break;
1067
1301
1068
  case DeclarationName::CXXOperatorName:
1302
  case DeclarationName::CXXOperatorName:
Lines 1078-1083 Link Here
1078
  case DeclarationName::CXXConversionFunctionName:
1312
  case DeclarationName::CXXConversionFunctionName:
1079
  case DeclarationName::CXXLiteralOperatorName:
1313
  case DeclarationName::CXXLiteralOperatorName:
1080
    mangleOperatorName(Name, Arity);
1314
    mangleOperatorName(Name, Arity);
1315
    writeAbiTags(ND, AdditionalAbiTags);
1081
    break;
1316
    break;
1082
1317
1083
  case DeclarationName::CXXUsingDirective:
1318
  case DeclarationName::CXXUsingDirective:
Lines 1094-1100 Link Here
1094
1329
1095
void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
1330
void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
1096
                                      const DeclContext *DC,
1331
                                      const DeclContext *DC,
1097
                                      bool NoFunction) {
1332
                                      const AbiTagList *AdditionalAbiTags,
1333
                                      bool NoFunction,
1334
                                      bool ExcludeUnqualifiedName) {
1098
  // <nested-name> 
1335
  // <nested-name> 
1099
  //   ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1336
  //   ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
1100
  //   ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> 
1337
  //   ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> 
Lines 1114-1143 Link Here
1114
  // Check if we have a template.
1351
  // Check if we have a template.
1115
  const TemplateArgumentList *TemplateArgs = nullptr;
1352
  const TemplateArgumentList *TemplateArgs = nullptr;
1116
  if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
1353
  if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
1117
    mangleTemplatePrefix(TD, NoFunction);
1354
    mangleTemplatePrefix(TD, AdditionalAbiTags, NoFunction,
1355
                         ExcludeUnqualifiedName);
1118
    mangleTemplateArgs(*TemplateArgs);
1356
    mangleTemplateArgs(*TemplateArgs);
1119
  }
1357
  }
1120
  else {
1358
  else {
1121
    manglePrefix(DC, NoFunction);
1359
    manglePrefix(DC, NoFunction);
1122
    mangleUnqualifiedName(ND);
1360
    if (!ExcludeUnqualifiedName)
1361
      mangleUnqualifiedName(ND, AdditionalAbiTags);
1123
  }
1362
  }
1124
1363
1125
  Out << 'E';
1364
  Out << 'E';
1126
}
1365
}
1127
void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
1366
void CXXNameMangler::mangleNestedName(const TemplateDecl *TD,
1367
                                      const AbiTagList *AdditionalAbiTags,
1368
                                      bool ExcludeUnqualifiedName,
1128
                                      const TemplateArgument *TemplateArgs,
1369
                                      const TemplateArgument *TemplateArgs,
1129
                                      unsigned NumTemplateArgs) {
1370
                                      unsigned NumTemplateArgs) {
1130
  // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1371
  // <nested-name> ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
1131
1372
1132
  Out << 'N';
1373
  Out << 'N';
1133
1374
1134
  mangleTemplatePrefix(TD);
1375
  mangleTemplatePrefix(TD, AdditionalAbiTags, ExcludeUnqualifiedName);
1135
  mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
1376
  mangleTemplateArgs(TemplateArgs, NumTemplateArgs);
1136
1377
1137
  Out << 'E';
1378
  Out << 'E';
1138
}
1379
}
1139
1380
1140
void CXXNameMangler::mangleLocalName(const Decl *D) {
1381
void CXXNameMangler::mangleLocalName(const Decl *D,
1382
                                     const AbiTagList *AdditionalAbiTags,
1383
                                     bool ExcludeUnqualifiedName) {
1141
  // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1384
  // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
1142
  //              := Z <function encoding> E s [<discriminator>]
1385
  //              := Z <function encoding> E s [<discriminator>]
1143
  // <local-name> := Z <function encoding> E d [ <parameter number> ] 
1386
  // <local-name> := Z <function encoding> E d [ <parameter number> ] 
Lines 1149-1163 Link Here
1149
1392
1150
  Out << 'Z';
1393
  Out << 'Z';
1151
1394
1152
  if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC))
1395
  {
1153
    mangleObjCMethodName(MD);
1396
    AbiTagState LocalAbiTags(AbiTags);
1154
  else if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC))
1397
1155
    mangleBlockForPrefix(BD);
1398
    if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(DC))
1156
  else
1399
      mangleObjCMethodName(MD);
1157
    mangleFunctionEncoding(cast<FunctionDecl>(DC));
1400
    else if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC))
1401
      mangleBlockForPrefix(BD);
1402
    else
1403
      mangleFunctionEncoding(cast<FunctionDecl>(DC));
1404
1405
    // Implicit ABI tags (from namespace) are not available in the following
1406
    // entity; reset to actually emitted tags, which are available.
1407
    LocalAbiTags.setUsedAbiTags(LocalAbiTags.getEmittedAbiTags());
1408
  }
1158
1409
1159
  Out << 'E';
1410
  Out << 'E';
1160
1411
1412
  // GCC 5.3.0 doesn't emit derived ABI tags for local names but that seems to
1413
  // be a bug that is fixed in trunk.
1414
1161
  if (RD) {
1415
  if (RD) {
1162
    // The parameter number is omitted for the last parameter, 0 for the 
1416
    // The parameter number is omitted for the last parameter, 0 for the 
1163
    // second-to-last parameter, 1 for the third-to-last parameter, etc. The 
1417
    // second-to-last parameter, 1 for the third-to-last parameter, etc. The 
Lines 1182-1194 Link Here
1182
    // Mangle the name relative to the closest enclosing function.
1436
    // Mangle the name relative to the closest enclosing function.
1183
    // equality ok because RD derived from ND above
1437
    // equality ok because RD derived from ND above
1184
    if (D == RD)  {
1438
    if (D == RD)  {
1185
      mangleUnqualifiedName(RD);
1439
      if (!ExcludeUnqualifiedName)
1440
        mangleUnqualifiedName(RD, AdditionalAbiTags);
1186
    } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1441
    } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1187
      manglePrefix(getEffectiveDeclContext(BD), true /*NoFunction*/);
1442
      manglePrefix(getEffectiveDeclContext(BD), true /*NoFunction*/);
1188
      mangleUnqualifiedBlock(BD);
1443
      assert(!AdditionalAbiTags && "Block cannot have additional abi tags");
1444
      if (!ExcludeUnqualifiedName)
1445
        mangleUnqualifiedBlock(BD);
1189
    } else {
1446
    } else {
1190
      const NamedDecl *ND = cast<NamedDecl>(D);
1447
      const NamedDecl *ND = cast<NamedDecl>(D);
1191
      mangleNestedName(ND, getEffectiveDeclContext(ND), true /*NoFunction*/);
1448
      mangleNestedName(ND, getEffectiveDeclContext(ND), AdditionalAbiTags,
1449
                       true /*NoFunction*/, ExcludeUnqualifiedName);
1192
    }
1450
    }
1193
  } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1451
  } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1194
    // Mangle a block in a default parameter; see above explanation for
1452
    // Mangle a block in a default parameter; see above explanation for
Lines 1205-1234 Link Here
1205
      }
1463
      }
1206
    }
1464
    }
1207
1465
1208
    mangleUnqualifiedBlock(BD);
1466
    assert(!AdditionalAbiTags && "Block cannot have additional abi tags");
1467
    if (!ExcludeUnqualifiedName)
1468
      mangleUnqualifiedBlock(BD);
1209
  } else {
1469
  } else {
1210
    mangleUnqualifiedName(cast<NamedDecl>(D));
1470
    if (!ExcludeUnqualifiedName)
1471
      mangleUnqualifiedName(cast<NamedDecl>(D), AdditionalAbiTags);
1211
  }
1472
  }
1212
1473
1213
  if (const NamedDecl *ND = dyn_cast<NamedDecl>(RD ? RD : D)) {
1474
  if (!ExcludeUnqualifiedName) {
1214
    unsigned disc;
1475
    if (const NamedDecl *ND = dyn_cast<NamedDecl>(RD ? RD : D)) {
1215
    if (Context.getNextDiscriminator(ND, disc)) {
1476
      unsigned disc;
1216
      if (disc < 10)
1477
      if (Context.getNextDiscriminator(ND, disc)) {
1217
        Out << '_' << disc;
1478
        if (disc < 10)
1218
      else
1479
          Out << '_' << disc;
1219
        Out << "__" << disc << '_';
1480
        else
1481
          Out << "__" << disc << '_';
1482
      }
1220
    }
1483
    }
1221
  }
1484
  }
1222
}
1485
}
1223
1486
1224
void CXXNameMangler::mangleBlockForPrefix(const BlockDecl *Block) {
1487
void CXXNameMangler::mangleBlockForPrefix(const BlockDecl *Block) {
1225
  if (GetLocalClassDecl(Block)) {
1488
  if (GetLocalClassDecl(Block)) {
1226
    mangleLocalName(Block);
1489
    mangleLocalName(Block, /* AdditionalAbiTags */ nullptr,
1490
                    /* ExcludeUnqualifiedName */ false);
1227
    return;
1491
    return;
1228
  }
1492
  }
1229
  const DeclContext *DC = getEffectiveDeclContext(Block);
1493
  const DeclContext *DC = getEffectiveDeclContext(Block);
1230
  if (isLocalContainerContext(DC)) {
1494
  if (isLocalContainerContext(DC)) {
1231
    mangleLocalName(Block);
1495
    mangleLocalName(Block, /* AdditionalAbiTags */ nullptr,
1496
                    /* ExcludeUnqualifiedName */ false);
1232
    return;
1497
    return;
1233
  }
1498
  }
1234
  manglePrefix(getEffectiveDeclContext(Block));
1499
  manglePrefix(getEffectiveDeclContext(Block));
Lines 1239-1248 Link Here
1239
  if (Decl *Context = Block->getBlockManglingContextDecl()) {
1504
  if (Decl *Context = Block->getBlockManglingContextDecl()) {
1240
    if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1505
    if ((isa<VarDecl>(Context) || isa<FieldDecl>(Context)) &&
1241
        Context->getDeclContext()->isRecord()) {
1506
        Context->getDeclContext()->isRecord()) {
1242
      if (const IdentifierInfo *Name
1507
      const auto *ND = cast<NamedDecl>(Context);
1243
            = cast<NamedDecl>(Context)->getIdentifier()) {
1508
      if (const IdentifierInfo *Name = ND->getIdentifier()) {
1244
        mangleSourceName(Name);
1509
        mangleSourceName(Name);
1245
        Out << 'M';            
1510
        writeAbiTags(ND, /* AdditionalAbiTags */ nullptr);
1511
        Out << 'M';
1246
      }
1512
      }
1247
    }
1513
    }
1248
  }
1514
  }
Lines 1275-1281 Link Here
1275
      if (const IdentifierInfo *Name
1541
      if (const IdentifierInfo *Name
1276
            = cast<NamedDecl>(Context)->getIdentifier()) {
1542
            = cast<NamedDecl>(Context)->getIdentifier()) {
1277
        mangleSourceName(Name);
1543
        mangleSourceName(Name);
1278
        Out << 'M';            
1544
        Out << 'M';
1279
      }
1545
      }
1280
    }
1546
    }
1281
  }
1547
  }
Lines 1358-1368 Link Here
1358
  // Check if we have a template.
1624
  // Check if we have a template.
1359
  const TemplateArgumentList *TemplateArgs = nullptr;
1625
  const TemplateArgumentList *TemplateArgs = nullptr;
1360
  if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
1626
  if (const TemplateDecl *TD = isTemplate(ND, TemplateArgs)) {
1361
    mangleTemplatePrefix(TD);
1627
    mangleTemplatePrefix(TD, /* AdditionalAbiTags */ nullptr);
1362
    mangleTemplateArgs(*TemplateArgs);
1628
    mangleTemplateArgs(*TemplateArgs);
1363
  } else {
1629
  } else {
1364
    manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1630
    manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1365
    mangleUnqualifiedName(ND);
1631
    mangleUnqualifiedName(ND, /* AdditionalAbiTags */ nullptr);
1366
  }
1632
  }
1367
1633
1368
  addSubstitution(ND);
1634
  addSubstitution(ND);
Lines 1373-1399 Link Here
1373
  //                   ::= <template-param>
1639
  //                   ::= <template-param>
1374
  //                   ::= <substitution>
1640
  //                   ::= <substitution>
1375
  if (TemplateDecl *TD = Template.getAsTemplateDecl())
1641
  if (TemplateDecl *TD = Template.getAsTemplateDecl())
1376
    return mangleTemplatePrefix(TD);
1642
    return mangleTemplatePrefix(TD, /* AdditionalAbiTags */ nullptr);
1377
1643
1378
  if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
1644
  if (QualifiedTemplateName *Qualified = Template.getAsQualifiedTemplateName())
1379
    manglePrefix(Qualified->getQualifier());
1645
    manglePrefix(Qualified->getQualifier());
1380
  
1646
1381
  if (OverloadedTemplateStorage *Overloaded
1647
  if (OverloadedTemplateStorage *Overloaded
1382
                                      = Template.getAsOverloadedTemplate()) {
1648
                                      = Template.getAsOverloadedTemplate()) {
1383
    mangleUnqualifiedName(nullptr, (*Overloaded->begin())->getDeclName(),
1649
    mangleUnqualifiedName(nullptr, (*Overloaded->begin())->getDeclName(),
1384
                          UnknownArity);
1650
                          UnknownArity,
1651
                          /* AdditionalAbiTags */ nullptr);
1385
    return;
1652
    return;
1386
  }
1653
  }
1387
   
1654
1388
  DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1655
  DependentTemplateName *Dependent = Template.getAsDependentTemplateName();
1389
  assert(Dependent && "Unknown template name kind?");
1656
  assert(Dependent && "Unknown template name kind?");
1390
  if (NestedNameSpecifier *Qualifier = Dependent->getQualifier())
1657
  if (NestedNameSpecifier *Qualifier = Dependent->getQualifier())
1391
    manglePrefix(Qualifier);
1658
    manglePrefix(Qualifier);
1392
  mangleUnscopedTemplateName(Template);
1659
  mangleUnscopedTemplateName(Template, /* AdditionalAbiTags */ nullptr);
1393
}
1660
}
1394
1661
1395
void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND,
1662
void CXXNameMangler::mangleTemplatePrefix(const TemplateDecl *ND,
1396
                                          bool NoFunction) {
1663
                                          const AbiTagList *AdditionalAbiTags,
1664
                                          bool NoFunction,
1665
                                          bool ExcludeUnqualifiedName) {
1397
  // <template-prefix> ::= <prefix> <template unqualified-name>
1666
  // <template-prefix> ::= <prefix> <template unqualified-name>
1398
  //                   ::= <template-param>
1667
  //                   ::= <template-param>
1399
  //                   ::= <substitution>
1668
  //                   ::= <substitution>
Lines 1408-1414 Link Here
1408
    mangleTemplateParameter(TTP->getIndex());
1677
    mangleTemplateParameter(TTP->getIndex());
1409
  } else {
1678
  } else {
1410
    manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1679
    manglePrefix(getEffectiveDeclContext(ND), NoFunction);
1411
    mangleUnqualifiedName(ND->getTemplatedDecl());
1680
    if (!ExcludeUnqualifiedName)
1681
      mangleUnqualifiedName(ND->getTemplatedDecl(), AdditionalAbiTags);
1412
  }
1682
  }
1413
1683
1414
  addSubstitution(ND);
1684
  addSubstitution(ND);
Lines 1452-1457 Link Here
1452
    // <name> ::= <nested-name>
1722
    // <name> ::= <nested-name>
1453
    mangleUnresolvedPrefix(Dependent->getQualifier());
1723
    mangleUnresolvedPrefix(Dependent->getQualifier());
1454
    mangleSourceName(Dependent->getIdentifier());
1724
    mangleSourceName(Dependent->getIdentifier());
1725
     // writeAbiTags(Dependent);
1455
    break;
1726
    break;
1456
  }
1727
  }
1457
1728
Lines 1544-1559 Link Here
1544
1815
1545
  case Type::Typedef:
1816
  case Type::Typedef:
1546
    mangleSourceName(cast<TypedefType>(Ty)->getDecl()->getIdentifier());
1817
    mangleSourceName(cast<TypedefType>(Ty)->getDecl()->getIdentifier());
1818
    writeAbiTags(cast<TypedefType>(Ty)->getDecl());
1547
    break;
1819
    break;
1548
1820
1549
  case Type::UnresolvedUsing:
1821
  case Type::UnresolvedUsing:
1550
    mangleSourceName(
1822
    mangleSourceName(
1551
        cast<UnresolvedUsingType>(Ty)->getDecl()->getIdentifier());
1823
        cast<UnresolvedUsingType>(Ty)->getDecl()->getIdentifier());
1824
    writeAbiTags(cast<UnresolvedUsingType>(Ty)->getDecl());
1552
    break;
1825
    break;
1553
1826
1554
  case Type::Enum:
1827
  case Type::Enum:
1555
  case Type::Record:
1828
  case Type::Record:
1556
    mangleSourceName(cast<TagType>(Ty)->getDecl()->getIdentifier());
1829
    mangleSourceName(cast<TagType>(Ty)->getDecl()->getIdentifier());
1830
    writeAbiTags(cast<TagType>(Ty)->getDecl());
1557
    break;
1831
    break;
1558
1832
1559
  case Type::TemplateSpecialization: {
1833
  case Type::TemplateSpecialization: {
Lines 1572-1577 Link Here
1572
        goto unresolvedType;
1846
        goto unresolvedType;
1573
1847
1574
      mangleSourceName(TD->getIdentifier());
1848
      mangleSourceName(TD->getIdentifier());
1849
      writeAbiTags(TD);
1575
      break;
1850
      break;
1576
    }
1851
    }
1577
1852
Lines 1603-1618 Link Here
1603
  case Type::InjectedClassName:
1878
  case Type::InjectedClassName:
1604
    mangleSourceName(
1879
    mangleSourceName(
1605
        cast<InjectedClassNameType>(Ty)->getDecl()->getIdentifier());
1880
        cast<InjectedClassNameType>(Ty)->getDecl()->getIdentifier());
1881
    writeAbiTags(cast<InjectedClassNameType>(Ty)->getDecl());
1606
    break;
1882
    break;
1607
1883
1608
  case Type::DependentName:
1884
  case Type::DependentName:
1609
    mangleSourceName(cast<DependentNameType>(Ty)->getIdentifier());
1885
    mangleSourceName(cast<DependentNameType>(Ty)->getIdentifier());
1886
    // writeAbiTags(cast<DependentNameType>(Ty));
1610
    break;
1887
    break;
1611
1888
1612
  case Type::DependentTemplateSpecialization: {
1889
  case Type::DependentTemplateSpecialization: {
1613
    const DependentTemplateSpecializationType *DTST =
1890
    const DependentTemplateSpecializationType *DTST =
1614
        cast<DependentTemplateSpecializationType>(Ty);
1891
        cast<DependentTemplateSpecializationType>(Ty);
1615
    mangleSourceName(DTST->getIdentifier());
1892
    mangleSourceName(DTST->getIdentifier());
1893
    // writeAbiTags(DTST);
1616
    mangleTemplateArgs(DTST->getArgs(), DTST->getNumArgs());
1894
    mangleTemplateArgs(DTST->getArgs(), DTST->getNumArgs());
1617
    break;
1895
    break;
1618
  }
1896
  }
Lines 2070-2076 Link Here
2070
  case BuiltinType::Id:
2348
  case BuiltinType::Id:
2071
#include "clang/AST/BuiltinTypes.def"
2349
#include "clang/AST/BuiltinTypes.def"
2072
  case BuiltinType::Dependent:
2350
  case BuiltinType::Dependent:
2073
    llvm_unreachable("mangling a placeholder type");
2351
    if (!NullOut)
2352
      llvm_unreachable("mangling a placeholder type");
2353
    break;
2074
  case BuiltinType::ObjCId:
2354
  case BuiltinType::ObjCId:
2075
    Out << "11objc_object";
2355
    Out << "11objc_object";
2076
    break;
2356
    break;
Lines 2546-2552 Link Here
2546
2826
2547
void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
2827
void CXXNameMangler::mangleType(const TemplateSpecializationType *T) {
2548
  if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2828
  if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) {
2549
    mangleName(TD, T->getArgs(), T->getNumArgs());
2829
    // types only have explicit abi tags, no addition tags
2830
    mangleTemplateName(TD,
2831
                       /* AdditionalAbiTags */ nullptr,
2832
                       /* ExcludeUnqualifiedName */ false,
2833
                       T->getArgs(), T->getNumArgs());
2550
  } else {
2834
  } else {
2551
    if (mangleSubstitution(QualType(T, 0)))
2835
    if (mangleSubstitution(QualType(T, 0)))
2552
      return;
2836
      return;
Lines 2872-2883 Link Here
2872
  case Expr::PseudoObjectExprClass:
3156
  case Expr::PseudoObjectExprClass:
2873
  case Expr::AtomicExprClass:
3157
  case Expr::AtomicExprClass:
2874
  {
3158
  {
2875
    // As bad as this diagnostic is, it's better than crashing.
3159
    if (!NullOut) {
2876
    DiagnosticsEngine &Diags = Context.getDiags();
3160
      // As bad as this diagnostic is, it's better than crashing.
2877
    unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3161
      DiagnosticsEngine &Diags = Context.getDiags();
2878
                                     "cannot yet mangle expression type %0");
3162
      unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
2879
    Diags.Report(E->getExprLoc(), DiagID)
3163
                                       "cannot yet mangle expression type %0");
2880
      << E->getStmtClassName() << E->getSourceRange();
3164
      Diags.Report(E->getExprLoc(), DiagID)
3165
        << E->getStmtClassName() << E->getSourceRange();
3166
    }
2881
    break;
3167
    break;
2882
  }
3168
  }
2883
3169
Lines 4020-4025 Link Here
4020
  Substitutions[Ptr] = SeqID++;
4306
  Substitutions[Ptr] = SeqID++;
4021
}
4307
}
4022
4308
4309
CXXNameMangler::AbiTagSet
4310
CXXNameMangler::getTagsFromPrefixAndTemplateArguments(const NamedDecl *ND) {
4311
  llvm::raw_null_ostream NullOutStream;
4312
  CXXNameMangler TrackPrefixAndTemplateArguments(*this, NullOutStream);
4313
4314
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
4315
    TrackPrefixAndTemplateArguments.mangleFunctionEncoding(
4316
        FD, /* ExcludeUnqualifiedName */ true);
4317
  } else {
4318
    TrackPrefixAndTemplateArguments.mangleName(
4319
        ND, /* ExcludeUnqualifiedName */ true);
4320
  }
4321
4322
  return std::move(
4323
      TrackPrefixAndTemplateArguments.AbiTagsRoot.getUsedAbiTags());
4324
}
4325
4326
CXXNameMangler::AbiTagList
4327
CXXNameMangler::makeAdditionalTagsForFunction(const FunctionDecl *FD) {
4328
  // when derived abi tags are disabled there is no need to make any list
4329
  if (DisableDerivedAbiTags)
4330
    return AbiTagList();
4331
4332
  AbiTagSet ImplicitlyAvailableTags =
4333
      getTagsFromPrefixAndTemplateArguments(FD);
4334
  AbiTagSet ReturnTypeTags;
4335
4336
  {
4337
    llvm::raw_null_ostream NullOutStream;
4338
    CXXNameMangler TrackReturnTypeTags(*this, NullOutStream);
4339
    TrackReturnTypeTags.disableDerivedAbiTags();
4340
4341
    const FunctionProtoType *Proto =
4342
        cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
4343
    TrackReturnTypeTags.FunctionTypeDepth.enterResultType();
4344
    TrackReturnTypeTags.mangleType(Proto->getReturnType());
4345
    TrackReturnTypeTags.FunctionTypeDepth.leaveResultType();
4346
4347
    ReturnTypeTags =
4348
        std::move(TrackReturnTypeTags.AbiTagsRoot.getUsedAbiTags());
4349
  }
4350
4351
  AbiTagList AdditionalAbiTags;
4352
4353
  for (const auto &Tag : ReturnTypeTags) {
4354
    if (ImplicitlyAvailableTags.count(Tag) == 0)
4355
      AdditionalAbiTags.push_back(Tag);
4356
  }
4357
4358
  return AdditionalAbiTags;
4359
}
4360
4361
CXXNameMangler::AbiTagList
4362
CXXNameMangler::makeAdditionalTagsForVariable(const VarDecl *VD) {
4363
  // when derived abi tags are disabled there is no need to make any list
4364
  if (DisableDerivedAbiTags)
4365
    return AbiTagList();
4366
4367
  AbiTagSet ImplicitlyAvailableTags =
4368
      getTagsFromPrefixAndTemplateArguments(VD);
4369
  AbiTagSet VariableTypeTags;
4370
4371
  {
4372
    llvm::raw_null_ostream NullOutStream;
4373
    CXXNameMangler TrackVariableType(*this, NullOutStream);
4374
    TrackVariableType.disableDerivedAbiTags();
4375
4376
    TrackVariableType.mangleType(VD->getType());
4377
4378
    VariableTypeTags =
4379
        std::move(TrackVariableType.AbiTagsRoot.getUsedAbiTags());
4380
  }
4381
4382
  AbiTagList AdditionalAbiTags;
4383
4384
  for (const auto &Tag : VariableTypeTags) {
4385
    if (ImplicitlyAvailableTags.count(Tag) == 0)
4386
      AdditionalAbiTags.push_back(Tag);
4387
  }
4388
4389
  return AdditionalAbiTags;
4390
}
4391
4392
bool CXXNameMangler::shouldHaveAbiTags(ItaniumMangleContextImpl &C,
4393
                                       const VarDecl *VD) {
4394
  llvm::raw_null_ostream NullOutStream;
4395
  CXXNameMangler TrackAbiTags(C, NullOutStream, nullptr, true);
4396
  TrackAbiTags.mangle(VD);
4397
  return TrackAbiTags.AbiTagsRoot.getUsedAbiTags().size();
4398
}
4399
4023
//
4400
//
4024
4401
4025
/// Mangles the name of the declaration D and emits that name to the given
4402
/// Mangles the name of the declaration D and emits that name to the given
Lines 4121-4126 Link Here
4121
  //  <special-name> ::= GV <object name>       # Guard variable for one-time
4498
  //  <special-name> ::= GV <object name>       # Guard variable for one-time
4122
  //                                            # initialization
4499
  //                                            # initialization
4123
  CXXNameMangler Mangler(*this, Out);
4500
  CXXNameMangler Mangler(*this, Out);
4501
  // GCC 5.3.0 doesn't emit derived ABI tags for local names but that seems to
4502
  // be a bug that is fixed in trunk.
4124
  Mangler.getStream() << "_ZGV";
4503
  Mangler.getStream() << "_ZGV";
4125
  Mangler.mangleName(D);
4504
  Mangler.mangleName(D);
4126
}
4505
}
(-)llvm-3.8.0.src.orig/tools/clang/lib/Sema/SemaDeclAttr.cpp (-4 lines)
Lines 4476-4485 Link Here
4476
  D->addAttr(::new (S.Context)
4476
  D->addAttr(::new (S.Context)
4477
             AbiTagAttr(Attr.getRange(), S.Context, Tags.data(), Tags.size(),
4477
             AbiTagAttr(Attr.getRange(), S.Context, Tags.data(), Tags.size(),
4478
                        Attr.getAttributeSpellingListIndex()));
4478
                        Attr.getAttributeSpellingListIndex()));
4479
4480
  // FIXME: remove this warning as soon as mangled part is ready.
4481
  S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored)
4482
        << Attr.getName();
4483
}
4479
}
4484
4480
4485
static void handleARMInterruptAttr(Sema &S, Decl *D,
4481
static void handleARMInterruptAttr(Sema &S, Decl *D,
(-)llvm-3.8.0.src.orig/tools/clang/test/CodeGenCXX/mangle-abi-tag.cpp (+146 lines)
Line 0 Link Here
1
// RUN: %clang_cc1 %s -emit-llvm -triple %itanium_abi_triple -std=c++11 -o - | FileCheck %s
2
// RUN: %clang_cc1 %s -emit-llvm -triple i686-linux-gnu -std=c++11 -o - | FileCheck %s
3
// RUN: %clang_cc1 %s -emit-llvm -triple x86_64-linux-gnu -std=c++11 -o - | FileCheck %s
4
5
struct __attribute__((abi_tag("A", "B"))) A { };
6
7
struct B: A { };
8
9
template<class T>
10
11
struct C {
12
};
13
14
struct D { A* p; };
15
16
template<class T>
17
struct __attribute__((abi_tag("C", "D"))) E {
18
};
19
20
struct __attribute__((abi_tag("A", "B"))) F { };
21
22
A a1;
23
// CHECK: @_Z2a1B1AB1B =
24
25
__attribute__((abi_tag("C", "D")))
26
A a2;
27
// CHECK: @_Z2a2B1AB1BB1CB1D =
28
29
B a3;
30
// CHECK: @a3 =
31
32
C<A> a4;
33
// CHECK: @_Z2a4B1AB1B =
34
35
D a5;
36
// CHECK: @a5 =
37
38
E<int> a6;
39
// CHECK: @_Z2a6B1CB1D =
40
41
E<A> a7;
42
// CHECK: @_Z2a7B1AB1BB1CB1D =
43
44
template<>
45
struct E<float> {
46
  static float a8;
47
};
48
float E<float>::a8;
49
// CHECK: @_ZN1EB1CB1DIfE2a8E =
50
51
template<>
52
struct E<F> {
53
  static bool a9;
54
};
55
bool E<F>::a9;
56
// CHECK: @_ZN1EB1CB1DI1FB1AB1BE2a9E =
57
58
struct __attribute__((abi_tag("A", "B"))) A10 {
59
  virtual ~A10() {}
60
} a10;
61
// vtable
62
// CHECK: @_ZTV3A10B1AB1B =
63
// typeinfo
64
// CHECK: @_ZTI3A10B1AB1B =
65
66
// Local variables from f13.
67
// f13()::L::foo[abi:C][abi:D]()::a[abi:A][abi:B]
68
// CHECK-DAG: @_ZZZ3f13vEN1L3fooB1CB1DEvE1aB1AB1B =
69
// guard variable for f13()::L::foo[abi:C][abi:D]()::a[abi:A][abi:B]
70
// CHECK-DAG: @_ZGVZZ3f13vEN1L3fooB1CB1DEvE1aB1AB1B =
71
72
__attribute__ ((abi_tag("C", "D")))
73
void* f1() {
74
  return 0;
75
}
76
// CHECK: define {{.*}} @_Z2f1B1CB1Dv(
77
78
__attribute__ ((abi_tag("C", "D")))
79
A* f2() {
80
  return 0;
81
}
82
// CHECK: define {{.*}} @_Z2f2B1AB1BB1CB1Dv(
83
84
B* f3() {
85
  return 0;
86
}
87
// CHECK: define {{.*}} @_Z2f3v(
88
89
C<A>* f4() {
90
  return 0;
91
}
92
// CHECK: define {{.*}} @_Z2f4B1AB1Bv(
93
94
D* f5() {
95
  return 0;
96
}
97
// CHECK: define {{.*}} @_Z2f5v(
98
99
E<char>* f6() {
100
  return 0;
101
}
102
// CHECK: define {{.*}} @_Z2f6B1CB1Dv(
103
104
E<A>* f7() {
105
  return 0;
106
}
107
// CHECK: define {{.*}} @_Z2f7B1AB1BB1CB1Dv(
108
109
void f8(E<A>*) {
110
}
111
// CHECK: define {{.*}} @_Z2f8P1EB1CB1DI1AB1AB1BE(
112
113
inline namespace Names1 __attribute__((__abi_tag__)) {
114
    class C1 {};
115
}
116
C1 f9() { return C1(); }
117
// CHECK: @_Z2f9B6Names1v(
118
119
inline namespace Names2 __attribute__((__abi_tag__("Tag1", "Tag2"))) {
120
    class C2 {};
121
}
122
C2 f10() { return C2(); }
123
// CHECK: @_Z3f10B4Tag1B4Tag2v(
124
125
void __attribute__((abi_tag("A"))) f11(A) {}
126
// f11[abi:A](A[abi:A][abi:B])
127
// CHECK: define {{.*}} @_Z3f11B1A1AB1AB1B(
128
129
A f12(A) { return A(); }
130
// f12(A[abi:A][abi:B])
131
// CHECK: define {{.*}} @_Z3f121AB1AB1B(
132
133
inline void f13() {
134
  struct L {
135
    static E<int>* foo() {
136
      static A10 a;
137
      return 0;
138
    }
139
  };
140
  L::foo();
141
}
142
void f11_test() {
143
  f13();
144
}
145
// f13()::L::foo[abi:C][abi:D]()
146
// CHECK: define linkonce_odr %struct.E* @_ZZ3f13vEN1L3fooB1CB1DEv(
(-)llvm-3.8.0.src.orig/tools/clang/test/SemaCXX/attr-abi-tag-syntax.cpp (-10 lines)
Lines 16-43 Link Here
16
// expected-warning@-1 {{'abi_tag' attribute on anonymous namespace ignored}}
16
// expected-warning@-1 {{'abi_tag' attribute on anonymous namespace ignored}}
17
17
18
inline namespace N __attribute__((__abi_tag__)) {}
18
inline namespace N __attribute__((__abi_tag__)) {}
19
// FIXME: remove this warning as soon as attribute fully supported.
20
// expected-warning@-2 {{'__abi_tag__' attribute ignored}}
21
19
22
} // namespcace N2
20
} // namespcace N2
23
21
24
__attribute__((abi_tag("B", "A"))) extern int a1;
22
__attribute__((abi_tag("B", "A"))) extern int a1;
25
// FIXME: remove this warning as soon as attribute fully supported.
26
// expected-warning@-2 {{'abi_tag' attribute ignored}}
27
23
28
__attribute__((abi_tag("A", "B"))) extern int a1;
24
__attribute__((abi_tag("A", "B"))) extern int a1;
29
// expected-note@-1 {{previous declaration is here}}
25
// expected-note@-1 {{previous declaration is here}}
30
// FIXME: remove this warning as soon as attribute fully supported.
31
// expected-warning@-3 {{'abi_tag' attribute ignored}}
32
26
33
__attribute__((abi_tag("A", "C"))) extern int a1;
27
__attribute__((abi_tag("A", "C"))) extern int a1;
34
// expected-error@-1 {{'abi_tag' C missing in original declaration}}
28
// expected-error@-1 {{'abi_tag' C missing in original declaration}}
35
// FIXME: remove this warning as soon as attribute fully supported.
36
// expected-warning@-3 {{'abi_tag' attribute ignored}}
37
29
38
extern int a2;
30
extern int a2;
39
// expected-note@-1 {{previous declaration is here}}
31
// expected-note@-1 {{previous declaration is here}}
40
__attribute__((abi_tag("A")))extern int a2;
32
__attribute__((abi_tag("A")))extern int a2;
41
// expected-error@-1 {{cannot add 'abi_tag' attribute in a redeclaration}}
33
// expected-error@-1 {{cannot add 'abi_tag' attribute in a redeclaration}}
42
// FIXME: remove this warning as soon as attribute fully supported.
43
// expected-warning@-3 {{'abi_tag' attribute ignored}}

Return to bug 571600