| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S E M _ C H 2 -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 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. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
with Atree; use Atree; |
| 27 |
with Einfo; use Einfo; |
| 28 |
with Einfo.Entities; use Einfo.Entities; |
| 29 |
with Einfo.Utils; use Einfo.Utils; |
| 30 |
with Errout; use Errout; |
| 31 |
with Ghost; use Ghost; |
| 32 |
with Mutably_Tagged; use Mutably_Tagged; |
| 33 |
with Namet; use Namet; |
| 34 |
with Nlists; use Nlists; |
| 35 |
with Opt; use Opt; |
| 36 |
with Restrict; use Restrict; |
| 37 |
with Rident; use Rident; |
| 38 |
with Sem; use Sem; |
| 39 |
with Sem_Ch8; use Sem_Ch8; |
| 40 |
with Sem_Dim; use Sem_Dim; |
| 41 |
with Sem_Res; use Sem_Res; |
| 42 |
with Sinfo; use Sinfo; |
| 43 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 44 |
with Sinfo.Utils; use Sinfo.Utils; |
| 45 |
with Stand; use Stand; |
| 46 |
with Uintp; use Uintp; |
| 47 |
|
| 48 |
package body Sem_Ch2 is |
| 49 |
|
| 50 |
------------------------------- |
| 51 |
-- Analyze_Character_Literal -- |
| 52 |
------------------------------- |
| 53 |
|
| 54 |
procedure Analyze_Character_Literal (N : Node_Id) is |
| 55 |
begin |
| 56 |
-- The type is eventually inherited from the context. If expansion |
| 57 |
-- has already established the proper type, do not modify it. |
| 58 |
|
| 59 |
if No (Etype (N)) then |
| 60 |
Set_Etype (N, Any_Character); |
| 61 |
end if; |
| 62 |
|
| 63 |
Set_Is_Static_Expression (N); |
| 64 |
|
| 65 |
if Comes_From_Source (N) |
| 66 |
and then not In_Character_Range (UI_To_CC (Char_Literal_Value (N))) |
| 67 |
then |
| 68 |
Check_Restriction (No_Wide_Characters, N); |
| 69 |
end if; |
| 70 |
end Analyze_Character_Literal; |
| 71 |
|
| 72 |
------------------------ |
| 73 |
-- Analyze_Identifier -- |
| 74 |
------------------------ |
| 75 |
|
| 76 |
procedure Analyze_Identifier (N : Node_Id) is |
| 77 |
begin |
| 78 |
-- Ignore call if prior errors, and identifier has no name, since |
| 79 |
-- this is the result of some kind of previous error generating a |
| 80 |
-- junk identifier. |
| 81 |
|
| 82 |
if not Is_Valid_Name (Chars (N)) and then Total_Errors_Detected /= 0 then |
| 83 |
return; |
| 84 |
else |
| 85 |
Find_Direct_Name (N); |
| 86 |
end if; |
| 87 |
|
| 88 |
-- Generate a conversion when we see an expanded mutably tagged type |
| 89 |
|
| 90 |
if Is_Mutably_Tagged_CW_Equivalent_Type (Etype (N)) then |
| 91 |
Make_Mutably_Tagged_Conversion (N); |
| 92 |
end if; |
| 93 |
|
| 94 |
-- A Ghost entity must appear in a specific context. Only do this |
| 95 |
-- checking on non-overloaded expressions, as otherwise we need to |
| 96 |
-- wait for resolution, and the checking is done in Resolve_Entity_Name. |
| 97 |
|
| 98 |
if Nkind (N) in N_Expanded_Name | N_Identifier |
| 99 |
and then Present (Entity (N)) |
| 100 |
and then Is_Ghost_Entity (Entity (N)) |
| 101 |
and then not Is_Overloaded (N) |
| 102 |
then |
| 103 |
Check_Ghost_Context (Entity (N), N); |
| 104 |
end if; |
| 105 |
|
| 106 |
Analyze_Dimension (N); |
| 107 |
end Analyze_Identifier; |
| 108 |
|
| 109 |
----------------------------- |
| 110 |
-- Analyze_Integer_Literal -- |
| 111 |
----------------------------- |
| 112 |
|
| 113 |
procedure Analyze_Integer_Literal (N : Node_Id) is |
| 114 |
begin |
| 115 |
-- As a lexical element, an integer literal has type Universal_Integer, |
| 116 |
-- i.e., is compatible with any integer type. This is semantically |
| 117 |
-- consistent and simplifies type checking and subsequent constant |
| 118 |
-- folding when needed. An exception is caused by 64-bit modular types, |
| 119 |
-- whose upper bound is not representable in a nonstatic context that |
| 120 |
-- will use 64-bit integers at run time. For such cases, we need to |
| 121 |
-- preserve the information that the analyzed literal has that modular |
| 122 |
-- type. For simplicity, we preserve the information for all integer |
| 123 |
-- literals that result from a modular operation. This happens after |
| 124 |
-- prior analysis (or construction) of the literal, and after type |
| 125 |
-- checking and resolution. |
| 126 |
|
| 127 |
if No (Etype (N)) or else not Is_Modular_Integer_Type (Etype (N)) then |
| 128 |
Set_Etype (N, Universal_Integer); |
| 129 |
end if; |
| 130 |
|
| 131 |
Set_Is_Static_Expression (N); |
| 132 |
end Analyze_Integer_Literal; |
| 133 |
|
| 134 |
----------------------------------------- |
| 135 |
-- Analyze_Interpolated_String_Literal -- |
| 136 |
----------------------------------------- |
| 137 |
|
| 138 |
procedure Analyze_Interpolated_String_Literal (N : Node_Id) is |
| 139 |
|
| 140 |
procedure Check_Ambiguous_Call (Func_Call : Node_Id); |
| 141 |
-- Examine the interpretations of the call to the given function call |
| 142 |
-- and report the location of each interpretation. |
| 143 |
|
| 144 |
-------------------------- |
| 145 |
-- Check_Ambiguous_Call -- |
| 146 |
-------------------------- |
| 147 |
|
| 148 |
procedure Check_Ambiguous_Call (Func_Call : Node_Id) is |
| 149 |
Result : Boolean; |
| 150 |
pragma Unreferenced (Result); |
| 151 |
|
| 152 |
begin |
| 153 |
Check_Parameterless_Call (Func_Call); |
| 154 |
|
| 155 |
if Is_Overloaded (Func_Call) then |
| 156 |
Result := |
| 157 |
Is_Ambiguous_Operand |
| 158 |
(Operand => Func_Call, |
| 159 |
In_Interp_Expr => True, |
| 160 |
Report_Errors => True); |
| 161 |
|
| 162 |
-- Discard Result because the function has been invoked to report |
| 163 |
-- ambiguities (if any); no further action required. |
| 164 |
end if; |
| 165 |
end Check_Ambiguous_Call; |
| 166 |
|
| 167 |
-- Local variables |
| 168 |
|
| 169 |
Str_Elem : Node_Id; |
| 170 |
|
| 171 |
-- Start of processing for Analyze_Interpolated_String_Literal |
| 172 |
|
| 173 |
begin |
| 174 |
Set_Etype (N, Any_String); |
| 175 |
|
| 176 |
Str_Elem := First (Expressions (N)); |
| 177 |
while Present (Str_Elem) loop |
| 178 |
Analyze (Str_Elem); |
| 179 |
|
| 180 |
-- The parser has split the contents of the interpolated string |
| 181 |
-- into its components. For example, f"before {expr} after" is |
| 182 |
-- stored in the list of expressions of N as follows: |
| 183 |
-- first = "before " (is_interpolated_string_literal) |
| 184 |
-- next = expr |
| 185 |
-- next = " after" (is_interpolated_string_literal) |
| 186 |
-- |
| 187 |
-- No further action is needed for string literals with the |
| 188 |
-- attribute Is_Interpolated_String_Literal set, as they are |
| 189 |
-- components of the interpolated string literal. The type of |
| 190 |
-- these components will be determined by the context when |
| 191 |
-- resolved (see Expand_N_Interpolated_String_Literal). The |
| 192 |
-- rest of the components in the list of expressions of N are |
| 193 |
-- the root nodes of the interpolated expressions. |
| 194 |
|
| 195 |
if Nkind (Str_Elem) = N_String_Literal |
| 196 |
and then Is_Interpolated_String_Literal (Str_Elem) |
| 197 |
then |
| 198 |
null; |
| 199 |
|
| 200 |
elsif Nkind (Str_Elem) = N_Function_Call then |
| 201 |
Check_Ambiguous_Call (Str_Elem); |
| 202 |
|
| 203 |
-- Before analyzed, a function call that has parameters is an |
| 204 |
-- N_Indexed_Component node, and a call to a function that has |
| 205 |
-- no parameters is an N_Identifier or an N_Expanded_Name node. |
| 206 |
-- If the analysis could not rewrite it as N_Function_Call, it |
| 207 |
-- indicates that ambiguity may have been encountered. |
| 208 |
|
| 209 |
elsif Nkind (Str_Elem) in N_Identifier | N_Expanded_Name |
| 210 |
and then Ekind (Entity (Str_Elem)) = E_Function |
| 211 |
then |
| 212 |
Check_Ambiguous_Call (Str_Elem); |
| 213 |
|
| 214 |
-- Report common errors |
| 215 |
|
| 216 |
elsif Nkind (Str_Elem) = N_String_Literal then |
| 217 |
|
| 218 |
-- No further action needed for components of the interpolated |
| 219 |
-- string literal; its type will be imposed by its context when |
| 220 |
-- resolved. |
| 221 |
|
| 222 |
if Is_Interpolated_String_Literal (Str_Elem) then |
| 223 |
null; |
| 224 |
|
| 225 |
else |
| 226 |
Error_Msg_N |
| 227 |
("ambiguous string literal in interpolated expression", |
| 228 |
Str_Elem); |
| 229 |
Error_Msg_N |
| 230 |
("\\possible interpretation 'Ada.'String type!", |
| 231 |
Str_Elem); |
| 232 |
Error_Msg_N |
| 233 |
("\\possible interpretation 'Ada.'Wide_'String type!", |
| 234 |
Str_Elem); |
| 235 |
Error_Msg_N |
| 236 |
("\\possible interpretation 'Ada.'Wide_'Wide_'String" |
| 237 |
& " type!", Str_Elem); |
| 238 |
Error_Msg_N |
| 239 |
("\\must use a qualified expression", Str_Elem); |
| 240 |
end if; |
| 241 |
|
| 242 |
elsif Nkind (Str_Elem) = N_Character_Literal then |
| 243 |
Error_Msg_N |
| 244 |
("ambiguous character literal in interpolated expression", |
| 245 |
Str_Elem); |
| 246 |
Error_Msg_N |
| 247 |
("\\possible interpretation 'Ada.'Character type!", |
| 248 |
Str_Elem); |
| 249 |
Error_Msg_N |
| 250 |
("\\possible interpretation 'Ada.'Wide_'Character type!", |
| 251 |
Str_Elem); |
| 252 |
Error_Msg_N |
| 253 |
("\\possible interpretation 'Ada.'Wide_'Wide_'Character" |
| 254 |
& " type!", Str_Elem); |
| 255 |
Error_Msg_N |
| 256 |
("\\must use a qualified expression", Str_Elem); |
| 257 |
|
| 258 |
elsif Nkind (Str_Elem) in N_Integer_Literal |
| 259 |
| N_Real_Literal |
| 260 |
then |
| 261 |
Error_Msg_N |
| 262 |
("ambiguous number in interpolated expression", |
| 263 |
Str_Elem); |
| 264 |
Error_Msg_N |
| 265 |
("\\must use a qualified expression", Str_Elem); |
| 266 |
|
| 267 |
elsif Nkind (Str_Elem) = N_Interpolated_String_Literal then |
| 268 |
Error_Msg_N ("nested interpolated string not allowed", Str_Elem); |
| 269 |
|
| 270 |
elsif Etype (Str_Elem) in Any_Type |
| 271 |
| Any_Array |
| 272 |
| Any_Composite |
| 273 |
| Any_Discrete |
| 274 |
| Any_Fixed |
| 275 |
| Any_Integer |
| 276 |
| Any_Modular |
| 277 |
| Any_Numeric |
| 278 |
| Any_Real |
| 279 |
| Any_String |
| 280 |
| Universal_Integer |
| 281 |
| Universal_Real |
| 282 |
| Universal_Fixed |
| 283 |
| Universal_Access |
| 284 |
then |
| 285 |
Error_Msg_N ("ambiguous interpolated expression", Str_Elem); |
| 286 |
end if; |
| 287 |
|
| 288 |
Next (Str_Elem); |
| 289 |
end loop; |
| 290 |
end Analyze_Interpolated_String_Literal; |
| 291 |
|
| 292 |
-------------------------- |
| 293 |
-- Analyze_Real_Literal -- |
| 294 |
-------------------------- |
| 295 |
|
| 296 |
procedure Analyze_Real_Literal (N : Node_Id) is |
| 297 |
begin |
| 298 |
Set_Etype (N, Universal_Real); |
| 299 |
Set_Is_Static_Expression (N); |
| 300 |
end Analyze_Real_Literal; |
| 301 |
|
| 302 |
---------------------------- |
| 303 |
-- Analyze_String_Literal -- |
| 304 |
---------------------------- |
| 305 |
|
| 306 |
procedure Analyze_String_Literal (N : Node_Id) is |
| 307 |
begin |
| 308 |
-- The type is eventually inherited from the context. If expansion |
| 309 |
-- has already established the proper type, do not modify it. |
| 310 |
|
| 311 |
if No (Etype (N)) then |
| 312 |
Set_Etype (N, Any_String); |
| 313 |
end if; |
| 314 |
|
| 315 |
-- String literals are static in Ada 95. Note that if the subtype |
| 316 |
-- turns out to be non-static, then the Is_Static_Expression flag |
| 317 |
-- will be reset in Eval_String_Literal. |
| 318 |
|
| 319 |
if Ada_Version >= Ada_95 then |
| 320 |
Set_Is_Static_Expression (N); |
| 321 |
end if; |
| 322 |
|
| 323 |
if Comes_From_Source (N) and then Has_Wide_Character (N) then |
| 324 |
Check_Restriction (No_Wide_Characters, N); |
| 325 |
end if; |
| 326 |
end Analyze_String_Literal; |
| 327 |
|
| 328 |
end Sem_Ch2; |