Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the type of a variable in C code?

Is there any way that I can discover the type of a variable automatically in C, either through some mechanism within the program itself, or--more likely--through a pre-compilation script that uses the compiler's passes up to the point where it has parsed the variables and assigned them their types? I'm looking for general suggestions about this. Below is more background about what I need and why.

I would like to change the semantics of the OpenMP reduction clause. At this point, it seems easiest simply to replace the clause in the source code (through a script) with a call to a function, and then I can define the function to implement the reduction semantics I want. For instance, my script would convert this

#pragma omp parallel for reduction(+:x)

into this:

my_reduction(PLUS, &x, sizeof(x));
#pragma omp parallel for

where, earlier, I have (say)

enum reduction_op {PLUS, MINUS, TIMES, AND,
  OR, BIT_AND, BIT_OR, BIT_XOR, /* ... */};

And my_reduction has signature

void my_reduction(enum reduction_op op, void * var, size_t size);

Among other things, my_reduction would have to apply the addition operation to the reduction variable as the programmer had originally intended. But my function cannot know how to do this correctly. In particular, although it knows the kind of operation (PLUS), the location of the original variable (var), and the size of the variable's type, it does not know the variable's type itself. In particular, it does not know whether var has an integral or floating-point type. From a low-level POV, the addition operation for those two classes of types is completely different.

If only the nonstandard operator typeof, which GCC supports, would work the way sizeof works--returning some sort of type variable--I could solve this problem easily. But typeof is not really like sizeof: it can only be used, apparently, in l-value declarations.

Now, the compiler obviously does know the type of x before it finishes generating the executable code. This leads me to wonder whether I can somehow leverage GCC's parser, just to get x's type and pass it to my script, and then run GCC again, all the way, to compile my altered source code. It would then be simple enough to declare

enum var_type { INT8, UINT8, INT16, UINT16, /* ,..., */ FLOAT, DOUBLE};
void my_reduction(enum reduction_op op, void * var, enum var_type vtype);

And my_reduction can cast appropriately before dereferencing and applying the operator.

As you can see, I am trying to create a kind of "dispatching" mechanism in C. Why not just use C++ overloading? Because my project constrains me to work with legacy source code written in C. I can alter the code automatically with a script, but I cannot rewrite it into a different language.

Thanks!

like image 243
Amittai Aviram Avatar asked Feb 06 '12 06:02

Amittai Aviram


People also ask

What is variable_name and data type in C++?

Data types can be int, float, char, double, long int, etc. variable_name: Indicates the name of the variable. It can be anything other than the keyword. For example, 1, int is a data type, and a is a variable name. In the second example, we have declared three variables, a, b, and c.

How to find the type of a variable in C++?

Therefore, the C++ libraries have provided us with different methods for finding the type of a variable. One of the methods is to use the typeid operator, which is present in the <typeinfo> library of C++. However, the other one has been introduced in C++11, known as the decltype (x), which converts an expression into a type of the result produced.

Can variables be different in C?

Data that variables can be different like int, float, char, double, etc. All the code or program depends on the variables as it describes the type of data for execution. Variables in C must not start with the number; else, the Variable will not be valid.

What is the function of variable in C programming?

It acts as a memory card where it saves all the data and used it during program execution. There are different types of variables in C; according to their types, the amount of memory or storage space it requires differs.


1 Answers

GCC provides the typeof extension. It is not standard, but common enough (several other compilers, e.g. clang/llvm, have it).

You could perhaps consider customizing GCC by extending it with MELT (a domain specific language to extend GCC) to fit your purposes.

like image 129
Basile Starynkevitch Avatar answered Oct 11 '22 17:10

Basile Starynkevitch