Line data Source code
1 : /* m2decl.cc provides an interface to GCC decl trees.
2 :
3 : Copyright (C) 2012-2026 Free Software Foundation, Inc.
4 : Contributed by Gaius Mulley <gaius@glam.ac.uk>.
5 :
6 : This file is part of GNU Modula-2.
7 :
8 : GNU Modula-2 is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3, or (at your option)
11 : any later version.
12 :
13 : GNU Modula-2 is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GNU Modula-2; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "gcc-consolidation.h"
23 :
24 : #include "../gm2-lang.h"
25 : #include "../m2-tree.h"
26 :
27 : #define m2decl_c
28 : #include "m2assert.h"
29 : #include "m2block.h"
30 : #include "m2decl.h"
31 : #include "m2expr.h"
32 : #include "m2tree.h"
33 : #include "m2treelib.h"
34 : #include "m2type.h"
35 : #include "m2convert.h"
36 :
37 : extern GTY (()) tree current_function_decl;
38 :
39 : /* Used in BuildStartFunctionType. */
40 : static GTY (()) tree param_type_list;
41 : static GTY (()) tree param_list = NULL_TREE; /* Ready for the next time we
42 : call/define a function. */
43 : #if 0
44 : tree
45 : m2decl_DeclareM2linkStaticInitialization (location_t location,
46 : int ScaffoldStatic)
47 : {
48 : m2block_pushGlobalScope ();
49 : /* Generate: int M2LINK_StaticInitialization = ScaffoldStatic; */
50 : tree init = m2decl_BuildIntegerConstant (ScaffoldStatic);
51 : tree static_init = m2decl_DeclareKnownVariable (location, "m2pim_M2LINK_StaticInitialization",
52 : integer_type_node,
53 : TRUE, FALSE, FALSE, TRUE, NULL_TREE, init);
54 : m2block_popGlobalScope ();
55 : return static_init;
56 : }
57 :
58 :
59 : tree
60 : m2decl_DeclareM2linkForcedModuleInitOrder (location_t location,
61 : const char *RuntimeOverride)
62 : {
63 : m2block_pushGlobalScope ();
64 : /* Generate: const char *ForcedModuleInitOrder = RuntimeOverride; */
65 : tree ptr_to_char = build_pointer_type (char_type_node);
66 : TYPE_READONLY (ptr_to_char) = TRUE;
67 : tree init = m2decl_BuildPtrToTypeString (location, RuntimeOverride, ptr_to_char);
68 : tree forced_order = m2decl_DeclareKnownVariable (location, "m2pim_M2LINK_ForcedModuleInitOrder",
69 : ptr_to_char,
70 : TRUE, FALSE, FALSE, TRUE, NULL_TREE, init);
71 : m2block_popGlobalScope ();
72 : return forced_order;
73 : }
74 : #endif
75 :
76 :
77 : /* DeclareKnownVariable declares a variable to GCC. */
78 :
79 : tree
80 654198 : m2decl_DeclareKnownVariable (location_t location, const char *name, tree type,
81 : bool exported, bool imported, bool istemporary,
82 : bool isglobal, tree scope, tree initial)
83 : {
84 654198 : tree id;
85 654198 : tree decl;
86 :
87 654198 : m2assert_AssertLocation (location);
88 654198 : ASSERT (m2tree_is_type (type), type);
89 654198 : ASSERT_BOOL (isglobal);
90 :
91 654198 : id = get_identifier (name);
92 654198 : type = m2tree_skip_type_decl (type);
93 654198 : decl = build_decl (location, VAR_DECL, id, type);
94 :
95 654198 : DECL_SOURCE_LOCATION (decl) = location;
96 :
97 654198 : DECL_EXTERNAL (decl) = imported;
98 654198 : TREE_STATIC (decl) = isglobal;
99 654198 : TREE_PUBLIC (decl) = exported || imported;
100 :
101 654198 : gcc_assert ((istemporary == 0) || (istemporary == 1));
102 :
103 : /* The variable was not declared by GCC, but by the front end. */
104 654198 : DECL_ARTIFICIAL (decl) = istemporary;
105 : /* If istemporary then we don't want debug info for it. */
106 654198 : DECL_IGNORED_P (decl) = istemporary;
107 : /* If istemporary we don't want even the fancy names of those printed in
108 : -fdump-final-insns= dumps. */
109 654198 : DECL_NAMELESS (decl) = istemporary;
110 :
111 : /* Make the variable writable. */
112 654198 : TREE_READONLY (decl) = 0;
113 :
114 654198 : DECL_CONTEXT (decl) = scope;
115 :
116 654198 : if (initial)
117 0 : DECL_INITIAL (decl) = initial;
118 :
119 654198 : m2block_pushDecl (decl);
120 :
121 654198 : if (DECL_SIZE (decl) == 0)
122 0 : error ("storage size of %qD has not been resolved", decl);
123 :
124 654198 : if ((TREE_PUBLIC (decl) == 0) && DECL_EXTERNAL (decl))
125 0 : internal_error ("inconsistent because %qs",
126 : "PUBLIC_DECL(decl) == 0 && DECL_EXTERNAL(decl) == 1");
127 :
128 654198 : m2block_addDeclExpr (build_stmt (location, DECL_EXPR, decl));
129 :
130 654198 : return decl;
131 : }
132 :
133 : /* DeclareKnownConstant - given a constant, value, of, type, create a
134 : constant in the GCC symbol table. Note that the name of the
135 : constant is not used as _all_ constants are declared in the global
136 : scope. The front end deals with scoping rules - here we declare
137 : all constants with no names in the global scope. This allows
138 : M2SubExp and constant folding routines the liberty of operating
139 : with quadruples which all assume constants can always be
140 : referenced. */
141 :
142 : tree
143 67422 : m2decl_DeclareKnownConstant (location_t location, tree type, tree value)
144 : {
145 67422 : tree decl;
146 :
147 67422 : m2assert_AssertLocation (location);
148 67422 : m2expr_ConstantExpressionWarning (value);
149 67422 : type = m2tree_skip_type_decl (type);
150 67422 : layout_type (type);
151 :
152 67422 : decl = build_decl (location, CONST_DECL, NULL_TREE, type);
153 :
154 67422 : value = copy_node (value);
155 67422 : TREE_TYPE (value) = type;
156 67422 : DECL_INITIAL (decl) = value;
157 67422 : TREE_TYPE (decl) = type;
158 67422 : decl = m2block_global_constant (decl);
159 67422 : return decl;
160 : }
161 :
162 : /* BuildParameterDeclaration - creates and returns one parameter
163 : from, name, and, type. It appends this parameter to the internal
164 : param_type_list. */
165 :
166 : tree
167 7932911 : m2decl_BuildParameterDeclaration (location_t location, const char *name, tree type,
168 : bool isreference)
169 : {
170 7932911 : tree parm_decl;
171 :
172 7932911 : m2assert_AssertLocation (location);
173 7932911 : ASSERT_BOOL (isreference);
174 7932911 : type = m2tree_skip_type_decl (type);
175 7932911 : layout_type (type);
176 7932911 : if (isreference)
177 201093 : type = build_reference_type (type);
178 :
179 7932911 : if (name == NULL)
180 96276 : parm_decl = build_decl (location, PARM_DECL, NULL, type);
181 : else
182 7836635 : parm_decl = build_decl (location, PARM_DECL, get_identifier (name), type);
183 7932911 : DECL_ARG_TYPE (parm_decl) = type;
184 7932911 : if (isreference)
185 201093 : TREE_READONLY (parm_decl) = TRUE;
186 :
187 7932911 : param_list = chainon (parm_decl, param_list);
188 7932911 : layout_type (type);
189 7932911 : param_type_list = tree_cons (NULL_TREE, type, param_type_list);
190 7932911 : return parm_decl;
191 : }
192 :
193 : /* BuildStartFunctionDeclaration - initializes global variables ready
194 : for building a function. */
195 :
196 : void
197 3923988 : m2decl_BuildStartFunctionDeclaration (bool uses_varargs)
198 : {
199 3923988 : if (uses_varargs)
200 11448 : param_type_list = NULL_TREE;
201 : else
202 3912540 : param_type_list = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
203 3923988 : param_list = NULL_TREE; /* Ready for when we define a function. */
204 3923988 : }
205 :
206 : /* BuildEndFunctionDeclaration - build a function which will return a
207 : value of returntype. The arguments have been created by
208 : BuildParameterDeclaration. */
209 :
210 : tree
211 3923988 : m2decl_BuildEndFunctionDeclaration (location_t location_begin,
212 : location_t location_end, const char *name,
213 : tree returntype, bool isexternal,
214 : bool isnested, bool ispublic, bool isnoreturn)
215 : {
216 3923988 : tree fntype;
217 3923988 : tree fndecl;
218 :
219 3923988 : m2assert_AssertLocation (location_begin);
220 3923988 : m2assert_AssertLocation (location_end);
221 3923988 : ASSERT_BOOL (isexternal);
222 3923988 : ASSERT_BOOL (isnested);
223 3923988 : ASSERT_BOOL (ispublic);
224 3923988 : returntype = m2tree_skip_type_decl (returntype);
225 : /* The function type depends on the return type and type of args,
226 : both of which we have created in BuildParameterDeclaration */
227 3923988 : if (returntype == NULL_TREE)
228 1701734 : returntype = void_type_node;
229 2222254 : else if (TREE_CODE (returntype) == FUNCTION_TYPE)
230 0 : returntype = ptr_type_node;
231 :
232 3923988 : fntype = build_function_type (returntype, param_type_list);
233 3923988 : fndecl = build_decl (location_begin, FUNCTION_DECL, get_identifier (name),
234 : fntype);
235 :
236 3923988 : if (isexternal)
237 3771584 : ASSERT_CONDITION (ispublic);
238 :
239 3923988 : DECL_EXTERNAL (fndecl) = isexternal;
240 3923988 : TREE_PUBLIC (fndecl) = ispublic;
241 3923988 : TREE_STATIC (fndecl) = (!isexternal);
242 3923988 : DECL_ARGUMENTS (fndecl) = param_list;
243 7847976 : DECL_RESULT (fndecl)
244 3923988 : = build_decl (location_end, RESULT_DECL, NULL_TREE, returntype);
245 3923988 : DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
246 3923988 : TREE_TYPE (fndecl) = fntype;
247 3923988 : TREE_THIS_VOLATILE (fndecl) = isnoreturn;
248 :
249 3923988 : DECL_SOURCE_LOCATION (fndecl) = location_begin;
250 :
251 : /* Prevent the optimizer from removing it if it is public. */
252 3923988 : if (TREE_PUBLIC (fndecl))
253 3792933 : gm2_mark_addressable (fndecl);
254 :
255 3923988 : m2block_pushDecl (fndecl);
256 :
257 3923988 : rest_of_decl_compilation (fndecl, 1, 0);
258 3923988 : param_list
259 3923988 : = NULL_TREE; /* Ready for the next time we call/define a function. */
260 3923988 : return fndecl;
261 : }
262 :
263 : /* BuildModuleCtor creates the per module constructor used as part of
264 : the dynamic linking scaffold. */
265 :
266 : void
267 15184 : m2decl_BuildModuleCtor (tree module_ctor)
268 : {
269 15184 : decl_init_priority_insert (module_ctor, DEFAULT_INIT_PRIORITY);
270 15184 : }
271 :
272 : /* DeclareModuleCtor configures the function to be used as a ctor. */
273 :
274 : tree
275 15184 : m2decl_DeclareModuleCtor (tree decl)
276 : {
277 : /* Declare module_ctor (). */
278 15184 : TREE_PUBLIC (decl) = 1;
279 15184 : DECL_ARTIFICIAL (decl) = 1;
280 15184 : DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
281 15184 : DECL_VISIBILITY_SPECIFIED (decl) = 1;
282 15184 : DECL_STATIC_CONSTRUCTOR (decl) = 1;
283 15184 : return decl;
284 : }
285 :
286 : /* BuildConstLiteralNumber - returns a GCC TREE built from the
287 : string, str. It assumes that, str, represents a legal number in
288 : Modula-2. It always returns a positive value. */
289 :
290 : tree
291 626772 : m2decl_BuildConstLiteralNumber (location_t location, const char *str,
292 : unsigned int base, bool issueError)
293 : {
294 626772 : widest_int wval;
295 626772 : tree value;
296 626772 : bool overflow = m2expr_OverflowZType (location, str, base, issueError);
297 626772 : if (overflow)
298 6 : value = m2expr_GetIntegerZero (location);
299 : else
300 : {
301 626766 : overflow = m2expr_StrToWideInt (location, str, base, wval, issueError);
302 626766 : if (overflow)
303 0 : value = m2expr_GetIntegerZero (location);
304 : else
305 : {
306 626766 : value = wide_int_to_tree (m2type_GetM2ZType (), wval);
307 626766 : overflow = m2expr_TreeOverflow (value);
308 : }
309 : }
310 626772 : if (issueError && overflow)
311 0 : error_at (location, "constant %qs is too large", str);
312 626772 : return m2block_RememberConstant (value);
313 626772 : }
314 :
315 : /* BuildCStringConstant - creates a string constant given a, string,
316 : and, length. */
317 :
318 : tree
319 245507 : m2decl_BuildCStringConstant (const char *string, int length)
320 : {
321 245507 : tree elem, index, type;
322 :
323 : /* +1 ensures that we always nul terminate our strings. */
324 245507 : elem = build_type_variant (char_type_node, 1, 0);
325 245507 : index = build_index_type (build_int_cst (integer_type_node, length + 1));
326 245507 : type = build_array_type (elem, index);
327 245507 : return m2decl_BuildStringConstantType (length + 1, string, type);
328 : }
329 :
330 : /* BuildStringConstant - creates a string constant given a, string,
331 : and, length. */
332 :
333 : tree
334 232167 : m2decl_BuildStringConstant (const char *string, int length)
335 : {
336 232167 : tree elem, index, type;
337 :
338 232167 : elem = build_type_variant (char_type_node, 1, 0);
339 232167 : index = build_index_type (build_int_cst (integer_type_node, length));
340 232167 : type = build_array_type (elem, index);
341 232167 : return m2decl_BuildStringConstantType (length, string, type);
342 : // maybe_wrap_with_location
343 : }
344 :
345 :
346 : tree
347 0 : m2decl_BuildPtrToTypeString (location_t location, const char *string, tree type)
348 : {
349 0 : if ((string == NULL) || (strlen (string) == 0))
350 0 : return m2convert_BuildConvert (location, type,
351 : m2decl_BuildIntegerConstant (0),
352 0 : FALSE);
353 0 : return build_string_literal (strlen (string), string);
354 : }
355 :
356 :
357 : /* BuildIntegerConstant - return a tree containing the integer value. */
358 :
359 : tree
360 6363464 : m2decl_BuildIntegerConstant (int value)
361 : {
362 6363464 : switch (value)
363 : {
364 :
365 1218984 : case 0:
366 1218984 : return integer_zero_node;
367 619766 : case 1:
368 619766 : return integer_one_node;
369 :
370 4524714 : default:
371 4524714 : return m2block_RememberConstant (
372 4524714 : build_int_cst (integer_type_node, value));
373 : }
374 : }
375 :
376 : /* BuildStringConstantType - builds a string constant with a type. */
377 :
378 : tree
379 480986 : m2decl_BuildStringConstantType (int length, const char *string, tree type)
380 : {
381 480986 : tree id = build_string (length, string);
382 :
383 480986 : TREE_TYPE (id) = type;
384 480986 : TREE_CONSTANT (id) = TRUE;
385 480986 : TREE_READONLY (id) = TRUE;
386 480986 : TREE_STATIC (id) = TRUE;
387 :
388 480986 : return m2block_RememberConstant (id);
389 : }
390 :
391 : /* GetBitsPerWord - returns the number of bits in a WORD. */
392 :
393 : int
394 0 : m2decl_GetBitsPerWord (void)
395 : {
396 0 : return BITS_PER_WORD;
397 : }
398 :
399 : /* GetBitsPerInt - returns the number of bits in a INTEGER. */
400 :
401 : int
402 32092 : m2decl_GetBitsPerInt (void)
403 : {
404 32092 : return INT_TYPE_SIZE;
405 : }
406 :
407 : /* GetBitsPerBitset - returns the number of bits in a BITSET. */
408 :
409 : int
410 102164 : m2decl_GetBitsPerBitset (void)
411 : {
412 102164 : return SET_WORD_SIZE;
413 : }
414 :
415 : /* GetBitsPerUnit - returns the number of bits in a UNIT. */
416 :
417 : int
418 1932 : m2decl_GetBitsPerUnit (void)
419 : {
420 1932 : return BITS_PER_UNIT;
421 : }
422 :
423 : /* m2decl_GetDeclContext - returns the DECL_CONTEXT of tree, t. */
424 :
425 : tree
426 0 : m2decl_GetDeclContext (tree t)
427 : {
428 0 : return DECL_CONTEXT (t);
429 : }
430 :
431 : #include "gt-m2-m2decl.h"
|