This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] Stage 2: Implement


Hi

This is the work to implement access checking of template I
proposed as a stage 2 project for GCC 4.1.  It work is done
but is splitted into multiple parts due to its size.  And this
is the first in the series of the patches.

This part fixes protected member access inside class templates.
A testcase is included in the patch and cannot be compiled with
the current mainline GCC.  There is some use of this in the
g++ testsuite that future work in the series will expose
this bug.

The problem is a flaw handling CLASSTYPE_BEFRIENDING_CLASSES
for class templates.  While DECL_BEFRIENDING_CLASSES of
function templates are correctly tsubst'ed, we forget to tsubst
CLASSTYPE_BEFRIENDING_CLASSES.

Note that we do tsubst for function templates in tsubst_decl
because checking access of function parameter and return type
during tsubst_decl call mundate that.  But for class templates
we only need this when the class is actually instantiated, hence
the change is in instantiate_class_template.

Tested on i686-pc-linux-gnu. OK for the mainline?

--Kriang

2005-05-08  Kriang Lerdsuwanakij  <lerdsuwa@users.sourceforge.net>

	Defer access check until template instantiation 1/n
	* pt.c (instantiate_class_template): Substitute
	CLASSTYPE_BEFRIENDING_CLASSES.

2005-05-08  Kriang Lerdsuwanakij  <lerdsuwa@users.sourceforge.net>

	* g++.dg/template/access16.C: New test.


diff -cprN gcc-main-save/gcc/cp/pt.c gcc-main-new/gcc/cp/pt.c
*** gcc-main-save/gcc/cp/pt.c	2005-05-08 16:24:13.000000000 +0700
--- gcc-main-new/gcc/cp/pt.c	2005-05-08 18:42:32.000000000 +0700
*************** instantiate_class_template (tree type)
*** 5846,5851 ****
--- 5846,5862 ----
  	}
      }
  
+   /* This is required for protected member access [class.protected].
+      We use TREE_TYPE (TEMPLATE) rather than PATTERN here because
+      the information is stored in the general template while PATTERN
+      might be a partial specialization.  */
+   for (t = CLASSTYPE_BEFRIENDING_CLASSES (TREE_TYPE (template));
+        t; t = TREE_CHAIN (t))
+     CLASSTYPE_BEFRIENDING_CLASSES (type)
+       = tree_cons (NULL_TREE,
+ 		   tsubst (TREE_VALUE (t), args, tf_none, NULL_TREE),
+ 		   CLASSTYPE_BEFRIENDING_CLASSES (type));
+ 
    /* Set the file and line number information to whatever is given for
       the class itself.  This puts error messages involving generated
       implicit functions at a predictable point, and the same point
diff -cprN gcc-main-save/gcc/testsuite/g++.dg/template/access16.C gcc-main-new/gcc/testsuite/g++.dg/template/access16.C
*** gcc-main-save/gcc/testsuite/g++.dg/template/access16.C	1970-01-01 07:00:00.000000000 +0700
--- gcc-main-new/gcc/testsuite/g++.dg/template/access16.C	2005-05-08 18:41:44.000000000 +0700
***************
*** 0 ****
--- 1,26 ----
+ // Copyright (C) 2005 Free Software Foundation
+ // Contributed by Kriang Lerdsuwanakij <lerdsuwa@users.sourceforge.net>
+ // { dg-do compile }
+ 
+ // Protected member access [class.protected] and partial specialization
+ 
+ class B {
+   protected:
+     static int i;
+ };
+ 
+ template <class T> class C;
+ 
+ class D : public B {
+   template <class T> friend class C;
+ };
+ 
+ template <class T> class C<T*> {
+   void f();
+ };
+ 
+ template <class T> void C<T*>::f() {
+   B::i = 0;
+ }
+ 
+ template class C<int*>;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]