for review: Underscore vars in pragma c code arguments

Andrew Bromage bromage at cs.mu.OZ.AU
Tue Aug 4 16:59:29 AEST 1998


G'day all.

Could someone better versed in the code generator than I please review
this?  Thanks.

Cheers,
Andrew Bromage


Estimated hours taken: 2

Do not generate C code for pragma c_code argument variables which begin
with an underscore.  This avoids possible clashes with the system
namespace.  (We shouldn't declare any C identifiers which begin with an
underscore.)


compiler/pragma_c_gen.m:
	Changes detailed above.

Index: pragma_c_gen.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/pragma_c_gen.m,v
retrieving revision 1.16
diff -u -t -r1.16 pragma_c_gen.m
--- pragma_c_gen.m	1998/05/16 07:30:41	1.16
+++ pragma_c_gen.m	1998/07/30 03:44:52
@@ -877,7 +877,16 @@
 make_pragma_decls([], []).
 make_pragma_decls([Arg | Args], Decls) :-
         Arg = c_arg(_Var, ArgName, OrigType, _ArgInfo),
-        ( ArgName = yes(Name) ->
+        (
+                ArgName = yes(Name),
+
+                        % If the first char of the variable is an
+                        % underscore, we should ignore it since:
+                        %       - it should not appear in the C code
+                        %       - it could clash with the system name
+                        %         space
+                \+ string__first_char(Name, '_', _)
+        ->
                 Decl = pragma_c_arg_decl(OrigType, Name),
                 make_pragma_decls(Args, Decls1),
                 Decls = [Decl | Decls1]
@@ -899,7 +908,17 @@
 get_pragma_input_vars([], [], empty) --> [].
 get_pragma_input_vars([Arg | Args], Inputs, Code) -->
         { Arg = c_arg(Var, MaybeName, Type, _ArgInfo) },
-        ( { MaybeName = yes(Name) } ->
+        (
+                { MaybeName = yes(Name),
+
+                        % If the first char of the variable is an
+                        % underscore, we should ignore it since:
+                        %       - it should not appear in the C code
+                        %       - it could clash with the system name
+                        %         space
+                  \+ string__first_char(Name, '_', _)
+                }
+        ->
                 code_info__produce_variable(Var, Code0, Rval),
                 { Input = pragma_c_input(Name, Type, Rval) },
                 get_pragma_input_vars(Args, Inputs1, Code1),
@@ -935,17 +954,26 @@
         list(pragma_c_output)::out, code_info::in, code_info::out) is det.
 
 place_pragma_output_args_in_regs([], [], []) --> [].
-place_pragma_output_args_in_regs([Arg | Args], [Reg | Regs],
-                [Output | Outputs]) -->
+place_pragma_output_args_in_regs([Arg | Args], [Reg | Regs], Outputs) -->
         { Arg = c_arg(Var, MaybeName, OrigType, _ArgInfo) },
         code_info__release_reg(Reg),
         code_info__set_var_location(Var, Reg),
-        ( { MaybeName = yes(Name) } ->
-                { Output = pragma_c_output(Reg, OrigType, Name) }
+        { MaybeName = yes(Name) ->
+                (
+                        % If the first char of the variable is an
+                        % underscore, ignore it.
+
+                        string__first_char(Name, '_', _)
+                ->
+                        Outputs = Outputs0
+                ;
+                        Outputs = [pragma_c_output(Reg, OrigType, Name)
+                                        | Outputs0]
+                )
         ;
-                { error("place_pragma_output_args_in_regs: unnamed arg") }
-        ),
-        place_pragma_output_args_in_regs(Args, Regs, Outputs).
+                error("place_pragma_output_args_in_regs: unnamed arg")
+        },
+        place_pragma_output_args_in_regs(Args, Regs, Outputs0).
 place_pragma_output_args_in_regs([_|_], [], _) -->
         { error("place_pragma_output_args_in_regs: length mismatch") }.
 place_pragma_output_args_in_regs([], [_|_], _) -->
@@ -962,7 +990,16 @@
 input_descs_from_arg_info([], []).
 input_descs_from_arg_info([Arg | Args], Inputs) :-
         Arg = c_arg(_Var, MaybeName, OrigType, ArgInfo),
-        ( MaybeName = yes(Name) ->
+        (
+                MaybeName = yes(Name),
+
+                        % If the first char of the variable is an
+                        % underscore, we should ignore it since:
+                        %       - it should not appear in the C code
+                        %       - it could clash with the system name
+                        %         space
+                \+ string__first_char(Name, '_', _)
+        ->
                 ArgInfo = arg_info(N, _),
                 Reg = reg(r, N),
                 Input = pragma_c_input(Name, OrigType, lval(Reg)),
@@ -982,16 +1019,28 @@
         is det.
 
 output_descs_from_arg_info([], []).
-output_descs_from_arg_info([Arg | Args], [Output | Outputs]) :-
+output_descs_from_arg_info([Arg | Args], Outputs) :-
         Arg = c_arg(_Var, MaybeName, OrigType, ArgInfo),
-        ( MaybeName = yes(Name) ->
-                ArgInfo = arg_info(N, _),
-                Reg = reg(r, N),
-                Output = pragma_c_output(Reg, OrigType, Name)
+        (
+                MaybeName = yes(Name)
+        ->
+                (
+                        % If the first char of the variable is an
+                        % underscore, ignore it.
+
+                        string__first_char(Name, '_', _)
+                ->
+                        Outputs = Outputs0
+                ;
+                        ArgInfo = arg_info(N, _),
+                        Reg = reg(r, N),
+                        Outputs = [pragma_c_output(Reg, OrigType, Name)
+                                        | Outputs0]
+                )
         ;
                 error("output_descs_from_arg_info: unnamed arg")
         ),
-        output_descs_from_arg_info(Args, Outputs).
+        output_descs_from_arg_info(Args, Outputs0).
 
 %---------------------------------------------------------------------------%
 




More information about the developers mailing list