c-mera icon indicating copy to clipboard operation
c-mera copied to clipboard

When comment is not enough

Open kiselgra opened this issue 8 years ago • 8 comments

I want to write this code:

inline __attribute((always_inline)) int foo() { return 0; }

However, neither of the following two work. The former because the function-call gets messed up (which, I think, is ok), the latter because of c-compatible renaming (which, I think, we want to have in the general case).

(function foo () -> (inline __attribute__((always_inline)) int)
  (return 0))

(function foo () -> (inline |__ATTRIBUTE__((ALWAYS_INLINE))| int)
  (return 0))

The result is as follows

inline __attribute__ 
always_inline()(); int foo(void)
{
        return 0;
}

inline __attribute____always_inline__ int foo(void)
{
        return 0;
}

As usual with unusual cases, comment comes in handy:

(function foo () -> (inline (comment "__attribute__((always_inline))" :prefix "") int)
  (return 0))

But starts a new line:

inline 
__attribute__((always_inline)) int foo(void)
{
        return 0;
}

@lispbub Maybe we can just add an option to comment to not trigger a newline?

kiselgra avatar Jan 15 '17 20:01 kiselgra

The syntax is slightly different: (Also there was a small bug in the code, but it should work now)

(function foo () -> (inline (__attribute__ always_inline) int)
     (return 0))

lispbub avatar Jan 16 '17 11:01 lispbub

That version is still missing a set of parens:

inline __attribute__(always_inline) int foo()
{
        return 0;
}

Adding the pair then generates a function call:

inline __attribute__(always_inline)() int foo()
{
        return 0;
}

The intended output is the (somewhat weird) gcc syntax:

inline __attribute__((always_inline)) int foo()
{
        return 0;
}

kiselgra avatar Jan 16 '17 12:01 kiselgra

Support for new command attribute since e9a5aed44dbcab97465543d6714c922efa0dd4f3

(function foo () -> (inline (attribute always_inline) int)
     (return 0))

yields:

inline __attribute__ ((always_inline)) int foo()
{
        return 0;
}

works also with multiple arguments: (attribute weak (alias "__f")) results in __attribute __ ((weak, alias("___f"))) and arguments with parameters: (attribute (assume_aligned 32 8)) generates __attribute__ ((assume_aligned(32, 8))

lispbub avatar Feb 06 '17 13:02 lispbub

It's now possible to disable the line break in comments with:

(function foo () -> (inline (comment "__attribute__((always_inline))"
                                     :prefix ""
                                     :linebreak nil)
                    int)
  (return 0))

allowing easy workarounds:

inline __attribute__((always_inline)) int foo()
{
        return 0;
}

lispbub avatar Feb 06 '17 17:02 lispbub

It seems we forgot about a very unfortunate operator: operator().

(function operator<< ((int a) (int b)) -> bool (return true))

(function operator() ((int a) (int b)) -> bool (return true))

(function (comment "operator()" :prefix "" :linebreak nil) ((int a) (int b)) -> bool
          (return true))
bool operator<<(int a, int b)
{
        return true;
}

__ operator()
{
        return true;
}

bool operator()(int a, int b)
{
        return true;
}

I think parsing this one will be really hard, so I'm open for alternatives, one would be just keeping

(function operator\(\) ((int a) (int b)) -> bool (return true))

I'm basically re-opening this to have it acknowledged. :)

kiselgra avatar Feb 19 '17 20:02 kiselgra

(function |operator()| ... also works, but generates OPERATOR(). (function |OPERATOR()|... works fine :)

What about adding an inverted reader to |foo|?

lispbub avatar Feb 20 '17 10:02 lispbub

How bad do you think this would mess up regular code? Think of SBCL's |List|. Will such things be affected? They should be if some piece of code macroexpands to them, right?

kiselgra avatar Feb 20 '17 20:02 kiselgra

Proposal: simply use (function (operator) ... which internally generates a function call in the name slot and therefore we get operator() ;)

lispbub avatar May 28 '19 15:05 lispbub