what's different between '(foo bar)' and 'foo bar' in stylus? - stylus

func()
foo bar
and
func()
(foo bar)
foo in func() all return true, bars in func() all return false.
As I know, the (foo bar) called tuple. What's name about foo bar? what's different?

foo bar is syntax sugar for foo: bar. In other words, a css rule called foo is being assigned the value bar. For example, color red and color: red are the same. You can also use this syntax to invoke a function. For example, if you have a function foo, all three of the following are equivalent:
foo: bar
foo bar
foo(bar)
(foo bar) is a Stylus list.
Using the colon-less syntax is not recommended. It's ambiguous to the Stylus compiler, so it is likely to cause errors in your code. Colons will be required in the next major version of Stylus.
// Never omit colons
color red
// Do this instead
color: red

Related

Call C function without declaring it beforehand

Short version: I would like to declare a function in the same statement that calls it. The syntax I'm looking for is something of this sort:
// foo is undeclared in this file, and implemented in another file
int main() {
void* p = (cast_to_function_that_receivs_ints_and_returns_pointer)foo(1,2);
}
Long version:
The following code creates an obvious implicit declaration warning and undefined reference error, because of the call to foo:
// a.c
int main() {
void* p = foo(1,2);
}
I add the following file to the compilation to solve the undefined reference:
// b.c
void* foo(int a, int b) {
return (void*)0xbadcafe;
}
I would now like to solve the implicit declaration. The usual solution is to modify a.c to either #include a declaration to foo or declare it itself, something like:
// a.c
void* foo(int a, int b);
int main() {
void* p = foo(1,2);
}
But I would rather not declare foo, instead modifying the line that calls foo, similar to function pointers syntax, or to the example I posted in the "short versions". Is it even possible?
Assume I am proficient in C and that I have a valid motivation - I would like to "override" the behavior of foo by recompiling with -Dfoo=bar.
So if I understand correctly, your motivation is that you have existing code that looks like
p = bar(1,2);
and you would like to define macros so that it calls foo(1,2) instead. But you don't want to modify the source file to include a declaration of foo - you want to do everything by means of command-line macro definitions. Have I got that right?
Since you've tagged this gcc, perhaps you are willing to consider non-standard gcc extensions to the C language. If so, you can do it with gcc statement expressions, also supported by clang and icc. Define bar to expand to an expression containing a block which declares foo and whose value is a pointer to foo. That is:
#define bar ({ extern void *foo(int, int); foo; })
Or from the command line:
gcc -D'bar=({ extern void *foo(int, int); foo; })' call_bar.c
Try it on godbolt.
This has
A variant would be to define a macro bar(a,b) with two arguments, where the corresponding statement expression actually calls foo:
gcc -D'bar(a,b)=({ extern void *foo(int, int); foo((a), (b)); })' call_bar.c
but this will fail if the original code tries to call p = (bar)(a,b) or tries to take the address of bar.
I'm not aware of any way to get this exact effect in standard C. But a different approach would be to create a header file containing the declaration of foo, and then using -include to "inject" it at the top of the source file:
gcc -include declare_foo.h -Dbar=foo call_bar.c
This isn't technically what you asked for, because at some level it does involve declaring foo "beforehand", but it may still help solve your problem. In this case everything is standard C, but we have moved the "non-portability" from the code to the build process.
On the other hand, if the desired replacement for bar is something simple enough to put in a macro, like the constant return in your example, then you can cut out the middleman foo and just define a macro:
gcc -D'bar(a,b)=((void *)0xbadcafe)' call_bar.c
There's no way around the declaration requirement. You must define a symbol for the compiler to work with. Some compilers allow you to use a pragma or other non-standard feature to create the mapping between the symbol and physical/virtual address.
Compile your mock_foo.c file and link the object file to the program instead of foo.c.
Another approach is only ever call through a macro definition:
#ifdef MOCK_FOO
#define (FOO(a, b) mock_foo(a, b))
#else
#define (FOO(a, b) foo(a, b)
#endif
Otherwise, you have to understand how the compiler/linker and OS/loader work, to correctly hook functions to call mocks. There's a reason tooling for quality mock frameworks cost so much money. They are very complex.
You can cast a function as you call it:
void *p = ((void *(*)(int, int))foo)(1, 2);
It's ugly, I don't see a valid reason for it, but you can.

Correct way to define a prototype

Came across a situation where I was in doubt how to define the prototype the correct way. It´s easier to just look at a simple example:
Document A.c:
#define foo bar
void mon() {
foo();
}
Document B.c:
void bar() {
Do something;
}
Gives following warning:
Warning: Function does not have a full prototype
Normally I would solve it by:
extern void foo(void);
But as example show, the function dont exactly exist but is defined to point on another function. What is the correct way to make a prototype for this?
If the compiler encounters the declaration extern void foo(void); after the #define foo bar for the same source file, it will parse it as extern void bar(void); And the linker will just solve the bar symbol.
Note that you definition of bar is not consistent with the declaration above. The definition of bar should read:
void bar(void) {
// Do something;
}
in C, unlike C++, an argument list of (void) is subtly different from an empty argument list.
I think, that what happens is the following:
The compiler replaces the macro foo with bar but since at that stage bar is not declared anywhere as a function the compiler will complain, that it cannot find it.
Please see more: Are prototypes required for all functions in C89, C90 or C99?

Using #define in C with no value

If a #define is used with no value, like
#define COMMAND_SPI()
does it take value 0 by default?
No, it evaluates to nothing. Literally the symbol gets replaced with nothing.
However, once you have #define FOO, the preprocessor conditional #ifdef FOO will now be true.
Note also that in gcc and possibly other compilers, if you define a macro with -DFOO on the command line, that evaluates to 1 by default.
Since the OP updated his question to reference function-like macros, let's consider a small example.
#define FOO
#define BAR()
FOO
BAR
BAR()
This is not a valid C program, but the preprocessor does not care.
If I compile this with gcc -E Input.c, I get a blank, followed by BAR followed by another blank. This is because the first and third expressions evaluate to nothingness, and the middle expression is not expanded because there are no () after it.

Create sum type in C implementation of OCaml function

Let's say you had a type declaration:
type foo = Bar | Baz of int
How would you implement a C function to create a Baz? Let's say I declare it like this:
external create_baz : int -> foo = "create_baz"
Then I would need to fill out this code:
CAMLprim value create_baz(value bar) {
// what do I do here?
}
I understand that this is a rather silly thing to do, but it's just and example of what I'm trying to do.
This is described in Chapter 19 of the OCaml manual.
Basically, constructors are numbered in order, in two separate sequences. Nullary constructors (those taking no values, like Bar) are numbered in one sequence, and constructors that take a value (like Baz) are numbered in a second sequence. So both of your constructors are numbered 0.
Nullary constructors are represented by immediate values (a simple bit pattern representing a value like an int). Constructors taking values are represented by pointers to blocks, which have fields that can store the contained values.
So, basically your function wants to make a block of size 1 with tag 0. bar is saved in the block's 0th field (the only field).
It looks something like this:
value create_baz(value bar) {
// Caller guarantees that bar is an int.
//
CAMLparam1(bar);
CAMLlocal1(result);
result = caml_alloc(1, 0);
Store_field(result, 0, bar);
CAMLreturn(result);
}

C - Architecture independent function call interposition

I'm writing a piece of code where I have a function pointer that gets invoked. What I'd like to do is interpose on this function call to do something, and then invoke anotherfunction call with the same arguments. I wonder if there is some way to do this without having to write assembly for each architecture I'm targeting. Perhaps there are some GCC tricks?
As an example I call my function pointer and it invokes
foo (/*arguments*/) {
do_something...
bar(/*same arguments*/);
}
In assembly this is fairly easy. At least in x86 I just make sure that my stack pointer is reset to the beginning of my stack frame and jump to function bar (not call).
EDIT: Perhaps the example isn't clear. The user expects to be calling function bar but instead I have redirected it to function foo (I don't know what arguments bar takes). I want to do something in foo before calling bar with the same arguments that were passed on. In this way, whatever I'm doing in foo is transparent to the user who thinks they just called bar.
Have a look into gcc option -finstrument-functions.
Sounds like what the the gcc specific ___builtin_apply_args is for. It's an intristic that captures the passed in argument, and you can call another function with those arguments using __builtin_apply
libffi does all (or at least most) of what you need for source-level interposing.
Another option is to use dynamic binary instrumentation tools like DynamoRIO or Pin.
You could try creating a global function pointer variable that is used as a look-up for pre-binding the two function calls to one another. For instance,
typedef void (*bar_type)(int arg1, int arg2);
bar_type function_ptr; //a global function pointer used for binding
//create a bar_type function that is our "actual" function call
void __bar(int arg1, int arg2)
{
//do something else
}
//create a bar_type function called "foo" that is bound to calling whatever
//function is being pointed to by function_ptr
void foo(int arg1, int arg2)
{
//do something
function_ptr(arg1, arg2); //"foo" now calls "__bar"
}
bar_type transform_func(bar_type func_call, bar_type int_call)
{
function_ptr = func_call; //set the global function ptr variable
return int_call;
}
//create your function pointer bar that will call "foo" before calling "__bar"
bar_type bar = transform_func(__bar, foo);
//later on in your code
bar(3, 4); //this will call foo() which will then call __bar() internally
You could also with this approach create a macro that for the user where you could define bar as a macro that looks like
#define bar(arg1, arg2) (*(transform_func(__bar, foo)))(arg1, arg2);
Hopefully this isn't too kludgy ... there is definitely a performance hit from what could be done with assembly, but using the global function pointer would be a way to re-bind a function call.
Can you use a function-like macro to implement "do something" in the context of the calling function, and then do a normal call to the function pointer? (Yes, standard disclaimers about macros, esp. function-like macros apply...)
For example something like:
#define CALL_FUNC(fp,ARG1,ARG2) do {<do something>;fp(ARG1,ARG2);} while (0)
And then in the application, replace where you de-reference the function pointer with the macro.
It's not clear to me from the original question if foo or bar is the function called through the function pointer, so you might need to adjust the macro, but the general approach stays the same.

Resources