13a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall// RUN: %clang_cc1 -fsyntax-only %s -verify 232564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor 3e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// C++'0x [namespace.memdef] p3: 4e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// Every name first declared in a namespace is a member of that namespace. If 5e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// a friend declaration in a non-local class first declares a class or 6e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// function the friend class or function is a member of the innermost 7e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// enclosing namespace. 8e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth 9e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruthnamespace N { 10e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth struct S0 { 11e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth friend struct F0; 12e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth friend void f0(int); 13e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth struct F0 member_func(); 14e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth }; 15e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth struct F0 { }; 16e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth F0 f0() { return S0().member_func(); } 17e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth} 18e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler CarruthN::F0 f0_var = N::f0(); 19e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth 20e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// Ensure we can handle attaching friend declarations to an enclosing namespace 21e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// with multiple contexts. 22e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruthnamespace N { struct S1 { struct IS1; }; } 23e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruthnamespace N { 24e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth struct S1::IS1 { 25e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth friend struct F1; 26e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth friend void f1(int); 27e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth struct F1 member_func(); 28e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth }; 29e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth struct F1 { }; 30e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth F1 f1() { return S1::IS1().member_func(); } 31e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth} 32e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler CarruthN::F1 f1_var = N::f1(); 33e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth 34e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// The name of the friend is not found by unqualified lookup (3.4.1) or by 35e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// qualified lookup (3.4.3) until a matching declaration is provided in that 36e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// namespace scope (either before or after the class definition granting 37e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// friendship). If a friend function is called, its name may be found by the 38e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// name lookup that considers functions from namespaces and classes 39e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// associated with the types of the function arguments (3.4.2). If the name 40e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// in a friend declaration is neither qualified nor a template-id and the 41e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// declaration is a function or an elaborated-type-specifier, the lookup to 42e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// determine whether the entity has been previously declared shall not 43e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth// consider any scopes outside the innermost enclosing namespace. 44e092dec5ea2419f4f614b4a4136ffc85ff8dafc0Chandler Carruth 4532564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregortemplate<typename T> struct X0 { }; 4632564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregorstruct X1 { }; 4732564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor 4832564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregorstruct Y { 4932564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor template<typename T> union X0; 5032564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor template<typename T> friend union X0; 5132564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor 5232564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor union X1; 5332564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor friend union X1; 5432564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor}; 5532564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor 56bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregornamespace N { 57bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor namespace M { 58bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor template<typename T> class X; 59bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor } 60bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor} 61bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor 62bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregornamespace N3 { 63bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor class Y { 64bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor template<typename T> friend class N::M::X; 65bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor }; 66bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor} 67bb6d0a274486e7a090bd88a451299657f9e9bcf6Douglas Gregor 6832564ed7d110bfb87e45ebd23e5211d867725fd8Douglas Gregor// FIXME: Woefully inadequate for testing 693a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall 703a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall// Friends declared as template-ids aren't subject to the restriction 713a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall// on innermost namespaces. 723a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall// rdar://problem/8552377 733a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCallnamespace test5 { 743a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall template <class T> void f(T); 753a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall namespace ns { 763a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall class A { 773a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall friend void f<int>(int); 783a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall static void foo(); // expected-note 2 {{declared private here}} 793a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall }; 803a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall 813a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall // Note that this happens without instantiation. 823a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall template <class T> void f(T) { 833a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall A::foo(); // expected-error {{'foo' is a private member of 'test5::ns::A'}} 843a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall } 853a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall } 863a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall 873a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall template <class T> void f(T) { 883a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall ns::A::foo(); // expected-error {{'foo' is a private member of 'test5::ns::A'}} 893a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall } 903a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall 913a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall template void f<int>(int); 923a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall template void f<long>(long); //expected-note {{instantiation}} 933a84ffd08c22c0ae95a90fc4983c885dfe4df904John McCall} 94