Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C callback functions defined in an unnamed namespace?

I have a C++ project that uses a C bison parser. The C parser uses a struct of function pointers to call functions that create proper AST nodes when productions are reduced by bison:

typedef void Node;
struct Actions {
  Node *(*newIntLit)(int val);
  Node *(*newAsgnExpr)(Node *left, Node *right);
  /* ... */
};

Now, in the C++ part of the project, i fill those pointers

class AstNode {
  /* ... */
};
class IntLit : public AstNode { 
  /* ... */
};

extern "C" {
  Node *newIntLit(int val) {
    return (Node*)new IntLit(val);
  }

  /* ... */
}

Actions createActions() {
  Actions a;
  a.newIntLit = &newIntLit;
  /* ... */
  return a;
}

Now the only reason i put them within extern "C" is because i want them to have C calling conventions. But optimally, i would like their names still be mangled. They are never called by-name from C code, so name mangling isn't an issue. Having them mangled will avoid name conflicts, since some actions are called like error, and the C++ callback function has ugly names like the following just to avoid name clashes with other modules.

extern "C" {
  void uglyNameError(char const *str) {
    /* ... */
  }

  /* ... */
}

a.error = &uglyNameError;

I wondered whether it could be possible by merely giving the function type C linkage

extern "C" void fty(char const *str);
namespace {
  fty error; /* Declared! But i can i define it with that type!? */
}

Any ideas? I'm looking for Standard-C++ solutions.

like image 939
Johannes Schaub - litb Avatar asked May 09 '10 10:05

Johannes Schaub - litb


1 Answers

I don't get the problem. The extern keyword does not affect the calling convention, merely the name presented to the linker. A function written in C++ that is not an instance method is still __cdecl, with or without extern "C". Furthermore, as long as you keep createActions() in the same source code file, these functions don't need external linkage. You could declare them static or put them in an unnamed namespace to avoid collisions.

like image 142
Hans Passant Avatar answered Sep 30 '22 01:09

Hans Passant