From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9730 invoked by alias); 25 Jan 2014 05:38:41 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 9716 invoked by uid 89); 25 Jan 2014 05:38:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=3.2 required=5.0 tests=AWL,BAYES_50,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,FREEMAIL_REPLYTO_END_DIGIT,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: nm9-vm3.bullet.mail.ne1.yahoo.com Received: from nm9-vm3.bullet.mail.ne1.yahoo.com (HELO nm9-vm3.bullet.mail.ne1.yahoo.com) (98.138.91.139) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Sat, 25 Jan 2014 05:38:38 +0000 Received: from [98.138.100.116] by nm9.bullet.mail.ne1.yahoo.com with NNFMP; 25 Jan 2014 05:38:35 -0000 Received: from [98.138.101.166] by tm107.bullet.mail.ne1.yahoo.com with NNFMP; 25 Jan 2014 05:38:35 -0000 Received: from [127.0.0.1] by omp1077.mail.ne1.yahoo.com with NNFMP; 25 Jan 2014 05:38:35 -0000 Received: (qmail 16710 invoked by uid 60001); 25 Jan 2014 05:38:35 -0000 Received: from [67.173.246.76] by web125802.mail.ne1.yahoo.com via HTTP; Fri, 24 Jan 2014 21:38:35 PST Message-ID: <1390628315.14186.YahooMailNeo@web125802.mail.ne1.yahoo.com> Date: Sat, 25 Jan 2014 05:38:00 -0000 From: Stephan Friedl Reply-To: Stephan Friedl Subject: Adding a global static instance of a class and having the constructor invoked on initialization To: "gcc-help@gcc.gnu.org" MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-SW-Source: 2014-01/txt/msg00091.txt.bz2 I am writing a GCC plugin and am trying to add a global variable which is a= class instance. =A0The class I am trying to create a global static variabl= e from is: class CTestClass { public : =A0 =A0 CTestClass() =A0 =A0 { =A0 =A0 =A0 =A0 std::cout << "Test Class Initialized." << std::endl; =A0 =A0 } }; The key code snippet looks like this (with the appropriate type in declType= ): tree globalDeclaration =3D build_decl( UNKNOWN_LOCATION,=A0VAR_DECL,=A0get_= identifier( globalDecl.name().c_str() ),=A0declType ); /* allocate static storage for this variable */ TREE_STATIC( globalDeclaration ) =3D 1; /* static: internal linkage */ TREE_PUBLIC( globalDeclaration ) =3D 1; DECL_EXTERNAL( globalDeclaration ) =3D 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 t= he global to appear in the=A0__static_initialization_and_destruction_0() fu= nction dumped using the=A0-fdump-tree-gimple command line option. What I am wondering is how the variables to be initialized in the=A0=A0__st= atic_initialization_and_destruction_0() function are identified and if it i= s possible to insert a global instance of a class such that it will automat= ically get initialized in the=A0=A0__static_initialization_and_destruction_= 0() function -or- if I have to modify the=A0=A0__static_initialization_and_= destruction_0() function's gimple representation and inject the call to the= constructor there. =A0I have poked through the source code for the cpp par= ser 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