Re: [sv-bc] Mantis 1571: proposal for macro defaults

From: Coffin, Eric <eric_coffin_at_.....>
Date: Wed Aug 15 2007 - 12:20:57 PDT
* Greg's definition of what constitutes default_text is important.  I 
would change his definition a little by removing the "i.e.: it cannot 
have a ',' or ')' character outside of balanced parenthesis, balanced 
quotes, or escaped identifiers.  The definition of default_text would 
then be: "The text for a default macro argument is subject to the same 
constraints as any actual argument in a  macro expansion."  If the 
language pertaining to commas or parenthesis is not found in the LRM 
dealing with macro actuals, then perhaps it should be added to the LRM's 
treatment of macro actuals.

* Shalom's proposed change contains the following paragraph:
"If formal arguments are used, the list of formal argument names shall 
be enclosed in parentheses following the name of the macro.  The formal 
argument names shall be simple_identifiers, separated by commas and 
optionally whitespace.  The left parenthesis shall follow the text macro 
name immediately, with no space in between."
The LRM allows macros to be named with escaped identifiers.  If an 
escaped identifier names the macro then one character of white space 
(space, tab, newline, eof) must be used to delimit the macro name as 
defined in section A.9.3 and A.9.4.  First of all, does this make 
sense?  Should language be added that indicates that one, and only one 
space character or tab character separate the escaped identifier from 
the opening parenthesis?

Example:
`define \macro0 (a) $display("Something0:", a);  // Legal
`define \macro1           (a) $display("Something1:", a); // Legal or 
Illegal?
`define \macro2<eof>(a) $display("Something2:", a); // Legal or 
Illegal?  Note that the text "`define \macro2" resides in file 1 while 
"(a) $disp..." resides in file 2.

* The examples in the proposal could be clarified if only the expanded 
text is shown, and not the output generated by the $display().  Thus we 
would have:

Example without default:
`define D(x,y) initial $display("start", x, y, "end");

`D( "msg1" , "msg2" ) // expands to 'initial $display("start", "msg1", 
"msg2", "end");'
`D( "msg1",  )              // expands to 'initial $display("start", 
"msg1",,"end");'
`D( , "msg2 ")              // expands to 'initial 
$display("start",,"msg2 ", "end");'
`D(,)                              // expands to 'initial 
$display("start",,,"end");'
`D(    ,    )                      // expands to 'initial 
$display("start",,,"end");'
`D("msg1")                  // illegal, only one argument
`D()                               // illegal, only one empty argument
`D(,,,,)                           // illegal, more actuals than formals

-Eric

Greg Jaxon wrote:
> Bresticker, Shalom wrote:
>   
>> Hi, Dave. 
>>
>>     
>>> [What] happens if an argument to the macro is 
>>> itself a macro that turns into empty text. Does the default apply?
>>>       
>
> Although it seems like having your cake and eating it too, I think
> the answer here is effectively "yes".  A macro's arguments must be
> fully expanded before they are substituted for their formal counterparts.
> Macro expansion results in source text; the grammatical effects of
> that text must not depend upon how it was provided or produced.  So
> the outer macro must be unable to distinguish an empty text argument
> that was merely omitted from one which is empty due to other macro
> expansion or substitution actions.  For example,
> `define N(form=123)
> `define M(arg=) `N(arg)
> allows `M() to expand to the text "123", even though N looks like it has
> been given an actual argument.
>
>   
>>> When is the default expression evaluated?
>>>       
>
> In Shalom's proposal, the default is raw text (constrained below*); it is
> not an expression that needs evaluating.  However, as text, it could itself
> be produced by a macro expansion.  There may be a little room for debate
> about when to expand macros in the default text, but I think the precedents
> here all favor expansion in the context of the macro's call site /before/
> the expansion of the macro call itself begins.  For example, `Q1(2) and `Q2(2)
> produce the same source text when defined as follows:
>
> `define DBL(arg=form) ((arg) + (arg))
> `define Q1(form) `DBL()
> `define Q2(form) `DBL(form)
>
> Macro expansion is famously dangerous and powerful because its formal/actual
> substitution rule uses pure text substitution where macro and argument names
> are resolved /at the call site/.
> Attempts to make this less chaotic would be in vain.  The attempt
> would also be unwelcome among the true believers in the brute force of
> macro substitution.
>
> [*] Important rule to add to proposal:
> The text for a default macro argument is subject to the same constraints
> as any actual argument in a macro expansion - i.e. it cannot have a "," or
> ")" character outside of balanced parentheses, balanced quotes, or escaped
> identifiers.
>
> Greg Jaxon
>
>
>   

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Wed Aug 15 12:21:22 2007

This archive was generated by hypermail 2.1.8 : Wed Aug 15 2007 - 12:21:55 PDT