context.hh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Generic plugin context
  2. Copyright (C) 2020-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. #ifndef CC1_PLUGIN_CONTEXT_HH
  16. #define CC1_PLUGIN_CONTEXT_HH
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tree.h"
  20. #include "connection.hh"
  21. namespace cc1_plugin
  22. {
  23. static inline unsigned long long
  24. convert_out (tree t)
  25. {
  26. return (unsigned long long) (uintptr_t) t;
  27. }
  28. static inline tree
  29. convert_in (unsigned long long v)
  30. {
  31. return (tree) (uintptr_t) v;
  32. }
  33. struct decl_addr_value
  34. {
  35. tree decl;
  36. tree address;
  37. };
  38. struct decl_addr_hasher : free_ptr_hash<decl_addr_value>
  39. {
  40. static hashval_t hash (const decl_addr_value *e)
  41. {
  42. return DECL_UID (e->decl);
  43. }
  44. static bool equal (const decl_addr_value *p1,
  45. const decl_addr_value *p2)
  46. {
  47. return p1->decl == p2->decl;
  48. }
  49. };
  50. struct string_hasher : nofree_ptr_hash<const char>
  51. {
  52. static inline hashval_t hash (const char *s)
  53. {
  54. return htab_hash_string (s);
  55. }
  56. static inline bool equal (const char *p1, const char *p2)
  57. {
  58. return strcmp (p1, p2) == 0;
  59. }
  60. };
  61. struct plugin_context : public cc1_plugin::connection
  62. {
  63. plugin_context (int fd)
  64. : cc1_plugin::connection (fd),
  65. address_map (30),
  66. preserved (30),
  67. file_names (30)
  68. {
  69. }
  70. // Map decls to addresses.
  71. hash_table<decl_addr_hasher> address_map;
  72. // A collection of trees that are preserved for the GC.
  73. hash_table< nofree_ptr_hash<tree_node> > preserved;
  74. // File name cache.
  75. hash_table<string_hasher> file_names;
  76. // Perform GC marking.
  77. void mark ();
  78. // Preserve a tree during the plugin's operation.
  79. tree preserve (tree t)
  80. {
  81. tree_node **slot = preserved.find_slot (t, INSERT);
  82. *slot = t;
  83. return t;
  84. }
  85. location_t get_location_t (const char *filename,
  86. unsigned int line_number);
  87. private:
  88. // Add a file name to FILE_NAMES and return the canonical copy.
  89. const char *intern_filename (const char *filename);
  90. };
  91. extern plugin_context *current_context;
  92. void generic_plugin_init (struct plugin_name_args *plugin_info,
  93. unsigned int version);
  94. }
  95. #endif // CC1_PLUGIN_CONTEXT_HH