Merge pull request #18697 from slavapestov/minor-irgen-cleanups

Minor IRGen cleanups
diff --git a/lib/IRGen/ClassMetadataVisitor.h b/lib/IRGen/ClassMetadataVisitor.h
index 82bacaf..00eb737 100644
--- a/lib/IRGen/ClassMetadataVisitor.h
+++ b/lib/IRGen/ClassMetadataVisitor.h
@@ -76,7 +76,7 @@
     asImpl().addIVarDestroyer();
 
     // Class members.
-    addClassMembers(Target, Target->getDeclaredTypeInContext());
+    addClassMembers(Target);
   }
 
   /// Notes the beginning of the field offset vector for a particular ancestor
@@ -89,11 +89,13 @@
 
 private:
   /// Add fields associated with the given class and its bases.
-  void addClassMembers(ClassDecl *theClass, Type type) {
+  void addClassMembers(ClassDecl *theClass) {
     // Visit the superclass first.
-    if (Type superclass = type->getSuperclass()) {
-      auto *superclassDecl = superclass->getClassOrBoundGenericClass();
-      if (IGM.isResilient(superclassDecl, ResilienceExpansion::Maximal)) {
+    if (auto *superclassDecl = theClass->getSuperclassDecl()) {
+      if (superclassDecl->hasClangNode()) {
+        // Nothing to do; Objective-C classes do not add new members to
+        // Swift class metadata.
+      } else if (IGM.isResilient(superclassDecl, ResilienceExpansion::Maximal)) {
         // Runtime metadata instantiation will initialize our field offset
         // vector and vtable entries.
         //
@@ -104,7 +106,7 @@
         // NB: We don't apply superclass substitutions to members because we want
         // consistent metadata layout between generic superclasses and concrete
         // subclasses.
-        addClassMembers(superclassDecl, superclass);
+        addClassMembers(superclassDecl);
       }
     }
 
@@ -114,7 +116,7 @@
 
     // Add space for the generic parameters, if applicable.
     // This must always be the first item in the immediate members.
-    asImpl().addGenericFields(theClass, type, theClass);
+    asImpl().addGenericFields(theClass, theClass);
 
     // Add vtable entries.
     asImpl().addVTableEntries(theClass);
@@ -192,13 +194,8 @@
       addPointer();
     }
   }
-  void addGenericArgument(CanType argument, ClassDecl *forClass) {
-    addPointer();
-  }
-  void addGenericWitnessTable(CanType argument, ProtocolConformanceRef conf,
-                              ClassDecl *forClass) {
-    addPointer();
-  }
+  void addGenericArgument(ClassDecl *forClass) { addPointer(); }
+  void addGenericWitnessTable(ClassDecl *forClass) { addPointer(); }
   void addPlaceholder(MissingMemberDecl *MMD) {
     for (auto i : range(MMD->getNumberOfVTableEntries())) {
       (void)i;
diff --git a/lib/IRGen/DebugTypeInfo.cpp b/lib/IRGen/DebugTypeInfo.cpp
index 1463b6d..cb87f65 100644
--- a/lib/IRGen/DebugTypeInfo.cpp
+++ b/lib/IRGen/DebugTypeInfo.cpp
@@ -65,8 +65,7 @@
                                               VarDecl *Decl, swift::Type Ty,
                                               const TypeInfo &Info) {
 
-  auto DeclType =
-      Decl->hasInterfaceType() ? Decl->getInterfaceType() : Decl->getType();
+  auto DeclType = Decl->getInterfaceType();
   auto RealType = Ty;
 
   // DynamicSelfType is also sugar as far as debug info is concerned.
@@ -101,9 +100,7 @@
   if (auto *Decl = GV->getDecl()) {
     DC = Decl->getDeclContext();
     GE = DC->getGenericEnvironmentOfContext();
-    auto DeclType =
-        (Decl->hasType() ? Decl->getType()
-                         : DC->mapTypeIntoContext(Decl->getInterfaceType()));
+    auto DeclType = Decl->getType();
     if (DeclType->isEqual(LowTy))
       Type = DeclType.getPointer();
   }
diff --git a/lib/IRGen/EnumMetadataVisitor.h b/lib/IRGen/EnumMetadataVisitor.h
index 503f627..30bd8da 100644
--- a/lib/IRGen/EnumMetadataVisitor.h
+++ b/lib/IRGen/EnumMetadataVisitor.h
@@ -55,11 +55,11 @@
 
     // Generic arguments.
     // This must always be the first piece of trailing data.
-    asImpl().addGenericFields(Target, Target->getDeclaredTypeInContext());
+    asImpl().addGenericFields(Target);
 
     // Reserve a word to cache the payload size if the type has dynamic layout.
     auto &strategy = getEnumImplStrategy(IGM,
-           Target->DeclContext::getDeclaredTypeInContext()->getCanonicalType());
+           Target->getDeclaredTypeInContext()->getCanonicalType());
     if (strategy.needsPayloadSizeInMetadata())
       asImpl().addPayloadSize();
   }
@@ -82,10 +82,8 @@
   void addMetadataFlags() { addPointer(); }
   void addValueWitnessTable() { addPointer(); }
   void addNominalTypeDescriptor() { addPointer(); }
-  void addGenericArgument(CanType argument) { addPointer(); }
-  void addGenericWitnessTable(CanType argument, ProtocolConformanceRef conf) {
-    addPointer();
-  }
+  void addGenericArgument() { addPointer(); }
+  void addGenericWitnessTable() { addPointer(); }
   void addPayloadSize() { addPointer(); }
   void noteStartOfTypeSpecificMembers() {}
 
diff --git a/lib/IRGen/GenMeta.cpp b/lib/IRGen/GenMeta.cpp
index e89f8f5..ede68df 100644
--- a/lib/IRGen/GenMeta.cpp
+++ b/lib/IRGen/GenMeta.cpp
@@ -1929,13 +1929,12 @@
     void emitInitializeMethodOverrides(IRGenFunction &IGF,
                                        llvm::Value *metadata) {}
 
-    void addGenericArgument(CanType argTy, ClassDecl *forClass) {
-      B.addNullPointer(IGM.TypeMetadataPtrTy);
+    void addGenericArgument(ClassDecl *forClass) {
+      llvm_unreachable("Fixed class metadata cannot have generic parameters");
     }
 
-    void addGenericWitnessTable(CanType argTy, ProtocolConformanceRef conf,
-                                ClassDecl *forClass) {
-      B.addNullPointer(IGM.WitnessTablePtrTy);
+    void addGenericWitnessTable(ClassDecl *forClass) {
+      llvm_unreachable("Fixed class metadata cannot have generic requirements");
     }
   };
 
@@ -1986,10 +1985,9 @@
       }
     }
 
-    void addGenericArgument(CanType argTy, ClassDecl *forClass) {}
+    void addGenericArgument(ClassDecl *forClass) {}
 
-    void addGenericWitnessTable(CanType argTy, ProtocolConformanceRef conf,
-                                ClassDecl *forClass) {}
+    void addGenericWitnessTable(ClassDecl *forClass) {}
   };
 
   /// Base class for laying out class metadata.
@@ -2275,13 +2273,12 @@
 
     void addMethodOverride(SILDeclRef baseRef, SILDeclRef declRef) {}
 
-    void addGenericArgument(CanType argTy, ClassDecl *forClass) {
-      Members.addGenericArgument(argTy, forClass);
+    void addGenericArgument(ClassDecl *forClass) {
+      Members.addGenericArgument(forClass);
     }
 
-    void addGenericWitnessTable(CanType argTy, ProtocolConformanceRef conf,
-                                ClassDecl *forClass) {
-      Members.addGenericWitnessTable(argTy, conf, forClass);
+    void addGenericWitnessTable(ClassDecl *forClass) {
+      Members.addGenericWitnessTable(forClass);
     }
 
   protected:
@@ -3036,11 +3033,11 @@
       B.addAlignmentPadding(super::IGM.getPointerAlignment());
     }
 
-    void addGenericArgument(CanType type) {
+    void addGenericArgument() {
       B.addNullPointer(IGM.TypeMetadataPtrTy);
     }
 
-    void addGenericWitnessTable(CanType type, ProtocolConformanceRef conf) {
+    void addGenericWitnessTable() {
       B.addNullPointer(IGM.WitnessTablePtrTy);
     }
 
@@ -3188,7 +3185,6 @@
 
 /// Emit the type metadata or metadata template for a struct.
 void irgen::emitStructMetadata(IRGenModule &IGM, StructDecl *structDecl) {
-  // TODO: structs nested within generic types
   ConstantInitBuilder initBuilder(IGM);
   auto init = initBuilder.beginStruct();
   init.setPacked(true);
@@ -3288,11 +3284,11 @@
       B.add(emitNominalTypeDescriptor());
     }
 
-    void addGenericArgument(CanType type) {
+    void addGenericArgument() {
       B.addNullPointer(IGM.TypeMetadataPtrTy);
     }
 
-    void addGenericWitnessTable(CanType type, ProtocolConformanceRef conf) {
+    void addGenericWitnessTable() {
       B.addNullPointer(IGM.WitnessTablePtrTy);
     }
 
@@ -3405,7 +3401,6 @@
 } // end anonymous namespace
 
 void irgen::emitEnumMetadata(IRGenModule &IGM, EnumDecl *theEnum) {
-  // TODO: enums nested inside generic types
   ConstantInitBuilder initBuilder(IGM);
   auto init = initBuilder.beginStruct();
   init.setPacked(true);
diff --git a/lib/IRGen/GenProto.cpp b/lib/IRGen/GenProto.cpp
index e3eeaf6..58e13d6 100644
--- a/lib/IRGen/GenProto.cpp
+++ b/lib/IRGen/GenProto.cpp
@@ -3004,8 +3004,7 @@
 }
 
 /// The information we need to record in generic type metadata
-/// is the information in the type's generic signature, minus the
-/// information recoverable from the type's parent type.  This is
+/// is the information in the type's generic signature.  This is
 /// simply the information that would be passed to a generic function
 /// that takes the (thick) parent metatype as an argument.
 GenericTypeRequirements::GenericTypeRequirements(IRGenModule &IGM,
diff --git a/lib/IRGen/MetadataLayout.cpp b/lib/IRGen/MetadataLayout.cpp
index 70720aa..a1fe75c 100644
--- a/lib/IRGen/MetadataLayout.cpp
+++ b/lib/IRGen/MetadataLayout.cpp
@@ -313,19 +313,18 @@
       super::noteStartOfGenericRequirements(forClass);
     }
 
-    void addGenericWitnessTable(CanType argType, ProtocolConformanceRef conf,
-                                ClassDecl *forClass) {
+    void addGenericWitnessTable(ClassDecl *forClass) {
       if (forClass == Target) {
         Layout.NumImmediateMembers++;
       }
-      super::addGenericWitnessTable(argType, conf, forClass);
+      super::addGenericWitnessTable(forClass);
     }
 
-    void addGenericArgument(CanType argType, ClassDecl *forClass) {
+    void addGenericArgument(ClassDecl *forClass) {
       if (forClass == Target) {
         Layout.NumImmediateMembers++;
       }
-      super::addGenericArgument(argType, forClass);
+      super::addGenericArgument(forClass);
     }
 
     void addMethod(SILDeclRef fn) {
diff --git a/lib/IRGen/NominalMetadataVisitor.h b/lib/IRGen/NominalMetadataVisitor.h
index 3944a4e..196463d 100644
--- a/lib/IRGen/NominalMetadataVisitor.h
+++ b/lib/IRGen/NominalMetadataVisitor.h
@@ -56,8 +56,7 @@
   /// fields.  e.g., if B<T> extends A<T>, the witness for T in A's
   /// section should be enough.
   template <class... T>
-  void addGenericFields(NominalTypeDecl *typeDecl, Type type,
-                        T &&...args) {
+  void addGenericFields(NominalTypeDecl *typeDecl, T &&...args) {
     // The archetype order here needs to be consistent with
     // NominalTypeDescriptorBase::addGenericParams.
     
@@ -65,19 +64,13 @@
     asImpl().noteStartOfGenericRequirements(args...);
 
     GenericTypeRequirements requirements(IGM, typeDecl);
-    if (requirements.empty()) return;
-
-    auto subs = type->getContextSubstitutionMap(IGM.getSwiftModule(),
-                                                typeDecl);
-    requirements.enumerateFulfillments(IGM, subs,
-                    [&](unsigned reqtIndex, CanType argType,
-                        Optional<ProtocolConformanceRef> conf) {
-      if (conf) {
-        asImpl().addGenericWitnessTable(argType, *conf, args...);
+    for (auto reqt : requirements.getRequirements()) {
+      if (reqt.Protocol) {
+        asImpl().addGenericWitnessTable(args...);
       } else {
-        asImpl().addGenericArgument(argType, args...);
+        asImpl().addGenericArgument(args...);
       }
-    });
+    }
 
     asImpl().noteEndOfGenericRequirements(args...);
   }
diff --git a/lib/IRGen/StructMetadataVisitor.h b/lib/IRGen/StructMetadataVisitor.h
index 3a4fda7..837ea00 100644
--- a/lib/IRGen/StructMetadataVisitor.h
+++ b/lib/IRGen/StructMetadataVisitor.h
@@ -53,7 +53,7 @@
 
     // Generic arguments.
     // This must always be the first piece of trailing data.
-    asImpl().addGenericFields(Target, Target->getDeclaredTypeInContext());
+    asImpl().addGenericFields(Target);
 
     // Struct field offsets.
     asImpl().noteStartOfFieldOffsets();
@@ -87,10 +87,8 @@
   void addValueWitnessTable() { addPointer(); }
   void addNominalTypeDescriptor() { addPointer(); }
   void addFieldOffset(VarDecl *) { addInt32(); }
-  void addGenericArgument(CanType argument) { addPointer(); }
-  void addGenericWitnessTable(CanType argument, ProtocolConformanceRef conf) {
-    addPointer();
-  }
+  void addGenericArgument() { addPointer(); }
+  void addGenericWitnessTable() { addPointer(); }
   void noteStartOfTypeSpecificMembers() {}
 
   void noteEndOfFieldOffsets() {
diff --git a/lib/Sema/PCMacro.cpp b/lib/Sema/PCMacro.cpp
index 7a17a31..c5a7bef 100644
--- a/lib/Sema/PCMacro.cpp
+++ b/lib/Sema/PCMacro.cpp
@@ -484,6 +484,7 @@
                               Context.getIdentifier(NameBuf),
                               TypeCheckDC);
     VD->setType(MaybeLoadInitExpr->getType());
+    VD->setInterfaceType(MaybeLoadInitExpr->getType()->mapTypeOutOfContext());
     VD->setImplicit();
 
     NamedPattern *NP = new (Context) NamedPattern(VD, /*implicit*/ true);