2

In the huge (millons of LOC) C project I want to expand a "double" type into a struct containing two doubles:

Now I have

typedef double popular_type;
//... a lot (>1k) usage of the type in expressions like:
popular_type a;
a = (popular_type) some_double_variable;

I want to change popular_type definition into

#ifdef SYNTHETIC_POPULAR_TYPE
typedef struct {
  double orig_field;
  double additional_field;
} popular_type;
#else
typedef double popular_type;
#endif

If I do not change the expressions like

popular_type a;
a = (popular_type) some_double_variable;

to

popular_type a;
a.orig_field = some_double_variable;

will the program work?

How can I redeclare popular_type in order that I do not have to change all assignments?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
osgx
  • 90,338
  • 53
  • 357
  • 513
  • This projectis in C, with usage of some C99 features and 2-3 gcc extensions (supported by some other compilers) – osgx Jul 25 '11 at 22:56

2 Answers2

3

Unfortunately, it's not possible.

If you change popular_type definition to

typedef struct {
  double orig_fiels;
  double additional_field;
} popular_type;

without changing assignments, the compiler will throw the error conversion to non-scalar type requested, so your code won't even compile, and there is no workaround over this. The only superior scalar type above double is __float128, and you can't just use the extra bytes to store your fields.

You really have to change the assignments, unfortunately.

user703016
  • 37,307
  • 8
  • 87
  • 112
  • The needed capability of editor is not regexp, but a real C parsing and type finding. – osgx Jul 25 '11 at 15:20
  • You may get errors when you make the change because the C compiler won't normally complain about the automatic conversion if someone uses a double instead of the old popular_type. You may start getting compiler errors all over the place where people didn't do the cast properly. Good luck. – Jonathan Adelson Jul 25 '11 at 15:24
  • 2
    The easier solution is to rewrite to "`a = (popular_type){some_double_variable,};`" – osgx Jul 25 '11 at 15:27
  • Cicada, can you look to http://stackoverflow.com/questions/6457385/how-to-use-gcc-4-6-0-libquadmath-and-float128-on-x86-and-x86-64 ? – osgx Jul 25 '11 at 15:28
  • @Cicada: `__float128` is not standard. `long double` is at least as big as `double`, but on some implementations it may be the same size. – Keith Thompson Jul 25 '11 at 16:27
  • @osgx: That's a compound literal, a new feature in C99 that might not be supported by all compilers. – Keith Thompson Jul 25 '11 at 21:17
  • I can use c99. My major compiler is gcc, and I can shift even to new versions, like 4.5.3 – osgx Jul 25 '11 at 22:55
2

You'll need to change the code -- but first, you'll need to decided exactly what you want the code to do.

The result of converting a double to the version of popular_type that's typedef'ed as a double is well defined. After all, they're really the same type. But what is the result of converting a double to a structure? Given a double with the value 1.25, what should be the values of orig_field and additional_field in the resulting structure?

Once you've decided on the semantics, you can write a function (possibly inline) or perhaps a macro that does the conversion for you. You'll still need to change each conversion so it calls your new function or macro (and possibly some assignments without casts, since with the current definition a cast is not required, as @Sam Hoice points out). Fortunately, the compiler will tell you where this needs to be done.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631