marshall.hh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Marshalling and unmarshalling.
  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. #ifndef CC1_PLUGIN_MARSHALL_HH
  16. #define CC1_PLUGIN_MARSHALL_HH
  17. #include <type_traits>
  18. #include "status.hh"
  19. #include "gcc-interface.h"
  20. namespace cc1_plugin
  21. {
  22. class connection;
  23. // Only a single kind of integer is ever sent over the wire, and
  24. // this is it.
  25. typedef unsigned long long protocol_int;
  26. // Read an integer from the connection and verify that it has the
  27. // value V.
  28. status unmarshall_check (connection *, protocol_int v);
  29. // Write an integer, prefixed with the integer type marker, to the
  30. // connection.
  31. status marshall_intlike (connection *, protocol_int);
  32. // Read a type marker from the connection and verify that it is an
  33. // integer type marker. If not, return FAIL. If so, read an
  34. // integer store it in the out argument.
  35. status unmarshall_intlike (connection *, protocol_int *);
  36. status marshall_array_start (connection *, char, size_t);
  37. status marshall_array_elmts (connection *, size_t, void *);
  38. status unmarshall_array_start (connection *, char, size_t *);
  39. status unmarshall_array_elmts (connection *, size_t, void *);
  40. // An "empty" marshall call -- used to handle the base case for some
  41. // variadic templates.
  42. static inline
  43. status marshall (connection *)
  44. {
  45. return OK;
  46. }
  47. // A template function that can handle marshalling various integer
  48. // objects to the connection.
  49. template<typename T>
  50. status marshall (connection *conn, T scalar)
  51. {
  52. return marshall_intlike (conn, scalar);
  53. }
  54. // A template function that can handle unmarshalling various integer
  55. // objects from the connection. Note that there's no way at the
  56. // protocol level to distinguish different int types.
  57. template<typename T>
  58. status unmarshall (connection *conn, T *scalar,
  59. typename std::enable_if<std::is_integral<T>::value, T>::type * = 0)
  60. {
  61. protocol_int result;
  62. if (!unmarshall_intlike (conn, &result))
  63. return FAIL;
  64. *scalar = (T) result;
  65. return OK;
  66. }
  67. // A template function that can handle unmarshalling various enum
  68. // objects from the connection.
  69. template<typename T>
  70. status unmarshall (connection *conn, T *e_val,
  71. typename std::enable_if<std::is_enum<T>::value, T>::type * = 0)
  72. {
  73. protocol_int result;
  74. if (!unmarshall_intlike (conn, &result))
  75. return FAIL;
  76. *e_val = (T) result;
  77. return OK;
  78. }
  79. // Send a string type marker followed by a string.
  80. status marshall (connection *, const char *);
  81. // Read a string type marker followed by a string. The caller is
  82. // responsible for freeing the resulting string using 'delete[]'.
  83. status unmarshall (connection *, char **);
  84. // Send a gcc_type_array marker followed by the array.
  85. status marshall (connection *, const gcc_type_array *);
  86. // Read a gcc_type_array marker, followed by a gcc_type_array. The
  87. // resulting array must be freed by the caller, using 'delete[]' on
  88. // the elements, and 'delete' on the array object itself.
  89. status unmarshall (connection *, struct gcc_type_array **);
  90. template<typename T1, typename T2, typename... Arg>
  91. status marshall (connection *c, T1 arg1, T2 arg2, Arg... rest)
  92. {
  93. if (!marshall (c, arg1))
  94. return FAIL;
  95. return marshall (c, arg2, rest...);
  96. }
  97. };
  98. #endif // CC1_PLUGIN_MARSHALL_HH