diff options
author | Javier Miranda <miranda@adacore.com> | 2022-11-04 13:22:05 +0000 |
---|---|---|
committer | Marc Poulhiès <poulhies@adacore.com> | 2023-01-05 15:30:00 +0100 |
commit | 9ef547a7a99522db8f0f028bde8febc8f1ad8bb8 (patch) | |
tree | c13d0f005e9de9b4d6bed96dff970c446b03a26e /gcc/ada/sem_res.adb | |
parent | e45bef1ec6712be5c4566fd0782d58d4b70b3787 (diff) | |
download | gcc-9ef547a7a99522db8f0f028bde8febc8f1ad8bb8.zip gcc-9ef547a7a99522db8f0f028bde8febc8f1ad8bb8.tar.gz gcc-9ef547a7a99522db8f0f028bde8febc8f1ad8bb8.tar.bz2 |
ada: INOX: prototype RFC on String Interpolation
This patch incorporates a prototype for a new string literal syntax
which supports the use of "string interpolation," where the names
of variables or expressions can be used directly within the string
literal, such that the value of the variable or the expression is
"interpolated" directly into the value of the enclosing string
upon use at run-time.
gcc/ada/
* scans.ads (Tok_Left_Curly_Bracket, Tok_Right_Curly_Bracket)
(Tok_Left_Interpolated_String): Placed in no category since they
don't fit well in the existing categories. Fix typo in comment.
(Inside_Interpolated_String_Literal): New scan state variable.
* scng.adb (Slit): Scan interpolated string literals,
continuations of interpolated string literals and escaped
characters found in interpolated string literals.
(Scan): Handle consecutive interpolated expressions. Handle ending
delimiter placed immediately after an interpolated expression.
Handle string literal placed after interpolated expression. Handle
left and right curly brackets; when extensions are not allowed
they are treated as left and right paren; when extensions are
allowed they are handled as delimiters of interpolated string
literals.
* sinfo.ads (N_Interpolated_String_Literal): New node.
* gen_il-gen-gen_nodes.adb (N_Interpolated_String_Literal): Define
N_String_Literal node.
* gen_il-types.ads (Opt_Type_Enum): Define N_String_Literal as
concrete node type.
* par-ch2.adb (P_Interpolated_String_Literal): New subprogram.
* par-ch4.adb (P_Simple_Expression): Handle '}' as expression
terminator when scanning an interpolated expression; disable error
recovery machinery for binary operator when we are processing an
interpolated string literal and reach the expression terminator
'}'.
(P_Primary): Call P_Interpolated_String_Literal when the opening
interpolated-string-literal delimiter is found (that is, the left
curly bracket '{').
* par-tchk.adb (T_Right_Curly_Bracket): New subprogram.
* par.adb (P_Interpolated_String_Literal): New declaration.
(T_Right_Curly_Bracket): New declaration.
* sem.adb (Analyze): Call Analyze_Interpolated_String_Literal.
* sem_ch2.ads (Analyze_Interpolated_String_Literal): New
subprogram
* sem_ch2.adb (Analyze_Interpolated_String_Literal): Likewise.
* sem_util.adb (Is_User_Defined_Literal): Complete mapping of
literal aspects adding that interpolated string literals have no
correspondence with any aspect.
* sem_res.adb (Resolve_Interpolated_String_Literal): New
subprogram.
(Has_Applicable_User_Defined_Literal): Complete mapping of literal
aspects adding that interpolated string literals have no
correspondency with any aspect.
* expander.adb (Expand): Add call to
Expand_N_Interpolated_String_Literal.
* exp_util.adb (Insert_Actions): Handle
N_Interpolated_String_Literal nodes; that is, continue climbing.
* exp_ch2.ads (Expand_N_Interpolated_String_Literal): New
subprogram.
* exp_ch2.adb (Expand_N_Interpolated_String_Literal): Likewise.
* exp_put_image.adb (Build_Elementary_Put_Image_Call): Add missing
conversion to force dispatching call. Required to handle calls to
descendants.
(Build_String_Put_Image_Call): Do not output string delimiters
when the put_image call is part of an interpolated string literal.
* rtsfind.ads (RTU_Id): Add RE_Set_Trim_Leading_Spaces.
* sprint.adb (Sprint_Node): Output interpolated string contents.
* libgnat/a-stbubo.adb (Get_UTF_8): Add default value for
Trim_Leading_White_Spaces component in aggregate.
(Buffer_Type_Implementation): Update Trim_Leading_White_Spaces.
* libgnat/a-stbuun.adb (Get_UTF_8): Likewise.
(Buffer_Type_Implementation): Likewise.
* libgnat/a-sttebu.ads (Set_Trim_Leading_Spaces): New subprogram.
(Trim_Leading_Spaces): New subprogram.
(Root_Buffer_Type): Adding Trim_Leading_While_Spaces component.
* libgnat/a-sttebu.adb (procedure Set_Trim_Leading_Spaces): New
subprogram.
(Trim_Leading_Space): New subprogram.
(Put_UTF_8): Handle Trim_Leading_White_Spaces.
(New_Line): Likewise.
* libgnat/s-putima.ads (Put_Image_String): Adding formal
(with_delimiters).
(Put_Image_Wide_String): Likewise.
(Put_Image_Wide_Wide_String): Likewise.
* libgnat/s-putima.adb (Put_Image_String): Adding support for new
formal.
(Put_Image_Wide_String): Likewise.
(Put_Image_Wide_Wide_String): Likewise.
Diffstat (limited to 'gcc/ada/sem_res.adb')
-rw-r--r-- | gcc/ada/sem_res.adb | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb index 348d272..9fcbba73 100644 --- a/gcc/ada/sem_res.adb +++ b/gcc/ada/sem_res.adb @@ -212,6 +212,9 @@ package body Sem_Res is procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id); procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id); procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id); + procedure Resolve_Interpolated_String_Literal + (N : Node_Id; + Typ : Entity_Id); procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id); procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id); procedure Resolve_Null (N : Node_Id; Typ : Entity_Id); @@ -449,9 +452,10 @@ package body Sem_Res is Loc : constant Source_Ptr := Sloc (N); Literal_Aspect_Map : constant array (N_Numeric_Or_String_Literal) of Aspect_Id := - (N_Integer_Literal => Aspect_Integer_Literal, - N_Real_Literal => Aspect_Real_Literal, - N_String_Literal => Aspect_String_Literal); + (N_Integer_Literal => Aspect_Integer_Literal, + N_Interpolated_String_Literal => No_Aspect, + N_Real_Literal => Aspect_Real_Literal, + N_String_Literal => Aspect_String_Literal); Named_Number_Aspect_Map : constant array (Named_Kind) of Aspect_Id := (E_Named_Integer => Aspect_Integer_Literal, @@ -3437,6 +3441,9 @@ package body Sem_Res is when N_String_Literal => Resolve_String_Literal (N, Ctx_Type); + when N_Interpolated_String_Literal => + Resolve_Interpolated_String_Literal (N, Ctx_Type); + when N_Target_Name => Resolve_Target_Name (N, Ctx_Type); @@ -9672,6 +9679,35 @@ package body Sem_Res is Eval_Integer_Literal (N); end Resolve_Integer_Literal; + ----------------------------------------- + -- Resolve_Interpolated_String_Literal -- + ----------------------------------------- + + procedure Resolve_Interpolated_String_Literal (N : Node_Id; Typ : Entity_Id) + is + Str_Elem : Node_Id; + + begin + Str_Elem := First (Expressions (N)); + pragma Assert (Nkind (Str_Elem) = N_String_Literal); + + while Present (Str_Elem) loop + + -- Resolve string elements using the context type; for interpolated + -- expressions there is no need to check if their type has a suitable + -- image function because under Ada 2022 all the types have such + -- function available. + + if Etype (Str_Elem) = Any_String then + Resolve (Str_Elem, Typ); + end if; + + Next (Str_Elem); + end loop; + + Set_Etype (N, Typ); + end Resolve_Interpolated_String_Literal; + -------------------------------- -- Resolve_Intrinsic_Operator -- -------------------------------- |