ANSI Doc No: X3J16/93-0107 ISO Doc No: WG21/N0314 Date: July 30, 1993 Project: Programming Language C++ Ref Doc: Reply To: Eric J. Krohn krohn@bae.bellcore.com Bellcore Erwin Unruh erwin.unruh@stm.mchp.sni.de Siemens Nixdorf Information Systems New Grammar for Namespaces 1. Abstract The namespace proposal (N0312=93-0105) approved at the Munich meeting has some notable omissions from the grammar rules: o connection to the rest of the grammar, o compound names in a using-declarations. o syntax for using namespace qualified names, This paper proposes new grammar rules which fix the first two items. The third item will be addressed in a separate paper which will attempt to unify namespace and class qualification. 2. Context-dependent Keyword The namespace proposal introduces namespace-name as a possibly qualified name. namespace-name: identifier namespace-name :: identifier This usage is at odds with class-name, enum-name, typedef-name, and template-name. For uniformity, we propose adding namespace- name as a new context-dependent keyword and adding nested- namespace-specifier and qualified-namespace-specifier.1 namespace-name: identifier __________ 1. This is analogous to the class-name, nested-class-specifier, and qualified-class-specifier already in the grammar. July 30, 1993 Page 2 X3J16/93-0107 WG21/N0314 nested-namespace-specifier: namespace-name namespace-name :: nested-namespace-specifier qualified-namespace-specifier: ::(opt) nested-namespace-specifier 3. Rewrite of Other Namespace Grammar Rules There is no such rule as declaration-list, so we must instead use declaration-seq in namespace-definition. namespace-definition: namespace identifier(opt) { declaration-seq(opt) } Since we changed the meaning of namespace-name and since we wish to use a leading :: when specifying a namespace, replace namespace-name with qualified-namespace-specifier. namespace-alias-definition: namespace identifier = qualified-namespace-specifier ; using-directive: using namespace qualified-namespace-specifier ; The grammar rule for using-declaration excludes using operator- function-id or conversion-function-id, though document X3J16/93- 0055 says: Note that a name need not be a simple identifier; for example, it can be an operator-name. We allow operators by replacing identifier with id-expression. This change has the (possibly undesired) side effect of syntactically allowing using N1::String::~String; Additionally, we change using-declaration to allow using ::f; since that was noted as an oversight by Bjarne Stroustrup in c++std-ext-1544. using-declaration: using qualified-namespace-specifier(opt) :: id-expression ; 4. Connection to the Rest of the Grammar We need to specify where the new namespace rules may occur in programs. The text of the proposal states A namespace-definition or a namespace-alias-definition is a declaration. A namespace-definition can only occur at the global scope or within another namespace. A using-declaration can be July 30, 1993 Page 3 X3J16/93-0107 WG21/N0314 used as a declaration-statement, a member-declaration, at the global scope, or within another namespace. A using- directive can be used as a declaration-statement, at the global scope, or within another namespace (that is not a class). From this, we propose adding to declaration and member- declaration the following new productions. declaration: decl-specifier-seq(opt) init-declarator-list(opt) ; asm-definition function-definition template-declaration linkage-specification namespace-definition namespace-alias-definition using-declaration using-directive member-declaration: decl-specifier-seq(opt) member-declarator-list(opt) ; function-definition ;(opt) qualified-id ; using-declaration These rules are somewhat broader than the semantic restrictions given in the proposal (and presumably to be incorporated into the working paper).2 We rely on the semantic restrictions to eliminate some syntactic possibilities because it simplifies the grammar rules.3 __________ 2. For example, they syntactically allow namespace-definition in a declaration-statement or a template-declaration, and namespace-alias-definition in a template-declaration. 3. The chapter on templates uses a similar device when it gives the grammar rule template-declaration: template < template-parameter-list > declaration and then states The declaration in a template-declaration must declare or define a function or a class. July 30, 1993 Page 4 X3J16/93-0107 WG21/N0314 5. Future Changes This paper does not include the grammar changes necessary to use namespace qualified names apart from using-declarations and using-directives. The changes are much the same as those needed to use nested classes. Because of the number of nearly duplicated rules, a separate proposal will be presented to unify qualification with namespaces and classes. July 30, 1993 Page 5 X3J16/93-0107 WG21/N0314 Appendix I - Grammar from Namespace Proposal namespace-definition: namespace identifier(opt) { declaration-list(opt) } namespace-alias-definition: namespace identifier = namespace-name ; using-declaration: using namespace-name :: identifier ; using-directive: using namespace namespace-name ; namespace-name: identifier namespace-name :: identifier July 30, 1993 Page 6 X3J16/93-0107 WG21/N0314 Appendix II - Grammar Changes Proposed grammar changes showing all proposed modifications. namespace-name: identifier nested-namespace-specifier: namespace-name namespace-name :: nested-namespace-specifier qualified-namespace-specifier: ::(opt) nested-namespace-specifier namespace-definition: namespace identifier(opt) { declaration-seq(opt) } namespace-alias-definition: namespace identifier = qualified-namespace-specifier ; using-declaration: using qualified-namespace-specifier(opt) :: id-expression ; using-directive: using namespace qualified-namespace-specifier ; declaration: decl-specifier-seq(opt) init-declarator-list(opt) ; asm-definition function-definition template-declaration linkage-specification namespace-definition namespace-alias-definition using-declaration using-directive member-declaration: decl-specifier-seq(opt) member-declarator-list(opt) ; function-definition ;(opt) qualified-id ; using-declaration