libcc1plugin.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /* Library interface to C front end
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <cc1plugin-config.h>
  16. #undef PACKAGE_NAME
  17. #undef PACKAGE_STRING
  18. #undef PACKAGE_TARNAME
  19. #undef PACKAGE_VERSION
  20. #include "../gcc/config.h"
  21. #undef PACKAGE_NAME
  22. #undef PACKAGE_STRING
  23. #undef PACKAGE_TARNAME
  24. #undef PACKAGE_VERSION
  25. #include "gcc-plugin.h"
  26. #include "system.h"
  27. #include "coretypes.h"
  28. #include "stringpool.h"
  29. #include "gcc-interface.h"
  30. #include "hash-set.h"
  31. #include "machmode.h"
  32. #include "vec.h"
  33. #include "double-int.h"
  34. #include "input.h"
  35. #include "alias.h"
  36. #include "symtab.h"
  37. #include "options.h"
  38. #include "wide-int.h"
  39. #include "inchash.h"
  40. #include "tree.h"
  41. #include "fold-const.h"
  42. #include "stor-layout.h"
  43. #include "c-tree.h"
  44. #include "toplev.h"
  45. #include "timevar.h"
  46. #include "hash-table.h"
  47. #include "tm.h"
  48. #include "c-family/c-pragma.h"
  49. #include "c-lang.h"
  50. #include "diagnostic.h"
  51. #include "langhooks.h"
  52. #include "langhooks-def.h"
  53. #include "callbacks.hh"
  54. #include "connection.hh"
  55. #include "marshall.hh"
  56. #include "rpc.hh"
  57. #include "gcc-c-interface.h"
  58. #include "context.hh"
  59. #include <vector>
  60. using namespace cc1_plugin;
  61. // A wrapper for pushdecl that doesn't let gdb have a chance to
  62. // instantiate a symbol.
  63. static void
  64. pushdecl_safe (tree decl)
  65. {
  66. void (*save) (enum c_oracle_request, tree identifier);
  67. save = c_binding_oracle;
  68. c_binding_oracle = NULL;
  69. pushdecl (decl);
  70. c_binding_oracle = save;
  71. }
  72. static void
  73. plugin_binding_oracle (enum c_oracle_request kind, tree identifier)
  74. {
  75. enum gcc_c_oracle_request request;
  76. gcc_assert (current_context != NULL);
  77. switch (kind)
  78. {
  79. case C_ORACLE_SYMBOL:
  80. request = GCC_C_ORACLE_SYMBOL;
  81. break;
  82. case C_ORACLE_TAG:
  83. request = GCC_C_ORACLE_TAG;
  84. break;
  85. case C_ORACLE_LABEL:
  86. request = GCC_C_ORACLE_LABEL;
  87. break;
  88. default:
  89. abort ();
  90. }
  91. int ignore;
  92. cc1_plugin::call (current_context, "binding_oracle", &ignore,
  93. request, IDENTIFIER_POINTER (identifier));
  94. }
  95. static void
  96. plugin_pragma_user_expression (cpp_reader *)
  97. {
  98. c_binding_oracle = plugin_binding_oracle;
  99. }
  100. static void
  101. plugin_init_extra_pragmas (void *, void *)
  102. {
  103. c_register_pragma ("GCC", "user_expression", plugin_pragma_user_expression);
  104. }
  105. // Maybe rewrite a decl to its address.
  106. static tree
  107. address_rewriter (tree *in, int *walk_subtrees, void *arg)
  108. {
  109. plugin_context *ctx = (plugin_context *) arg;
  110. if (!DECL_P (*in) || DECL_NAME (*in) == NULL_TREE)
  111. return NULL_TREE;
  112. decl_addr_value value;
  113. value.decl = *in;
  114. decl_addr_value *found_value = ctx->address_map.find (&value);
  115. if (found_value != NULL)
  116. ;
  117. else if (DECL_IS_UNDECLARED_BUILTIN (*in))
  118. {
  119. gcc_address address;
  120. if (!cc1_plugin::call (ctx, "address_oracle", &address,
  121. IDENTIFIER_POINTER (DECL_NAME (*in))))
  122. return NULL_TREE;
  123. if (address == 0)
  124. return NULL_TREE;
  125. // Insert the decl into the address map in case it is referenced
  126. // again.
  127. value.address = build_int_cst_type (ptr_type_node, address);
  128. decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
  129. gcc_assert (*slot == NULL);
  130. *slot
  131. = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
  132. **slot = value;
  133. found_value = *slot;
  134. }
  135. else
  136. return NULL_TREE;
  137. if (found_value->address != error_mark_node)
  138. {
  139. // We have an address for the decl, so rewrite the tree.
  140. tree ptr_type = build_pointer_type (TREE_TYPE (*in));
  141. *in = fold_build1 (INDIRECT_REF, TREE_TYPE (*in),
  142. fold_build1 (CONVERT_EXPR, ptr_type,
  143. found_value->address));
  144. }
  145. *walk_subtrees = 0;
  146. return NULL_TREE;
  147. }
  148. // When generating code for gdb, we want to be able to use absolute
  149. // addresses to refer to otherwise external objects that gdb knows
  150. // about. gdb passes in these addresses when building decls, and then
  151. // before gimplification we go through the trees, rewriting uses to
  152. // the equivalent of "*(TYPE *) ADDR".
  153. static void
  154. rewrite_decls_to_addresses (void *function_in, void *)
  155. {
  156. tree function = (tree) function_in;
  157. // Do nothing if we're not in gdb.
  158. if (current_context == NULL)
  159. return;
  160. walk_tree (&DECL_SAVED_TREE (function), address_rewriter, current_context,
  161. NULL);
  162. }
  163. gcc_decl
  164. plugin_build_decl (cc1_plugin::connection *self,
  165. const char *name,
  166. enum gcc_c_symbol_kind sym_kind,
  167. gcc_type sym_type_in,
  168. const char *substitution_name,
  169. gcc_address address,
  170. const char *filename,
  171. unsigned int line_number)
  172. {
  173. plugin_context *ctx = static_cast<plugin_context *> (self);
  174. tree identifier = get_identifier (name);
  175. enum tree_code code;
  176. tree decl;
  177. tree sym_type = convert_in (sym_type_in);
  178. switch (sym_kind)
  179. {
  180. case GCC_C_SYMBOL_FUNCTION:
  181. code = FUNCTION_DECL;
  182. break;
  183. case GCC_C_SYMBOL_VARIABLE:
  184. code = VAR_DECL;
  185. break;
  186. case GCC_C_SYMBOL_TYPEDEF:
  187. code = TYPE_DECL;
  188. break;
  189. case GCC_C_SYMBOL_LABEL:
  190. // FIXME: we aren't ready to handle labels yet.
  191. // It isn't clear how to translate them properly
  192. // and in any case a "goto" isn't likely to work.
  193. return convert_out (error_mark_node);
  194. default:
  195. abort ();
  196. }
  197. location_t loc = ctx->get_location_t (filename, line_number);
  198. decl = build_decl (loc, code, identifier, sym_type);
  199. TREE_USED (decl) = 1;
  200. TREE_ADDRESSABLE (decl) = 1;
  201. if (sym_kind != GCC_C_SYMBOL_TYPEDEF)
  202. {
  203. decl_addr_value value;
  204. DECL_EXTERNAL (decl) = 1;
  205. value.decl = decl;
  206. if (substitution_name != NULL)
  207. {
  208. // If the translator gave us a name without a binding,
  209. // we can just substitute error_mark_node, since we know the
  210. // translator will be reporting an error anyhow.
  211. value.address
  212. = lookup_name (get_identifier (substitution_name));
  213. if (value.address == NULL_TREE)
  214. value.address = error_mark_node;
  215. }
  216. else
  217. value.address = build_int_cst_type (ptr_type_node, address);
  218. decl_addr_value **slot = ctx->address_map.find_slot (&value, INSERT);
  219. gcc_assert (*slot == NULL);
  220. *slot
  221. = static_cast<decl_addr_value *> (xmalloc (sizeof (decl_addr_value)));
  222. **slot = value;
  223. }
  224. return convert_out (ctx->preserve (decl));
  225. }
  226. int
  227. plugin_bind (cc1_plugin::connection *,
  228. gcc_decl decl_in, int is_global)
  229. {
  230. tree decl = convert_in (decl_in);
  231. c_bind (DECL_SOURCE_LOCATION (decl), decl, is_global);
  232. rest_of_decl_compilation (decl, is_global, 0);
  233. return 1;
  234. }
  235. int
  236. plugin_tagbind (cc1_plugin::connection *self,
  237. const char *name, gcc_type tagged_type,
  238. const char *filename, unsigned int line_number)
  239. {
  240. plugin_context *ctx = static_cast<plugin_context *> (self);
  241. tree t = convert_in (tagged_type), x;
  242. c_pushtag (ctx->get_location_t (filename, line_number),
  243. get_identifier (name), t);
  244. /* Propagate the newly-added type name so that previously-created
  245. variant types are not disconnected from their main variants. */
  246. for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
  247. TYPE_NAME (x) = TYPE_NAME (t);
  248. return 1;
  249. }
  250. gcc_type
  251. plugin_build_pointer_type (cc1_plugin::connection *,
  252. gcc_type base_type)
  253. {
  254. // No need to preserve a pointer type as the base type is preserved.
  255. return convert_out (build_pointer_type (convert_in (base_type)));
  256. }
  257. // TYPE_NAME needs to be a valid pointer, even if there is no name available.
  258. static tree
  259. build_anonymous_node (enum tree_code code)
  260. {
  261. tree node = make_node (code);
  262. tree type_decl = build_decl (input_location, TYPE_DECL, NULL_TREE, node);
  263. TYPE_NAME (node) = type_decl;
  264. TYPE_STUB_DECL (node) = type_decl;
  265. return node;
  266. }
  267. gcc_type
  268. plugin_build_record_type (cc1_plugin::connection *self)
  269. {
  270. plugin_context *ctx = static_cast<plugin_context *> (self);
  271. return convert_out (ctx->preserve (build_anonymous_node (RECORD_TYPE)));
  272. }
  273. gcc_type
  274. plugin_build_union_type (cc1_plugin::connection *self)
  275. {
  276. plugin_context *ctx = static_cast<plugin_context *> (self);
  277. return convert_out (ctx->preserve (build_anonymous_node (UNION_TYPE)));
  278. }
  279. int
  280. plugin_build_add_field (cc1_plugin::connection *,
  281. gcc_type record_or_union_type_in,
  282. const char *field_name,
  283. gcc_type field_type_in,
  284. unsigned long bitsize,
  285. unsigned long bitpos)
  286. {
  287. tree record_or_union_type = convert_in (record_or_union_type_in);
  288. tree field_type = convert_in (field_type_in);
  289. gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE
  290. || TREE_CODE (record_or_union_type) == UNION_TYPE);
  291. /* Note that gdb does not preserve the location of field decls, so
  292. we can't provide a decent location here. */
  293. tree decl = build_decl (BUILTINS_LOCATION, FIELD_DECL,
  294. get_identifier (field_name), field_type);
  295. DECL_FIELD_CONTEXT (decl) = record_or_union_type;
  296. if (TREE_CODE (field_type) == INTEGER_TYPE
  297. && TYPE_PRECISION (field_type) != bitsize)
  298. {
  299. DECL_BIT_FIELD_TYPE (decl) = field_type;
  300. TREE_TYPE (decl)
  301. = c_build_bitfield_integer_type (bitsize, TYPE_UNSIGNED (field_type));
  302. }
  303. SET_DECL_MODE (decl, TYPE_MODE (TREE_TYPE (decl)));
  304. // There's no way to recover this from DWARF.
  305. SET_DECL_OFFSET_ALIGN (decl, TYPE_PRECISION (pointer_sized_int_node));
  306. tree pos = bitsize_int (bitpos);
  307. pos_from_bit (&DECL_FIELD_OFFSET (decl), &DECL_FIELD_BIT_OFFSET (decl),
  308. DECL_OFFSET_ALIGN (decl), pos);
  309. DECL_SIZE (decl) = bitsize_int (bitsize);
  310. DECL_SIZE_UNIT (decl) = size_int ((bitsize + BITS_PER_UNIT - 1)
  311. / BITS_PER_UNIT);
  312. DECL_CHAIN (decl) = TYPE_FIELDS (record_or_union_type);
  313. TYPE_FIELDS (record_or_union_type) = decl;
  314. return 1;
  315. }
  316. int
  317. plugin_finish_record_or_union (cc1_plugin::connection *,
  318. gcc_type record_or_union_type_in,
  319. unsigned long size_in_bytes)
  320. {
  321. tree record_or_union_type = convert_in (record_or_union_type_in);
  322. gcc_assert (TREE_CODE (record_or_union_type) == RECORD_TYPE
  323. || TREE_CODE (record_or_union_type) == UNION_TYPE);
  324. /* We built the field list in reverse order, so fix it now. */
  325. TYPE_FIELDS (record_or_union_type)
  326. = nreverse (TYPE_FIELDS (record_or_union_type));
  327. if (TREE_CODE (record_or_union_type) == UNION_TYPE)
  328. {
  329. /* Unions can just be handled by the generic code. */
  330. layout_type (record_or_union_type);
  331. }
  332. else
  333. {
  334. // FIXME there's no way to get this from DWARF,
  335. // or even, it seems, a particularly good way to deduce it.
  336. SET_TYPE_ALIGN (record_or_union_type,
  337. TYPE_PRECISION (pointer_sized_int_node));
  338. TYPE_SIZE (record_or_union_type) = bitsize_int (size_in_bytes
  339. * BITS_PER_UNIT);
  340. TYPE_SIZE_UNIT (record_or_union_type) = size_int (size_in_bytes);
  341. compute_record_mode (record_or_union_type);
  342. finish_bitfield_layout (record_or_union_type);
  343. // FIXME we have no idea about TYPE_PACKED
  344. }
  345. tree t = record_or_union_type, x;
  346. for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
  347. {
  348. /* Like finish_struct, update the qualified variant types. */
  349. TYPE_FIELDS (x) = TYPE_FIELDS (t);
  350. TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
  351. C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
  352. C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
  353. C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
  354. /* We copy these fields too. */
  355. SET_TYPE_ALIGN (x, TYPE_ALIGN (t));
  356. TYPE_SIZE (x) = TYPE_SIZE (t);
  357. TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
  358. if (x != record_or_union_type)
  359. compute_record_mode (x);
  360. }
  361. return 1;
  362. }
  363. gcc_type
  364. plugin_build_enum_type (cc1_plugin::connection *self,
  365. gcc_type underlying_int_type_in)
  366. {
  367. tree underlying_int_type = convert_in (underlying_int_type_in);
  368. if (underlying_int_type == error_mark_node)
  369. return convert_out (error_mark_node);
  370. tree result = build_anonymous_node (ENUMERAL_TYPE);
  371. TYPE_PRECISION (result) = TYPE_PRECISION (underlying_int_type);
  372. TYPE_UNSIGNED (result) = TYPE_UNSIGNED (underlying_int_type);
  373. plugin_context *ctx = static_cast<plugin_context *> (self);
  374. return convert_out (ctx->preserve (result));
  375. }
  376. int
  377. plugin_build_add_enum_constant (cc1_plugin::connection *,
  378. gcc_type enum_type_in,
  379. const char *name,
  380. unsigned long value)
  381. {
  382. tree cst, decl, cons;
  383. tree enum_type = convert_in (enum_type_in);
  384. gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE);
  385. cst = build_int_cst (enum_type, value);
  386. /* Note that gdb does not preserve the location of enum constants,
  387. so we can't provide a decent location here. */
  388. decl = build_decl (BUILTINS_LOCATION, CONST_DECL,
  389. get_identifier (name), enum_type);
  390. DECL_INITIAL (decl) = cst;
  391. pushdecl_safe (decl);
  392. cons = tree_cons (DECL_NAME (decl), cst, TYPE_VALUES (enum_type));
  393. TYPE_VALUES (enum_type) = cons;
  394. return 1;
  395. }
  396. int
  397. plugin_finish_enum_type (cc1_plugin::connection *,
  398. gcc_type enum_type_in)
  399. {
  400. tree enum_type = convert_in (enum_type_in);
  401. tree minnode, maxnode, iter;
  402. iter = TYPE_VALUES (enum_type);
  403. minnode = maxnode = TREE_VALUE (iter);
  404. for (iter = TREE_CHAIN (iter);
  405. iter != NULL_TREE;
  406. iter = TREE_CHAIN (iter))
  407. {
  408. tree value = TREE_VALUE (iter);
  409. if (tree_int_cst_lt (maxnode, value))
  410. maxnode = value;
  411. if (tree_int_cst_lt (value, minnode))
  412. minnode = value;
  413. }
  414. TYPE_MIN_VALUE (enum_type) = minnode;
  415. TYPE_MAX_VALUE (enum_type) = maxnode;
  416. layout_type (enum_type);
  417. return 1;
  418. }
  419. gcc_type
  420. plugin_build_function_type (cc1_plugin::connection *self,
  421. gcc_type return_type_in,
  422. const struct gcc_type_array *argument_types_in,
  423. int is_varargs)
  424. {
  425. tree return_type = convert_in (return_type_in);
  426. tree result;
  427. std::vector<tree> argument_types (argument_types_in->n_elements);
  428. for (int i = 0; i < argument_types_in->n_elements; ++i)
  429. argument_types[i] = convert_in (argument_types_in->elements[i]);
  430. if (is_varargs)
  431. result = build_varargs_function_type_array (return_type,
  432. argument_types_in->n_elements,
  433. argument_types.data ());
  434. else
  435. result = build_function_type_array (return_type,
  436. argument_types_in->n_elements,
  437. argument_types.data ());
  438. plugin_context *ctx = static_cast<plugin_context *> (self);
  439. return convert_out (ctx->preserve (result));
  440. }
  441. /* Return a builtin type associated with BUILTIN_NAME. */
  442. static tree
  443. safe_lookup_builtin_type (const char *builtin_name)
  444. {
  445. tree result = NULL_TREE;
  446. if (!builtin_name)
  447. return result;
  448. result = identifier_global_value (get_identifier (builtin_name));
  449. if (!result)
  450. return result;
  451. gcc_assert (TREE_CODE (result) == TYPE_DECL);
  452. result = TREE_TYPE (result);
  453. return result;
  454. }
  455. static gcc_type
  456. plugin_int_check (cc1_plugin::connection *self,
  457. int is_unsigned, unsigned long size_in_bytes,
  458. tree result)
  459. {
  460. if (result == NULL_TREE)
  461. result = error_mark_node;
  462. else
  463. {
  464. gcc_assert (!TYPE_UNSIGNED (result) == !is_unsigned);
  465. gcc_assert (TREE_CODE (TYPE_SIZE (result)) == INTEGER_CST);
  466. gcc_assert (TYPE_PRECISION (result) == BITS_PER_UNIT * size_in_bytes);
  467. plugin_context *ctx = static_cast<plugin_context *> (self);
  468. ctx->preserve (result);
  469. }
  470. return convert_out (result);
  471. }
  472. gcc_type
  473. plugin_int_type_v0 (cc1_plugin::connection *self,
  474. int is_unsigned, unsigned long size_in_bytes)
  475. {
  476. tree result = c_common_type_for_size (BITS_PER_UNIT * size_in_bytes,
  477. is_unsigned);
  478. return plugin_int_check (self, is_unsigned, size_in_bytes, result);
  479. }
  480. gcc_type
  481. plugin_int_type (cc1_plugin::connection *self,
  482. int is_unsigned, unsigned long size_in_bytes,
  483. const char *builtin_name)
  484. {
  485. if (!builtin_name)
  486. return plugin_int_type_v0 (self, is_unsigned, size_in_bytes);
  487. tree result = safe_lookup_builtin_type (builtin_name);
  488. gcc_assert (!result || TREE_CODE (result) == INTEGER_TYPE);
  489. return plugin_int_check (self, is_unsigned, size_in_bytes, result);
  490. }
  491. gcc_type
  492. plugin_char_type (cc1_plugin::connection *)
  493. {
  494. return convert_out (char_type_node);
  495. }
  496. gcc_type
  497. plugin_float_type_v0 (cc1_plugin::connection *,
  498. unsigned long size_in_bytes)
  499. {
  500. if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (float_type_node))
  501. return convert_out (float_type_node);
  502. if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (double_type_node))
  503. return convert_out (double_type_node);
  504. if (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (long_double_type_node))
  505. return convert_out (long_double_type_node);
  506. return convert_out (error_mark_node);
  507. }
  508. gcc_type
  509. plugin_float_type (cc1_plugin::connection *self,
  510. unsigned long size_in_bytes,
  511. const char *builtin_name)
  512. {
  513. if (!builtin_name)
  514. return plugin_float_type_v0 (self, size_in_bytes);
  515. tree result = safe_lookup_builtin_type (builtin_name);
  516. if (!result)
  517. return convert_out (error_mark_node);
  518. gcc_assert (TREE_CODE (result) == REAL_TYPE);
  519. gcc_assert (BITS_PER_UNIT * size_in_bytes == TYPE_PRECISION (result));
  520. return convert_out (result);
  521. }
  522. gcc_type
  523. plugin_void_type (cc1_plugin::connection *)
  524. {
  525. return convert_out (void_type_node);
  526. }
  527. gcc_type
  528. plugin_bool_type (cc1_plugin::connection *)
  529. {
  530. return convert_out (boolean_type_node);
  531. }
  532. gcc_type
  533. plugin_build_array_type (cc1_plugin::connection *self,
  534. gcc_type element_type_in, int num_elements)
  535. {
  536. tree element_type = convert_in (element_type_in);
  537. tree result;
  538. if (num_elements == -1)
  539. result = build_array_type (element_type, NULL_TREE);
  540. else
  541. result = build_array_type_nelts (element_type, num_elements);
  542. plugin_context *ctx = static_cast<plugin_context *> (self);
  543. return convert_out (ctx->preserve (result));
  544. }
  545. gcc_type
  546. plugin_build_vla_array_type (cc1_plugin::connection *self,
  547. gcc_type element_type_in,
  548. const char *upper_bound_name)
  549. {
  550. tree element_type = convert_in (element_type_in);
  551. tree upper_bound = lookup_name (get_identifier (upper_bound_name));
  552. tree range = build_index_type (upper_bound);
  553. tree result = build_array_type (element_type, range);
  554. C_TYPE_VARIABLE_SIZE (result) = 1;
  555. plugin_context *ctx = static_cast<plugin_context *> (self);
  556. return convert_out (ctx->preserve (result));
  557. }
  558. gcc_type
  559. plugin_build_qualified_type (cc1_plugin::connection *,
  560. gcc_type unqualified_type_in,
  561. enum gcc_qualifiers qualifiers)
  562. {
  563. tree unqualified_type = convert_in (unqualified_type_in);
  564. int quals = 0;
  565. if ((qualifiers & GCC_QUALIFIER_CONST) != 0)
  566. quals |= TYPE_QUAL_CONST;
  567. if ((qualifiers & GCC_QUALIFIER_VOLATILE) != 0)
  568. quals |= TYPE_QUAL_VOLATILE;
  569. if ((qualifiers & GCC_QUALIFIER_RESTRICT) != 0)
  570. quals |= TYPE_QUAL_RESTRICT;
  571. return convert_out (build_qualified_type (unqualified_type, quals));
  572. }
  573. gcc_type
  574. plugin_build_complex_type (cc1_plugin::connection *self,
  575. gcc_type base_type)
  576. {
  577. plugin_context *ctx = static_cast<plugin_context *> (self);
  578. return convert_out (ctx->preserve (build_complex_type (convert_in (base_type))));
  579. }
  580. gcc_type
  581. plugin_build_vector_type (cc1_plugin::connection *self,
  582. gcc_type base_type, int nunits)
  583. {
  584. plugin_context *ctx = static_cast<plugin_context *> (self);
  585. return convert_out (ctx->preserve (build_vector_type (convert_in (base_type),
  586. nunits)));
  587. }
  588. int
  589. plugin_build_constant (cc1_plugin::connection *self, gcc_type type_in,
  590. const char *name, unsigned long value,
  591. const char *filename, unsigned int line_number)
  592. {
  593. plugin_context *ctx = static_cast<plugin_context *> (self);
  594. tree cst, decl;
  595. tree type = convert_in (type_in);
  596. cst = build_int_cst (type, value);
  597. decl = build_decl (ctx->get_location_t (filename, line_number),
  598. CONST_DECL, get_identifier (name), type);
  599. DECL_INITIAL (decl) = cst;
  600. pushdecl_safe (decl);
  601. return 1;
  602. }
  603. gcc_type
  604. plugin_error (cc1_plugin::connection *,
  605. const char *message)
  606. {
  607. error ("%s", message);
  608. return convert_out (error_mark_node);
  609. }
  610. #ifdef __GNUC__
  611. #pragma GCC visibility push(default)
  612. #endif
  613. int
  614. plugin_init (struct plugin_name_args *plugin_info,
  615. struct plugin_gcc_version *)
  616. {
  617. generic_plugin_init (plugin_info, GCC_C_FE_VERSION_1);
  618. register_callback (plugin_info->base_name, PLUGIN_PRAGMAS,
  619. plugin_init_extra_pragmas, NULL);
  620. register_callback (plugin_info->base_name, PLUGIN_PRE_GENERICIZE,
  621. rewrite_decls_to_addresses, NULL);
  622. #define GCC_METHOD0(R, N) \
  623. { \
  624. cc1_plugin::callback_ftype *fun \
  625. = cc1_plugin::invoker<R>::invoke<plugin_ ## N>; \
  626. current_context->add_callback (# N, fun); \
  627. }
  628. #define GCC_METHOD1(R, N, A) \
  629. { \
  630. cc1_plugin::callback_ftype *fun \
  631. = cc1_plugin::invoker<R, A>::invoke<plugin_ ## N>; \
  632. current_context->add_callback (# N, fun); \
  633. }
  634. #define GCC_METHOD2(R, N, A, B) \
  635. { \
  636. cc1_plugin::callback_ftype *fun \
  637. = cc1_plugin::invoker<R, A, B>::invoke<plugin_ ## N>; \
  638. current_context->add_callback (# N, fun); \
  639. }
  640. #define GCC_METHOD3(R, N, A, B, C) \
  641. { \
  642. cc1_plugin::callback_ftype *fun \
  643. = cc1_plugin::invoker<R, A, B, C>::invoke<plugin_ ## N>; \
  644. current_context->add_callback (# N, fun); \
  645. }
  646. #define GCC_METHOD4(R, N, A, B, C, D) \
  647. { \
  648. cc1_plugin::callback_ftype *fun \
  649. = cc1_plugin::invoker<R, A, B, C, \
  650. D>::invoke<plugin_ ## N>; \
  651. current_context->add_callback (# N, fun); \
  652. }
  653. #define GCC_METHOD5(R, N, A, B, C, D, E) \
  654. { \
  655. cc1_plugin::callback_ftype *fun \
  656. = cc1_plugin::invoker<R, A, B, C, D, \
  657. E>::invoke<plugin_ ## N>; \
  658. current_context->add_callback (# N, fun); \
  659. }
  660. #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
  661. { \
  662. cc1_plugin::callback_ftype *fun \
  663. = cc1_plugin::invoker<R, A, B, C, D, \
  664. E, F, G>::invoke<plugin_ ## N>; \
  665. current_context->add_callback (# N, fun); \
  666. }
  667. #include "gcc-c-fe.def"
  668. #undef GCC_METHOD0
  669. #undef GCC_METHOD1
  670. #undef GCC_METHOD2
  671. #undef GCC_METHOD3
  672. #undef GCC_METHOD4
  673. #undef GCC_METHOD5
  674. #undef GCC_METHOD7
  675. return 0;
  676. }