]> granicus.if.org Git - clang/commitdiff
Add basic, hackish support for instantiation of typedefs in a class
authorDouglas Gregor <dgregor@apple.com>
Wed, 11 Mar 2009 16:48:53 +0000 (16:48 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 11 Mar 2009 16:48:53 +0000 (16:48 +0000)
template. More importantly, start to sort out the issues regarding
complete types and nested-name-specifiers, especially the question of:
when do we instantiate a class template specialization that occurs to
the left of a '::' in a nested-name-specifier?

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66662 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.def
lib/Sema/Sema.h
lib/Sema/SemaCXXScopeSpec.cpp
lib/Sema/SemaDecl.cpp
lib/Sema/SemaExpr.cpp
lib/Sema/SemaLookup.cpp
lib/Sema/SemaTemplateInstantiate.cpp
test/SemaCXX/nested-name-spec.cpp
test/SemaTemplate/instantiate-typedef.cpp [new file with mode: 0644]

index 4589a97350177b01306b0655c2103f8a86722885..2c024de3804dd57e3e4aa076b5ad333413689158 100644 (file)
@@ -231,6 +231,10 @@ DIAG(error_property_implemented, ERROR,
 DIAG(warn_objc_property_attr_mutually_exclusive, WARNING,
      "property attributes '%0' and '%1' are mutually exclusive")
 
+// C++ name lookup
+DIAG(err_incomplete_nested_name_spec, ERROR,
+     "incomplete type %0 named in nested name specifier")
+
 // C++ class members
 DIAG(err_storageclass_invalid_for_member, ERROR,
      "storage class specified for a member declaration")
index b3669bc74d26d9d1d4b2a65467d09dd486c59e3f..2e02004113f4e03e13101228d18731533aa3a16c 100644 (file)
@@ -1394,6 +1394,8 @@ public:
                                                TypeTy *Ty,
                                                SourceLocation RParen);
 
+  bool RequireCompleteDeclContext(const CXXScopeSpec &SS);
+
   /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
   /// global scope ('::').
   virtual CXXScopeTy *ActOnCXXGlobalScopeSpecifier(Scope *S,
index c8a86cfd7d12c2380470e8be227c936291d0641f..7773d288ae54d092757611343f6f56ad2e665ebf 100644 (file)
 #include "llvm/ADT/STLExtras.h"
 using namespace clang;
 
+/// \brief Require that the context specified by SS be complete.
+///
+/// If SS refers to a type, this routine checks whether the type is
+/// complete enough (or can be made complete enough) for name lookup
+/// into the DeclContext. A type that is not yet completed can be
+/// considered "complete enough" if it is a class/struct/union/enum
+/// that is currently being defined. Or, if we have a type that names
+/// a class template specialization that is not a complete type, we
+/// will attempt to instantiate that class template.
+bool Sema::RequireCompleteDeclContext(const CXXScopeSpec &SS) {
+  if (!SS.isSet() || SS.isInvalid())
+    return false;
+  
+  DeclContext *DC = static_cast<DeclContext *>(SS.getScopeRep());
+  if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
+    // If we're currently defining this type, then lookup into the
+    // type is okay: don't complain that it isn't complete yet.
+    const TagType *TagT = Context.getTypeDeclType(Tag)->getAsTagType();
+    if (TagT->isBeingDefined())
+      return false;
+
+    // The type must be complete.
+    return RequireCompleteType(SS.getRange().getBegin(),
+                               Context.getTypeDeclType(Tag),
+                               diag::err_incomplete_nested_name_spec,
+                               SS.getRange());
+  }
+
+  return false;
+}
 
 /// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
 /// global scope ('::').
index 35573bb33d8e4991d4c33f36bbd4d2590e161b2d..03d00b83a3c94fec61cb6f0fdd77d12fc3ee6303 100644 (file)
@@ -1255,6 +1255,7 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl,
                           D.getIdentifierLoc());
   } else { // Something like "int foo::x;"
     DC = static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep());
+    // FIXME: RequireCompleteDeclContext(D.getCXXScopeSpec()); ?
     PrevDecl = LookupQualifiedName(DC, Name, LookupOrdinaryName, true);
 
     // C++ 7.3.1.2p2:
@@ -2895,6 +2896,7 @@ Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagSpec, TagKind TK,
       goto CreateNewDecl;
     }
 
+    // FIXME: RequireCompleteDeclContext(SS)?
     DC = static_cast<DeclContext*>(SS.getScopeRep());
     SearchDC = DC;
     // Look-up name inside 'foo::'.
index 9a86d65f9a0657177920d9d4c3504bd80b54d3f2..95c0baae5696c21c9e57a59a554038a8b4284039 100644 (file)
@@ -4344,6 +4344,8 @@ Sema::ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S,
   if (!Dependent && !ArgTy->isRecordType())
     return Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy;
 
+  // FIXME: Does the type need to be complete?
+
   // Otherwise, create a null pointer as the base, and iteratively process
   // the offsetof designators.
   QualType ArgTyPtr = Context.getPointerType(ArgTy);
index dd9c850d089e1407fe3a3b7fe1b95f0469648c46..a93689c9a05e9fd1323bb0967c3e65280c0ff2ef 100644 (file)
@@ -1035,7 +1035,7 @@ Sema::LookupParsedName(Scope *S, const CXXScopeSpec *SS,
                        bool RedeclarationOnly, bool AllowBuiltinCreation,
                        SourceLocation Loc) {
   if (SS) {
-    if (SS->isInvalid())
+    if (SS->isInvalid() || RequireCompleteDeclContext(*SS))
       return LookupResult::CreateLookupResult(Context, 0);
 
     if (SS->isSet())
index 21694b2e739785feda806bafc24827e37c4fa10a..d630e801b02ae468e55b1615547a7a93940ebfdd 100644 (file)
@@ -655,13 +655,44 @@ Sema::InstantiateClassTemplateSpecialization(
   // Start the definition of this instantiation.
   ClassTemplateSpec->startDefinition();
 
-  // FIXME: Create the injected-class-name for the
-  // instantiation. Should this be a typedef or something like it?
 
   // Instantiate the base class specifiers.
   if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template))
     Invalid = true;
 
+  // FIXME: Create the injected-class-name for the
+  // instantiation. Should this be a typedef or something like it?
+
+  RecordDecl *Pattern = Template->getTemplatedDecl();
+
+  for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
+                              MemberEnd = Pattern->decls_end();
+       Member != MemberEnd; ++Member) {
+    if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(*Member)) {
+      // FIXME: Simplified instantiation of typedefs needs to be made
+      // "real".
+      QualType T = Typedef->getUnderlyingType();
+      if (T->isDependentType()) {
+        T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(),
+                            ClassTemplateSpec->getNumTemplateArgs(),
+                            Typedef->getLocation(),
+                            Typedef->getDeclName());
+        if (T.isNull()) {
+          Invalid = true;
+          T = Context.IntTy;
+        }
+      }
+       
+      // Create the new typedef
+      TypedefDecl *New 
+        = TypedefDecl::Create(Context, ClassTemplateSpec,
+                              Typedef->getLocation(),
+                              Typedef->getIdentifier(),
+                              T);
+      ClassTemplateSpec->addDecl(New);
+    }
+  }
+
   // FIXME: Instantiate all of the members.
   
   // Add any implicitly-declared members that we might need.
index 2b6de3363b7e68f7a7a9acaa6e534f0ba744f579..f75b279a44933a839166141644ea3da3cfe7acc3 100644 (file)
@@ -1,5 +1,4 @@
-// RUN: clang -fsyntax-only -verify -std=c++98 %s 
-// fails due to exact diagnostic matching
+// RUN: clang -fsyntax-only -std=c++98 -verify %s 
 namespace A {
   struct C {
     static int cx;
@@ -151,5 +150,10 @@ void ::global_func2(int) { } // expected-error{{definition or redeclaration of '
 
 void N::f() { } // okay
 
+struct Y;  // expected-note{{forward declaration of 'struct Y'}}
+Y::foo y; // expected-error{{incomplete type 'struct Y' named in nested name specifier}} \
+         // FIXME: ugly: expected-error{{invalid token after top level declarator}}
+
 X::X() : a(5) { } // expected-error{{use of undeclared identifier 'X'}} \
       // expected-error{{expected function body after function declarator}}
+
diff --git a/test/SemaTemplate/instantiate-typedef.cpp b/test/SemaTemplate/instantiate-typedef.cpp
new file mode 100644 (file)
index 0000000..8ecee50
--- /dev/null
@@ -0,0 +1,15 @@
+// RUN: clang -fsyntax-only -verify %s
+
+template<typename T>
+struct add_pointer {
+  typedef T* type; // expected-error{{'type' declared as a pointer to a reference}}
+};
+
+add_pointer<int>::type test1(int * ptr) { return ptr; }
+
+add_pointer<float>::type test2(int * ptr) { 
+  return ptr; // expected-error{{incompatible type returning 'int *', expected 'type' (aka 'float *')}}
+}
+
+add_pointer<int&>::type // expected-note{{in instantiation of template class 'struct add_pointer<int &>' requested here}}
+test3(); // FIXME: expected-error{{invalid token after top level declarator}}