C++ modules: merging for enumerations and enumerators with multiple definitions
authorRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 15 Oct 2013 22:02:41 +0000 (22:02 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 15 Oct 2013 22:02:41 +0000 (22:02 +0000)
(eg through template instantiations in multiple modules).

llvm-svn: 192740

clang/include/clang/AST/Decl.h
clang/include/clang/Serialization/ASTReader.h
clang/lib/Serialization/ASTReaderDecl.cpp
clang/test/Modules/Inputs/cxx-templates-a.h
clang/test/Modules/Inputs/cxx-templates-b.h
clang/test/Modules/Inputs/cxx-templates-common.h
clang/test/Modules/cxx-templates.cpp

index 6d0a7f1..9d2a747 100644 (file)
@@ -2242,7 +2242,7 @@ public:
 /// that is defined.  For example, in "enum X {a,b}", each of a/b are
 /// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
 /// TagType for the X EnumDecl.
-class EnumConstantDecl : public ValueDecl {
+class EnumConstantDecl : public ValueDecl, public Mergeable<EnumConstantDecl> {
   Stmt *Init; // an integer constant expression
   llvm::APSInt Val; // The value.
 protected:
@@ -2268,6 +2268,14 @@ public:
 
   SourceRange getSourceRange() const LLVM_READONLY;
 
+  /// Retrieves the canonical declaration of this enumerator.
+  EnumConstantDecl *getCanonicalDecl() {
+    return getFirstDeclaration();
+  }
+  const EnumConstantDecl *getCanonicalDecl() const {
+    return getFirstDeclaration();
+  }
+
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   static bool classofKind(Kind K) { return K == EnumConstant; }
index 66fff8f..530c54e 100644 (file)
@@ -917,6 +917,10 @@ private:
   /// when merging implicit instantiations of class templates across modules.
   llvm::DenseMap<DeclContext *, DeclContext *> MergedDeclContexts;
 
+  /// \brief A mapping from canonical declarations of enums to their canonical
+  /// definitions. Only populated when using modules in C++.
+  llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions;
+
   /// \brief When reading a Stmt tree, Stmt operands are placed in this stack.
   SmallVector<Stmt *, 16> StmtStack;
 
index 2a1ac11..4bfb83d 100644 (file)
@@ -488,6 +488,19 @@ void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
   ED->IsScopedUsingClassTag = Record[Idx++];
   ED->IsFixed = Record[Idx++];
 
+  // If this is a definition subject to the ODR, and we already have a
+  // definition, merge this one into it.
+  if (ED->IsCompleteDefinition &&
+      Reader.getContext().getLangOpts().Modules &&
+      Reader.getContext().getLangOpts().CPlusPlus) {
+    if (EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()]) {
+      Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
+      ED->IsCompleteDefinition = false;
+    } else {
+      OldDef = ED;
+    }
+  }
+
   if (EnumDecl *InstED = ReadDeclAs<EnumDecl>(Record, Idx)) {
     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
     SourceLocation POI = ReadSourceLocation(Record, Idx);
@@ -516,6 +529,7 @@ void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
   if (Record[Idx++])
     ECD->setInitExpr(Reader.ReadExpr(F));
   ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
+  mergeMergeable(ECD);
 }
 
 void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
@@ -1893,6 +1907,12 @@ void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
   if (!Reader.getContext().getLangOpts().Modules)
     return;
 
+  // ODR-based merging is only performed in C++. In C, identically-named things
+  // in different translation units are not redeclarations (but may still have
+  // compatible types).
+  if (!Reader.getContext().getLangOpts().CPlusPlus)
+    return;
+
   if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
     if (T *Existing = ExistingRes)
       Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D),
@@ -2122,9 +2142,15 @@ static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
     // FIXME: Diagnose if the types don't match. More generally, diagnose if we
     // get a declaration in a class definition that isn't in the canonical class
     // definition.
+    // FIXME: Also check the bitwidth is odr-equivalent, if any.
     return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
   }
 
+  // Enumerators with the same name match.
+  if (isa<EnumConstantDecl>(X))
+    // FIXME: Also check the value is odr-equivalent.
+    return true;
+
   // FIXME: Many other cases to implement.
   return false;
 }
@@ -2138,6 +2164,9 @@ static DeclContext *getPrimaryContextForMerging(DeclContext *DC) {
   if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC))
     return RD->getDefinition();
 
+  if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
+    return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition() : 0;
+
   return 0;
 }
 
index d2261f6..0ca6e34 100644 (file)
@@ -33,3 +33,10 @@ void use_some_template_a() {
 
 template<int> struct MergeTemplates;
 MergeTemplates<0> *merge_templates_a;
+
+auto enum_a_from_a = CommonTemplate<int>::a;
+const auto enum_c_from_a = CommonTemplate<int>::c;
+
+template<int> struct UseInt;
+template<typename T> void UseRedeclaredEnum(UseInt<T() + CommonTemplate<char>::a>);
+constexpr void (*UseRedeclaredEnumA)(UseInt<1>) = UseRedeclaredEnum<int>;
index 6cc951b..f0921a7 100644 (file)
@@ -44,6 +44,13 @@ void use_some_template_b() {
   b = c;
 }
 
+auto enum_b_from_b = CommonTemplate<int>::b;
+const auto enum_c_from_b = CommonTemplate<int>::c;
+
+template<int> struct UseInt;
+template<typename T> void UseRedeclaredEnum(UseInt<T() + CommonTemplate<char>::a>);
+constexpr void (*UseRedeclaredEnumB)(UseInt<1>) = UseRedeclaredEnum<int>;
+
 @import cxx_templates_a;
 template<typename T> void UseDefinedInBImplIndirectly(T &v) {
   PerformDelayedLookup(v);
index 6993da8..40a11e2 100644 (file)
@@ -5,3 +5,7 @@ struct DefinedInCommon {
   struct Inner {};
   friend void FoundByADL(DefinedInCommon);
 };
+
+template<typename T> struct CommonTemplate {
+  enum E { a = 1, b = 2, c = 3 };
+};
index 911f953..0965aa8 100644 (file)
@@ -5,6 +5,7 @@
 
 @import cxx_templates_a;
 @import cxx_templates_b;
+@import cxx_templates_common;
 
 template<typename, char> struct Tmpl_T_C {};
 template<typename, int, int> struct Tmpl_T_I_I {};
@@ -12,6 +13,8 @@ template<typename, int, int> struct Tmpl_T_I_I {};
 template<typename A, typename B, A> struct Tmpl_T_T_A {};
 template<typename A, typename B, B> struct Tmpl_T_T_B {};
 
+template<int> struct UseInt {};
+
 void g() {
   f(0);
   f<double>(1.0);
@@ -71,6 +74,19 @@ void g() {
   PerformDelayedLookup(defined_in_b_impl); // expected-note {{in instantiation of}}
 
   merge_templates_a = merge_templates_b; // ok, same type
+
+  using T = decltype(enum_a_from_a);
+  using T = decltype(enum_b_from_b);
+  T e = true ? enum_a_from_a : enum_b_from_b;
+
+  UseRedeclaredEnum<int>(UseInt<1>());
+  // FIXME: Reintroduce this once we merge function template specializations.
+  //static_assert(UseRedeclaredEnumA == UseRedeclaredEnumB, "");
+  //static_assert(UseRedeclaredEnumA == UseRedeclaredEnum<int>, "");
+  //static_assert(UseRedeclaredEnumB == UseRedeclaredEnum<int>, "");
+  static_assert(enum_c_from_a == enum_c_from_b, "");
+  CommonTemplate<int> cti;
+  CommonTemplate<int>::E eee = CommonTemplate<int>::c;
 }
 
 RedeclaredAsFriend<int> raf1;