Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if an function argument is part of a function call

Tags:

objective-c

Ok, I think it is next to impossible. But nevertheless: Is it possible to know inside a method call if the method was called as part of a function call?

Example (and my actual function call):

HDMExpressionSQLSelectBuilder *sb = [[[HDMExpressionSQLSelectBuilder alloc] init] autorelease];
[sb orNestedWhere:[sb where:@"wheraCoumnB" equals:@"whereBEqualValue"], [sb where:@"wheraCoumnB" equals:@"sth"], nil];

There are two things happening from here on:

  • The arguments are evaluated in any order (in which order is not specified in C standard and it changes between compilers and settings)
  • Eventhough this should be obvious to every programmer: The inner functions are evaluated before

Now I want to know for example inside this method call...

 [sb where:@"wheraCoumnB" equals:@"whereBEqualValue"]

.. that it was called as part of an argument of a function.

Possible? Black magic?

Before you come and say I'm doing it wrong, and I should fix my code: I claim that I have one of the rare cases where it will make sense to know such thing. I'm writing a query builder, and this would greatly ease the use of nested conditions. Otherwise I'd have to do some silly nestedAndBegin and later nestedAndEnd in order to implement brackets, and so on. It would be awkward in this case, my query builder is tree based and I would not like to do this (unlike the string juggling query builders all that would be required is putting the node for the logical expression in place).

Update

So this is unsurprisingly not possible. For those who are interested how I worked around this for my particular problem: I've made it so all calls to functions are deferred, so the calls to a function of my query builder puts a method invocation object with function arguments to an invocation list. It doesn't execute any method code at that time. Each invocation object has an auto-inc sequence Id, so I know when a function has been evaluated. Now in the nestedAnd etc functions (so those functions that my question was about) I check if the sequence Id of the stored invocation objects correspond to the argument index of the function call. If not, this is the right time to reorder them.

The syntax check and query building phase is then deferred until a user actually calls the query() method (or ast() to get the expression tree).

like image 352
benjist Avatar asked Jul 09 '13 18:07

benjist


People also ask

Are arguments part of a function call?

Arguments are passed by value; that is, when a function is called, the parameter receives a copy of the argument's value, not its address. This rule applies to all scalar values, structures, and unions passed as arguments. Modifying a parameter does not modify the corresponding argument passed by the function call.

Which argument is used in function call?

Calling the function involves specifying the function name, followed by the function call operator and any data values the function expects to receive. These values are the arguments for the parameters defined for the function. This process is called passing arguments to the function.

How do you know when a function has an argument?

To check if a parameter is provided to a function, use the strict inequality (! ==) operator to compare the parameter to undefined , e.g. if (param !== undefined) . If the comparison returns true , then the parameter was provided to the function.

Does a function include arguments?

A function may require its first argument to be a number, the second argument a text string, and the third argument a cell reference. Some arguments can be of more than one type; for example, the SUM function's argument can be a set of numbers, range references, or array constants.


1 Answers

No, there is no (practical) way to do that.

You could, theoretically, add some code in your method that reads the debugging symbols from your binary, inspects the link register, and then uses that information to figure out the call site, loads up the source code from somewhere else, and figures out the mapping, parses the source code and figures out it's being called as you describe.

like image 82
Carl Norum Avatar answered Oct 16 '22 19:10

Carl Norum