xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/cp/expr.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /* Convert language-specific tree expression to rtl instructions,
2    for GNU compiler.
3    Copyright (C) 1988-2016 Free Software Foundation, Inc.
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11 
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 
27 /* Expand C++-specific constants.  Currently, this means PTRMEM_CST.  */
28 
29 tree
30 cplus_expand_constant (tree cst)
31 {
32   switch (TREE_CODE (cst))
33     {
34     case PTRMEM_CST:
35       {
36 	tree type = TREE_TYPE (cst);
37 	tree member;
38 
39 	/* Find the member.  */
40 	member = PTRMEM_CST_MEMBER (cst);
41 
42 	/* We can't lower this until the class is complete.  */
43 	if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
44 	  return cst;
45 
46 	if (TREE_CODE (member) == FIELD_DECL)
47 	  {
48 	    /* Find the offset for the field.  */
49 	    cst = byte_position (member);
50 	    while (!same_type_p (DECL_CONTEXT (member),
51 				 TYPE_PTRMEM_CLASS_TYPE (type)))
52 	      {
53 		/* The MEMBER must have been nestled within an
54 		   anonymous aggregate contained in TYPE.  Find the
55 		   anonymous aggregate.  */
56 		member = lookup_anon_field (TYPE_PTRMEM_CLASS_TYPE (type),
57 					    DECL_CONTEXT (member));
58 		cst = size_binop (PLUS_EXPR, cst, byte_position (member));
59 	      }
60 	    cst = fold (build_nop (type, cst));
61 	  }
62 	else
63 	  {
64 	    tree delta;
65 	    tree pfn;
66 
67 	    expand_ptrmemfunc_cst (cst, &delta, &pfn);
68 	    cst = build_ptrmemfunc1 (type, delta, pfn);
69 	  }
70       }
71       break;
72 
73     case CONSTRUCTOR:
74       {
75 	constructor_elt *elt;
76 	unsigned HOST_WIDE_INT idx;
77 	FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (cst), idx, elt)
78 	  elt->value = cplus_expand_constant (elt->value);
79       }
80 
81     default:
82       /* There's nothing to do.  */
83       break;
84     }
85 
86   return cst;
87 }
88 
89 /* Called whenever the expression EXPR is used in an rvalue context.
90    When REJECT_BUILTIN is true the expression is checked to make sure
91    it doesn't make it possible to obtain the address of a GCC built-in
92    function with no library fallback (or any of its bits, such as in
93    a conversion to bool).  */
94 tree
95 mark_rvalue_use (tree expr,
96 		 location_t loc /* = UNKNOWN_LOCATION */,
97 		 bool reject_builtin /* = true */)
98 {
99   if (reject_builtin && reject_gcc_builtin (expr, loc))
100     return error_mark_node;
101 
102   mark_exp_read (expr);
103   return expr;
104 }
105 
106 /* Called whenever an expression is used in an lvalue context.  */
107 
108 tree
109 mark_lvalue_use (tree expr)
110 {
111   mark_exp_read (expr);
112   return expr;
113 }
114 
115 /* Called whenever an expression is used in a type use context.  */
116 
117 tree
118 mark_type_use (tree expr)
119 {
120   mark_exp_read (expr);
121   return expr;
122 }
123 
124 /* Mark EXP as read, not just set, for set but not used -Wunused
125    warning purposes.  */
126 
127 void
128 mark_exp_read (tree exp)
129 {
130   if (exp == NULL)
131     return;
132 
133   switch (TREE_CODE (exp))
134     {
135     case VAR_DECL:
136     case PARM_DECL:
137       DECL_READ_P (exp) = 1;
138       break;
139     case ARRAY_REF:
140     case COMPONENT_REF:
141     case MODIFY_EXPR:
142     case REALPART_EXPR:
143     case IMAGPART_EXPR:
144     CASE_CONVERT:
145     case ADDR_EXPR:
146     case INDIRECT_REF:
147     case FLOAT_EXPR:
148     case NON_DEPENDENT_EXPR:
149       mark_exp_read (TREE_OPERAND (exp, 0));
150       break;
151     case COMPOUND_EXPR:
152       mark_exp_read (TREE_OPERAND (exp, 1));
153       break;
154     case COND_EXPR:
155       if (TREE_OPERAND (exp, 1))
156 	mark_exp_read (TREE_OPERAND (exp, 1));
157       if (TREE_OPERAND (exp, 2))
158 	mark_exp_read (TREE_OPERAND (exp, 2));
159       break;
160     default:
161       break;
162     }
163 }
164 
165