]> granicus.if.org Git - clang/commitdiff
Template instantiation for array subscript expressions. This was far
authorDouglas Gregor <dgregor@apple.com>
Tue, 19 May 2009 00:01:19 +0000 (00:01 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 19 May 2009 00:01:19 +0000 (00:01 +0000)
easier than expected because of the limitation that subscript
operators must be member functions.

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

lib/Sema/SemaExpr.cpp
lib/Sema/SemaOverload.cpp
lib/Sema/SemaTemplateInstantiateExpr.cpp
test/SemaTemplate/instantiate-subscript.cpp [new file with mode: 0644]

index 418b3b8ce0566cdded56134a1eb8ba0c1eeab5d7..ed62b254d560f0c5af1548a3a73bbf34e9bb0a49 100644 (file)
@@ -1665,6 +1665,14 @@ Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc,
        *RHSExp = static_cast<Expr*>(Idx.get());
 
   if (getLangOptions().CPlusPlus &&
+      (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) {
+    Base.release();
+    Idx.release();
+    return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp,
+                                                  Context.DependentTy, RLoc));
+  }
+
+  if (getLangOptions().CPlusPlus && 
       (LHSExp->getType()->isRecordType() ||
        LHSExp->getType()->isEnumeralType() ||
        RHSExp->getType()->isRecordType() ||
index 90da96b5fb07ecf04f228b26082d6dcaa21f8c9f..ef175315e6e9e980667f2db675df24df1bc20652 100644 (file)
@@ -2402,7 +2402,8 @@ void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S,
     T2 = Args[1]->getType();
 
   DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
-  LookupOverloadedOperatorName(Op, S, T1, T2, Functions);
+  if (S)
+    LookupOverloadedOperatorName(Op, S, T1, T2, Functions);
   ArgumentDependentLookup(OpName, Args, NumArgs, Functions);
   AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet);
   AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange);
index c6ce6f469381a38ff29966e068ba9911233082da..41df7aba29ea6b13894bcc0eb691bbfdd268b51a 100644 (file)
@@ -45,6 +45,7 @@ namespace {
     OwningExprResult VisitDeclRefExpr(DeclRefExpr *E);
     OwningExprResult VisitParenExpr(ParenExpr *E);
     OwningExprResult VisitUnaryOperator(UnaryOperator *E);
+    OwningExprResult VisitArraySubscriptExpr(ArraySubscriptExpr *E);
     OwningExprResult VisitBinaryOperator(BinaryOperator *E);
     OwningExprResult VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
     OwningExprResult VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E);
@@ -172,6 +173,36 @@ TemplateExprInstantiator::VisitUnaryOperator(UnaryOperator *E) {
                                       move(Arg));
 }
 
+Sema::OwningExprResult 
+TemplateExprInstantiator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
+  Sema::OwningExprResult LHS = Visit(E->getLHS());
+  if (LHS.isInvalid())
+    return SemaRef.ExprError();
+
+  Sema::OwningExprResult RHS = Visit(E->getRHS());
+  if (RHS.isInvalid())
+    return SemaRef.ExprError();
+
+  // Since the overloaded array-subscript operator (operator[]) can
+  // only be a member function, we can make several simplifying
+  // assumptions here:
+  //   1) Normal name lookup (from the current scope) will not ever
+  //   find any declarations of operator[] that won't also be found be
+  //   member operator lookup, so it is safe to pass a NULL Scope
+  //   during the instantiation to avoid the lookup entirely.
+  //
+  //   2) Neither normal name lookup nor argument-dependent lookup at
+  //   template definition time will find any operators that won't be
+  //   found at template instantiation time, so we do not need to
+  //   cache the results of name lookup as we do for the binary
+  //   operators.
+  SourceLocation LLocFake = ((Expr*)LHS.get())->getSourceRange().getBegin();
+  return SemaRef.ActOnArraySubscriptExpr(/*Scope=*/0, move(LHS),
+                                         /*FIXME:*/LLocFake,
+                                         move(RHS),
+                                         E->getRBracketLoc());
+}
+
 Sema::OwningExprResult 
 TemplateExprInstantiator::VisitBinaryOperator(BinaryOperator *E) {
   Sema::OwningExprResult LHS = Visit(E->getLHS());
diff --git a/test/SemaTemplate/instantiate-subscript.cpp b/test/SemaTemplate/instantiate-subscript.cpp
new file mode 100644 (file)
index 0000000..434d84e
--- /dev/null
@@ -0,0 +1,26 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+
+struct Sub0 {
+  int &operator[](int);
+};
+
+struct Sub1 {
+  long &operator[](long);
+};
+
+struct ConvertibleToInt {
+  operator int();
+};
+
+template<typename T, typename U, typename Result>
+struct Subscript0 {
+  void test(T t, U u) {
+    Result &result = t[u]; // expected-error{{subscripted value is not}}
+  }
+};
+
+template struct Subscript0<int*, int, int&>;
+template struct Subscript0<Sub0, int, int&>;
+template struct Subscript0<Sub1, ConvertibleToInt, long&>;
+template struct Subscript0<Sub1, Sub0, long&>; // expected-note{{instantiation}}