* Adding a global static instance of a class and having the constructor invoked on initialization
@ 2014-01-25 5:38 Stephan Friedl
0 siblings, 0 replies; only message in thread
From: Stephan Friedl @ 2014-01-25 5:38 UTC (permalink / raw)
To: gcc-help
I am writing a GCC plugin and am trying to add a global variable which is a class instance. The class I am trying to create a global static variable from is:
class CTestClass
{
public :
CTestClass()
{
std::cout << "Test Class Initialized." << std::endl;
}
};
The key code snippet looks like this (with the appropriate type in declType):
tree globalDeclaration = build_decl( UNKNOWN_LOCATION, VAR_DECL, get_identifier( globalDecl.name().c_str() ), declType );
/* allocate static storage for this variable */
TREE_STATIC( globalDeclaration ) = 1;
/* static: internal linkage */
TREE_PUBLIC( globalDeclaration ) = 1;
DECL_EXTERNAL( globalDeclaration ) = 0;
layout_decl( globalDeclaration, false );
rest_of_decl_compilation( globalDeclaration, 1, 0 );
I have tried inserting this into a number of passes and though there do not appear to be any errors, I have not been able to get the constructor for the global to appear in the __static_initialization_and_destruction_0() function dumped using the -fdump-tree-gimple command line option.
What I am wondering is how the variables to be initialized in the __static_initialization_and_destruction_0() function are identified and if it is possible to insert a global instance of a class such that it will automatically get initialized in the __static_initialization_and_destruction_0() function -or- if I have to modify the __static_initialization_and_destruction_0() function's gimple representation and inject the call to the constructor there. I have poked through the source code for the cpp parser and nothing has leapt out at me as a possible code segment to follow as an example.
Any suggestions or pointers would be most appreciated.
Thanks,
Stephan
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2014-01-25 5:38 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-25 5:38 Adding a global static instance of a class and having the constructor invoked on initialization Stephan Friedl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).