public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Stephan Friedl <stephanf01@yahoo.com>
To: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Adding a global static instance of a class and having the constructor invoked on initialization
Date: Sat, 25 Jan 2014 05:38:00 -0000	[thread overview]
Message-ID: <1390628315.14186.YahooMailNeo@web125802.mail.ne1.yahoo.com> (raw)

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

                 reply	other threads:[~2014-01-25  5:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1390628315.14186.YahooMailNeo@web125802.mail.ne1.yahoo.com \
    --to=stephanf01@yahoo.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).