diff mbox

C++ PATCH for c++/64487 (offsetof in templates)

Message ID 54AC5850.5020908@redhat.com
State New
Headers show

Commit Message

Jason Merrill Jan. 6, 2015, 9:49 p.m. UTC
The existing code was assuming that offsetof will always be fully 
instantiated if it goes through tsubst at all.  In general this is an 
invalid assumption; we need to deal with partial instantiation in 
still-dependent context.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox

Patch

commit fbbb80c79e464f12ee0977190189384b3ab9e429
Author: jason <jason@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Tue Jan 6 20:44:46 2015 +0000

    	PR c++/64487
    	* semantics.c (finish_offsetof): Handle templates here.
    	* parser.c (cp_parser_builtin_offsetof): Not here.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@219267 138bc75d-0d04-0410-961f-82ee72b054a4

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 52234de..22dff06 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -8729,15 +8729,7 @@  cp_parser_builtin_offsetof (cp_parser *parser)
     }
 
  success:
-  /* If we're processing a template, we can't finish the semantics yet.
-     Otherwise we can fold the entire expression now.  */
-  if (processing_template_decl)
-    {
-      expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
-      SET_EXPR_LOCATION (expr, loc);
-    }
-  else
-    expr = finish_offsetof (expr, loc);
+  expr = finish_offsetof (expr, loc);
 
  failure:
   parser->integral_constant_expression_p = save_ice_p;
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 551bad1..4365a53 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -3870,6 +3870,15 @@  finish_bases (tree type, bool direct)
 tree
 finish_offsetof (tree expr, location_t loc)
 {
+  /* If we're processing a template, we can't finish the semantics yet.
+     Otherwise we can fold the entire expression now.  */
+  if (processing_template_decl)
+    {
+      expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
+      SET_EXPR_LOCATION (expr, loc);
+      return expr;
+    }
+
   if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
     {
       error ("cannot apply %<offsetof%> to destructor %<~%T%>",
diff --git a/gcc/testsuite/g++.dg/template/offsetof3.C b/gcc/testsuite/g++.dg/template/offsetof3.C
new file mode 100644
index 0000000..b173746
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/offsetof3.C
@@ -0,0 +1,18 @@ 
+// PR c++/64487
+
+struct foo {
+      int member;
+};
+
+template < int N>
+struct bar {};
+
+template <int N>
+struct qux {
+        static bar<N+__builtin_offsetof(foo,member)> static_member;
+};
+
+template <int N>
+bar<N+__builtin_offsetof(foo,member)> qux<N>::static_member;
+
+int main() { }