1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- Pragma handling is isolated in a separate package
27 -- (logically this processing belongs in chapter 4)
29 with Namet
; use Namet
;
31 with Snames
; use Snames
;
32 with Types
; use Types
;
36 -- The following table lists all pragmas that emulate an Ada 2012 aspect
38 Aspect_Specifying_Pragma
: constant array (Pragma_Id
) of Boolean :=
39 (Pragma_Abstract_State
=> True,
40 Pragma_All_Calls_Remote
=> True,
41 Pragma_Annotate
=> True,
42 Pragma_Async_Readers
=> True,
43 Pragma_Async_Writers
=> True,
44 Pragma_Asynchronous
=> True,
45 Pragma_Atomic
=> True,
46 Pragma_Atomic_Components
=> True,
47 Pragma_Attach_Handler
=> True,
48 Pragma_Contract_Cases
=> True,
49 Pragma_Convention
=> True,
51 Pragma_Default_Initial_Condition
=> True,
52 Pragma_Default_Storage_Pool
=> True,
53 Pragma_Depends
=> True,
54 Pragma_Discard_Names
=> True,
55 Pragma_Dispatching_Domain
=> True,
56 Pragma_Effective_Reads
=> True,
57 Pragma_Effective_Writes
=> True,
58 Pragma_Elaborate_Body
=> True,
59 Pragma_Export
=> True,
60 Pragma_Extensions_Visible
=> True,
61 Pragma_Favor_Top_Level
=> True,
63 Pragma_Global
=> True,
64 Pragma_Import
=> True,
65 Pragma_Independent
=> True,
66 Pragma_Independent_Components
=> True,
67 Pragma_Initial_Condition
=> True,
68 Pragma_Initializes
=> True,
69 Pragma_Inline
=> True,
70 Pragma_Inline_Always
=> True,
71 Pragma_Interrupt_Handler
=> True,
72 Pragma_Interrupt_Priority
=> True,
73 Pragma_Invariant
=> True,
74 Pragma_Linker_Section
=> True,
75 Pragma_Lock_Free
=> True,
76 Pragma_No_Elaboration_Code_All
=> True,
77 Pragma_No_Return
=> True,
78 Pragma_Obsolescent
=> True,
80 Pragma_Part_Of
=> True,
81 Pragma_Persistent_BSS
=> True,
83 Pragma_Post_Class
=> True,
84 Pragma_Postcondition
=> True,
86 Pragma_Pre_Class
=> True,
87 Pragma_Precondition
=> True,
88 Pragma_Predicate
=> True,
89 Pragma_Preelaborable_Initialization
=> True,
90 Pragma_Preelaborate
=> True,
91 Pragma_Priority
=> True,
93 Pragma_Pure_Function
=> True,
94 Pragma_Refined_Depends
=> True,
95 Pragma_Refined_Global
=> True,
96 Pragma_Refined_Post
=> True,
97 Pragma_Refined_State
=> True,
98 Pragma_Relative_Deadline
=> True,
99 Pragma_Remote_Access_Type
=> True,
100 Pragma_Remote_Call_Interface
=> True,
101 Pragma_Remote_Types
=> True,
102 Pragma_Shared
=> True,
103 Pragma_Shared_Passive
=> True,
104 Pragma_Simple_Storage_Pool_Type
=> True,
105 Pragma_SPARK_Mode
=> True,
106 Pragma_Storage_Size
=> True,
107 Pragma_Suppress
=> True,
108 Pragma_Suppress_Debug_Info
=> True,
109 Pragma_Suppress_Initialization
=> True,
110 Pragma_Test_Case
=> True,
111 Pragma_Thread_Local_Storage
=> True,
112 Pragma_Type_Invariant
=> True,
113 Pragma_Unchecked_Union
=> True,
114 Pragma_Universal_Aliasing
=> True,
115 Pragma_Universal_Data
=> True,
116 Pragma_Unmodified
=> True,
117 Pragma_Unreferenced
=> True,
118 Pragma_Unreferenced_Objects
=> True,
119 Pragma_Unsuppress
=> True,
120 Pragma_Volatile
=> True,
121 Pragma_Volatile_Components
=> True,
122 Pragma_Volatile_Full_Access
=> True,
123 Pragma_Warnings
=> True,
126 -- The following table lists all pragmas that act as an assertion
129 Assertion_Expression_Pragma
: constant array (Pragma_Id
) of Boolean :=
130 (Pragma_Assert
=> True,
131 Pragma_Assert_And_Cut
=> True,
132 Pragma_Assume
=> True,
133 Pragma_Check
=> True,
134 Pragma_Contract_Cases
=> True,
135 Pragma_Default_Initial_Condition
=> True,
136 Pragma_Initial_Condition
=> True,
137 Pragma_Invariant
=> True,
138 Pragma_Loop_Invariant
=> True,
139 Pragma_Loop_Variant
=> True,
141 Pragma_Post_Class
=> True,
142 Pragma_Postcondition
=> True,
144 Pragma_Pre_Class
=> True,
145 Pragma_Precondition
=> True,
146 Pragma_Predicate
=> True,
147 Pragma_Refined_Post
=> True,
148 Pragma_Test_Case
=> True,
149 Pragma_Type_Invariant
=> True,
150 Pragma_Type_Invariant_Class
=> True,
153 -- The following table lists all the implementation-defined pragmas that
154 -- may apply to a body stub (no language defined pragmas apply). The table
155 -- should be synchronized with Aspect_On_Body_Or_Stub_OK in unit Aspects if
156 -- the pragmas below implement an aspect.
158 Pragma_On_Body_Or_Stub_OK
: constant array (Pragma_Id
) of Boolean :=
159 (Pragma_Refined_Depends
=> True,
160 Pragma_Refined_Global
=> True,
161 Pragma_Refined_Post
=> True,
162 Pragma_SPARK_Mode
=> True,
163 Pragma_Warnings
=> True,
170 procedure Analyze_Pragma
(N
: Node_Id
);
171 -- Analyze procedure for pragma reference node N
173 procedure Analyze_Contract_Cases_In_Decl_Part
(N
: Node_Id
);
174 -- Perform full analysis and expansion of delayed pragma Contract_Cases
176 procedure Analyze_Depends_In_Decl_Part
(N
: Node_Id
);
177 -- Perform full analysis of delayed pragma Depends. This routine is also
178 -- capable of performing basic analysis of pragma Refined_Depends.
180 procedure Analyze_External_Property_In_Decl_Part
182 Expr_Val
: out Boolean);
183 -- Perform full analysis of delayed pragmas Async_Readers, Async_Writers,
184 -- Effective_Reads and Effective_Writes. Flag Expr_Val contains the Boolean
185 -- argument of the pragma or a default True if no argument is present.
187 procedure Analyze_Global_In_Decl_Part
(N
: Node_Id
);
188 -- Perform full analysis of delayed pragma Global. This routine is also
189 -- capable of performing basic analysis of pragma Refind_Global.
191 procedure Analyze_Initial_Condition_In_Decl_Part
(N
: Node_Id
);
192 -- Perform full analysis of delayed pragma Initial_Condition
194 procedure Analyze_Initializes_In_Decl_Part
(N
: Node_Id
);
195 -- Perform full analysis of delayed pragma Initializes
197 procedure Analyze_Pre_Post_Condition_In_Decl_Part
(N
: Node_Id
);
198 -- Perform preanalysis of [refined] precondition or postcondition pragma
199 -- N that appears on a subprogram declaration or body [stub].
201 procedure Analyze_Refined_Depends_In_Decl_Part
(N
: Node_Id
);
202 -- Preform full analysis of delayed pragma Refined_Depends. This routine
203 -- uses Analyze_Depends_In_Decl_Part as a starting point, then performs
204 -- various consistency checks between Depends and Refined_Depends.
206 procedure Analyze_Refined_Global_In_Decl_Part
(N
: Node_Id
);
207 -- Perform full analysis of delayed pragma Refined_Global. This routine
208 -- uses Analyze_Global_In_Decl_Part as a starting point, then performs
209 -- various consistency checks between Global and Refined_Global.
211 procedure Analyze_Refined_State_In_Decl_Part
(N
: Node_Id
);
212 -- Perform full analysis of delayed pragma Refined_State
214 procedure Analyze_Test_Case_In_Decl_Part
(N
: Node_Id
);
215 -- Perform preanalysis of pragma Test_Case
217 procedure Check_Applicable_Policy
(N
: Node_Id
);
218 -- N is either an N_Aspect or an N_Pragma node. There are two cases. If
219 -- the name of the aspect or pragma is not one of those recognized as
220 -- an assertion kind by an Assertion_Policy pragma, then the call has
221 -- no effect. Note that in the case of a pragma derived from an aspect,
222 -- the name we use for the purpose of this procedure is the aspect name,
223 -- which may be different from the pragma name (e.g. Precondition for
224 -- Pre aspect). In addition, 'Class aspects are recognized (and the
225 -- corresponding special names used in the processing).
227 -- If the name is a valid assertion kind name, then the Check_Policy pragma
228 -- chain is checked for a matching entry (or for an Assertion entry which
229 -- matches all possibilities). If a matching entry is found then the policy
230 -- is checked. If it is On or Check, then the Is_Checked flag is set in
231 -- the aspect or pragma node. If it is Off, Ignore, or Disable, then the
232 -- Is_Ignored flag is set in the aspect or pragma node. Additionally for
233 -- policy Disable, the Is_Disabled flag is set.
235 -- If no matching Check_Policy pragma is found then the effect depends on
236 -- whether -gnata was used, if so, then the call has no effect, otherwise
237 -- Is_Ignored (but not Is_Disabled) is set True.
239 procedure Check_External_Properties
245 -- Flags AR, AW, ER and EW denote the static values of external properties
246 -- Async_Readers, Async_Writers, Effective_Reads and Effective_Writes. Item
247 -- is the related variable or state. Ensure legality of the combination and
248 -- issue an error for an illegal combination.
250 function Check_Kind
(Nam
: Name_Id
) return Name_Id
;
251 -- This function is used in connection with pragmas Assert, Check,
252 -- and assertion aspects and pragmas, to determine if Check pragmas
253 -- (or corresponding assertion aspects or pragmas) are currently active
254 -- as determined by the presence of -gnata on the command line (which
255 -- sets the default), and the appearance of pragmas Check_Policy and
256 -- Assertion_Policy as configuration pragmas either in a configuration
257 -- pragma file, or at the start of the current unit, or locally given
258 -- Check_Policy and Assertion_Policy pragmas that are currently active.
260 -- The value returned is one of the names Check, Ignore, Disable (On
261 -- returns Check, and Off returns Ignore).
263 -- Note: for assertion kinds Pre'Class, Post'Class, Invariant'Class,
264 -- and Type_Invariant'Class, the name passed is Name_uPre, Name_uPost,
265 -- Name_uInvariant, or Name_uType_Invariant, which corresponds to _Pre,
266 -- _Post, _Invariant, or _Type_Invariant, which are special names used
267 -- in identifiers to represent these attribute references.
269 procedure Check_Missing_Part_Of
(Item_Id
: Entity_Id
);
270 -- Determine whether the placement within the state space of an abstract
271 -- state, variable or package instantiation denoted by Item_Id requires the
272 -- use of indicator/option Part_Of. If this is the case, emit an error.
274 procedure Collect_Subprogram_Inputs_Outputs
275 (Subp_Id
: Entity_Id
;
276 Synthesize
: Boolean := False;
277 Subp_Inputs
: in out Elist_Id
;
278 Subp_Outputs
: in out Elist_Id
;
279 Global_Seen
: out Boolean);
280 -- Subsidiary to the analysis of pragmas Depends, Global, Refined_Depends
281 -- and Refined_Global. The routine is also used by GNATprove. Collect all
282 -- inputs and outputs of subprogram Subp_Id in lists Subp_Inputs (inputs)
283 -- and Subp_Outputs (outputs). The inputs and outputs are gathered from:
284 -- 1) The formal parameters of the subprogram
285 -- 2) The generic formal parameters of the generic subprogram
286 -- 3) The items of pragma [Refined_]Global
288 -- 4) The items of pragma [Refined_]Depends if there is no pragma
289 -- [Refined_]Global present and flag Synthesize is set to True.
290 -- If the subprogram has no inputs and/or outputs, then the returned list
291 -- is No_Elist. Flag Global_Seen is set when the related subprogram has
292 -- pragma [Refined_]Global.
294 function Delay_Config_Pragma_Analyze
(N
: Node_Id
) return Boolean;
295 -- N is a pragma appearing in a configuration pragma file. Most such
296 -- pragmas are analyzed when the file is read, before parsing and analyzing
297 -- the main unit. However, the analysis of certain pragmas results in
298 -- adding information to the compiled main unit, and this cannot be done
299 -- till the main unit is processed. Such pragmas return True from this
300 -- function and in Frontend pragmas where Delay_Config_Pragma_Analyze is
301 -- True have their analysis delayed until after the main program is parsed
304 function Find_Related_Package_Or_Body
306 Do_Checks
: Boolean := False) return Node_Id
;
307 -- Subsidiary to the analysis of pragmas Abstract_State, Initial_Condition,
308 -- Initializes and Refined_State. Find the declaration of the related
309 -- package [body] subject to pragma Prag. The return value is either
310 -- N_Package_Declaration, N_Package_Body or Empty if the placement of
311 -- the pragma is illegal. If flag Do_Checks is set, the routine reports
312 -- duplicate pragmas.
314 function Find_Related_Subprogram_Or_Body
316 Do_Checks
: Boolean := False) return Node_Id
;
317 -- Subsidiary to the analysis of pragmas Contract_Cases, Depends, Global,
318 -- Refined_Depends, Refined_Global and Refined_Post and attribute 'Result.
319 -- Find the declaration of the related subprogram [body or stub] subject
320 -- to pragma Prag. If flag Do_Checks is set, the routine reports duplicate
321 -- pragmas and detects improper use of refinement pragmas in stand alone
322 -- expression functions. The returned value depends on the related pragma
324 -- 1) Pragmas Contract_Cases, Depends and Global yield the corresponding
325 -- N_Subprogram_Declaration node or if the pragma applies to a stand
326 -- alone body, the N_Subprogram_Body node or Empty if illegal.
327 -- 2) Pragmas Refined_Depends, Refined_Global and Refined_Post yield
328 -- N_Subprogram_Body or N_Subprogram_Body_Stub nodes or Empty if
331 function Get_Argument
333 Context_Id
: Node_Id
:= Empty
) return Node_Id
;
334 -- Obtain the argument of pragma Prag depending on context and the nature
335 -- of the pragma. The argument is extracted in the following manner:
337 -- When the pragma is generated from an aspect, return the corresponding
338 -- aspect for ASIS or when Context_Id denotes a generic unit.
340 -- Otherwise return the first argument of Prag
342 -- Context denotes the entity of the function, package or procedure where
345 function Get_SPARK_Mode_From_Pragma
(N
: Node_Id
) return SPARK_Mode_Type
;
346 -- Given a pragma SPARK_Mode node, return corresponding mode id
348 procedure Initialize
;
349 -- Initializes data structures used for pragma processing. Must be called
350 -- before analyzing each new main source program.
352 function Is_Config_Static_String
(Arg
: Node_Id
) return Boolean;
353 -- This is called for a configuration pragma that requires either string
354 -- literal or a concatenation of string literals. We cannot use normal
355 -- static string processing because it is too early in the case of the
356 -- pragma appearing in a configuration pragmas file. If Arg is of an
357 -- appropriate form, then this call obtains the string (doing any necessary
358 -- concatenations) and places it in Name_Buffer, setting Name_Len to its
359 -- length, and then returns True. If it is not of the correct form, then an
360 -- appropriate error message is posted, and False is returned.
362 function Is_Elaboration_SPARK_Mode
(N
: Node_Id
) return Boolean;
363 -- Determine whether pragma SPARK_Mode appears in the statement part of a
366 function Is_Non_Significant_Pragma_Reference
(N
: Node_Id
) return Boolean;
367 -- The node N is a node for an entity and the issue is whether the
368 -- occurrence is a reference for the purposes of giving warnings about
369 -- unreferenced variables. This function returns True if the reference is
370 -- not a reference from this point of view (e.g. the occurrence in a pragma
371 -- Pack) and False if it is a real reference (e.g. the occurrence in a
374 function Is_Pragma_String_Literal
(Par
: Node_Id
) return Boolean;
375 -- Given an N_Pragma_Argument_Association node, Par, which has the form of
376 -- an operator symbol, determines whether or not it should be treated as an
377 -- string literal. This is called by Sem_Ch6.Analyze_Operator_Symbol. If
378 -- True is returned, the argument is converted to a string literal. If
379 -- False is returned, then the argument is treated as an entity reference
382 function Is_Private_SPARK_Mode
(N
: Node_Id
) return Boolean;
383 -- Determine whether pragma SPARK_Mode appears in the private part of a
386 function Is_Valid_Assertion_Kind
(Nam
: Name_Id
) return Boolean;
387 -- Returns True if Nam is one of the names recognized as a valid assertion
388 -- kind by the Assertion_Policy pragma. Note that the 'Class cases are
389 -- represented by the corresponding special names Name_uPre, Name_uPost,
390 -- Name_uInvariant, and Name_uType_Invariant (_Pre, _Post, _Invariant,
391 -- and _Type_Invariant).
393 procedure Process_Compilation_Unit_Pragmas
(N
: Node_Id
);
394 -- Called at the start of processing compilation unit N to deal with any
395 -- special issues regarding pragmas. In particular, we have to deal with
396 -- Suppress_All at this stage, since it can appear after the unit instead
397 -- of before (actually we allow it to appear anywhere).
399 procedure Relocate_Pragmas_To_Body
400 (Subp_Body
: Node_Id
;
401 Target_Body
: Node_Id
:= Empty
);
402 -- Resocate all pragmas that follow and apply to subprogram body Subp_Body
403 -- to its own declaration list. Candidate pragmas are classified in table
404 -- Pragma_On_Body_Or_Stub_OK. If Target_Body is set, the pragma are moved
405 -- to the declarations of Target_Body. This formal should be set when
406 -- dealing with subprogram body stubs or expression functions.
408 procedure Set_Encoded_Interface_Name
(E
: Entity_Id
; S
: Node_Id
);
409 -- This routine is used to set an encoded interface name. The node S is
410 -- an N_String_Literal node for the external name to be set, and E is an
411 -- entity whose Interface_Name field is to be set. In the normal case where
412 -- S contains a name that is a valid C identifier, then S is simply set as
413 -- the value of the Interface_Name. Otherwise it is encoded as needed by
414 -- particular operating systems. See the body for details of the encoding.
416 function Test_Case_Arg
419 From_Aspect
: Boolean := False) return Node_Id
;
420 -- Obtain argument "Name", "Mode", "Ensures" or "Requires" from Test_Case
421 -- pragma Prag as denoted by Arg_Nam. When From_Aspect is set, an attempt
422 -- is made to retrieve the argument from the corresponding aspect if there
423 -- is one. The returned argument has several formats:
425 -- N_Pragma_Argument_Association if retrieved directly from the pragma
427 -- N_Component_Association if retrieved from the corresponding aspect and
428 -- the argument appears in a named association form.
430 -- An arbitrary expression if retrieved from the corresponding aspect and
431 -- the argument appears in positional form.
433 -- Empty if there is no such argument