xref: /netbsd-src/external/gpl3/gcc/dist/libcc1/libcp1.cc (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1 /* The library used by gdb.
2    Copyright (C) 2014-2022 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10 
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include <cc1plugin-config.h>
21 #include <vector>
22 #include <string>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <sys/wait.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <stdlib.h>
31 #include "marshall-cp.hh"
32 #include "rpc.hh"
33 #include "connection.hh"
34 #include "names.hh"
35 #include "callbacks.hh"
36 #include "libiberty.h"
37 #include "compiler-name.hh"
38 #include "compiler.hh"
39 #include "gdbctx.hh"
40 
41 // The C compiler context that we hand back to our caller.
42 struct libcp1 : public cc1_plugin::base_gdb_plugin<gcc_cp_context>
43 {
44   explicit libcp1 (const gcc_cp_fe_vtable *);
45 
46   void add_callbacks () override;
47 
48   gcc_cp_oracle_function *binding_oracle = nullptr;
49   gcc_cp_symbol_address_function *address_oracle = nullptr;
50   gcc_cp_enter_leave_user_expr_scope_function *enter_scope = nullptr;
51   gcc_cp_enter_leave_user_expr_scope_function *leave_scope = nullptr;
52   void *oracle_datum = nullptr;
53 };
54 
libcp1(const gcc_cp_fe_vtable * cv)55 libcp1::libcp1 (const gcc_cp_fe_vtable *cv)
56   : cc1_plugin::base_gdb_plugin<gcc_cp_context> ("libcp1plugin",
57 						 CP_COMPILER_NAME,
58 						 GCC_CP_FE_VERSION_0)
59 {
60   cp_ops = cv;
61 }
62 
63 
64 
65 // Enclose these functions in an anonymous namespace because they
66 // shouldn't be exported, but they can't be static because they're
67 // used as template arguments.
68 namespace {
69   // This is a wrapper function that is called by the RPC system and
70   // that then forwards the call to the library user.  Note that the
71   // return value is not used; the type cannot be 'void' due to
72   // limitations in our simple RPC.
73   int
cp_call_binding_oracle(cc1_plugin::connection * conn,enum gcc_cp_oracle_request request,const char * identifier)74   cp_call_binding_oracle (cc1_plugin::connection *conn,
75 		       enum gcc_cp_oracle_request request,
76 		       const char *identifier)
77   {
78     libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
79 
80     self->binding_oracle (self->oracle_datum, self, request, identifier);
81     return 1;
82   }
83 
84   // This is a wrapper function that is called by the RPC system and
85   // that then forwards the call to the library user.
86   gcc_address
cp_call_symbol_address(cc1_plugin::connection * conn,const char * identifier)87   cp_call_symbol_address (cc1_plugin::connection *conn, const char *identifier)
88   {
89     libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
90 
91     return self->address_oracle (self->oracle_datum, self, identifier);
92   }
93 
94   int
cp_call_enter_scope(cc1_plugin::connection * conn)95   cp_call_enter_scope (cc1_plugin::connection *conn)
96   {
97     libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
98 
99     self->enter_scope (self->oracle_datum, self);
100     return 1;
101   }
102 
103   int
cp_call_leave_scope(cc1_plugin::connection * conn)104   cp_call_leave_scope (cc1_plugin::connection *conn)
105   {
106     libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
107 
108     self->leave_scope (self->oracle_datum, self);
109     return 1;
110   }
111 } /* anonymous namespace */
112 
113 
114 
115 static void
set_callbacks(struct gcc_cp_context * s,gcc_cp_oracle_function * binding_oracle,gcc_cp_symbol_address_function * address_oracle,gcc_cp_enter_leave_user_expr_scope_function * enter_scope,gcc_cp_enter_leave_user_expr_scope_function * leave_scope,void * datum)116 set_callbacks (struct gcc_cp_context *s,
117 	       gcc_cp_oracle_function *binding_oracle,
118 	       gcc_cp_symbol_address_function *address_oracle,
119 	       gcc_cp_enter_leave_user_expr_scope_function *enter_scope,
120 	       gcc_cp_enter_leave_user_expr_scope_function *leave_scope,
121 	       void *datum)
122 {
123   libcp1 *self = (libcp1 *) s;
124 
125   self->binding_oracle = binding_oracle;
126   self->address_oracle = address_oracle;
127   self->enter_scope = enter_scope;
128   self->leave_scope = leave_scope;
129   self->oracle_datum = datum;
130 }
131 
132 static const struct gcc_cp_fe_vtable cp_vtable =
133 {
134   GCC_CP_FE_VERSION_0,
135   set_callbacks,
136 
137 #define GCC_METHOD0(R, N) \
138   cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N>,
139 #define GCC_METHOD1(R, N, A) \
140   cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A>,
141 #define GCC_METHOD2(R, N, A, B) \
142   cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B>,
143 #define GCC_METHOD3(R, N, A, B, C) \
144   cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C>,
145 #define GCC_METHOD4(R, N, A, B, C, D) \
146   cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D>,
147 #define GCC_METHOD5(R, N, A, B, C, D, E) \
148   cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D, E>,
149 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
150   cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D, E, F, G>,
151 
152 #include "gcc-cp-fe.def"
153 
154 #undef GCC_METHOD0
155 #undef GCC_METHOD1
156 #undef GCC_METHOD2
157 #undef GCC_METHOD3
158 #undef GCC_METHOD4
159 #undef GCC_METHOD5
160 #undef GCC_METHOD7
161 };
162 
163 
164 
165 void
add_callbacks()166 libcp1::add_callbacks ()
167 {
168   cc1_plugin::callback_ftype *fun
169     = cc1_plugin::invoker<int, enum gcc_cp_oracle_request,
170 			  const char *>::invoke<cp_call_binding_oracle>;
171   connection->add_callback ("binding_oracle", fun);
172 
173   fun = cc1_plugin::invoker<gcc_address,
174 			    const char *>::invoke<cp_call_symbol_address>;
175   connection->add_callback ("address_oracle", fun);
176 
177   fun = cc1_plugin::invoker<int>::invoke<cp_call_enter_scope>;
178   connection->add_callback ("enter_scope", fun);
179 
180   fun = cc1_plugin::invoker<int>::invoke<cp_call_leave_scope>;
181   connection->add_callback ("leave_scope", fun);
182 }
183 
184 extern "C" gcc_cp_fe_context_function gcc_cp_fe_context;
185 
186 #ifdef __GNUC__
187 #pragma GCC visibility push(default)
188 #endif
189 
190 extern "C"
191 struct gcc_cp_context *
gcc_cp_fe_context(enum gcc_base_api_version base_version,enum gcc_cp_api_version cp_version)192 gcc_cp_fe_context (enum gcc_base_api_version base_version,
193 		    enum gcc_cp_api_version cp_version)
194 {
195   if ((base_version != GCC_FE_VERSION_0 && base_version != GCC_FE_VERSION_1)
196       || cp_version != GCC_CP_FE_VERSION_0)
197     return NULL;
198 
199   return new libcp1 (&cp_vtable);
200 }
201