(root)/
gcc-13.2.0/
gcc/
json.h
       1  /* JSON trees
       2     Copyright (C) 2017-2023 Free Software Foundation, Inc.
       3     Contributed by David Malcolm <dmalcolm@redhat.com>.
       4  
       5  This file is part of GCC.
       6  
       7  GCC is free software; you can redistribute it and/or modify it under
       8  the terms of the GNU General Public License as published by the Free
       9  Software Foundation; either version 3, or (at your option) any later
      10  version.
      11  
      12  GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13  WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15  for more details.
      16  
      17  You should have received a copy of the GNU General Public License
      18  along with GCC; see the file COPYING3.  If not see
      19  <http://www.gnu.org/licenses/>.  */
      20  
      21  #ifndef GCC_JSON_H
      22  #define GCC_JSON_H
      23  
      24  /* Implementation of JSON, a lightweight data-interchange format.
      25  
      26     See http://www.json.org/
      27     and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
      28     and https://tools.ietf.org/html/rfc7159
      29  
      30     Supports creating a DOM-like tree of json::value *, and then dumping
      31     json::value * to text.  */
      32  
      33  namespace json
      34  {
      35  
      36  /* Forward decls of json::value and its subclasses (using indentation
      37     to denote inheritance.  */
      38  
      39  class value;
      40    class object;
      41    class array;
      42    class float_number;
      43    class integer_number;
      44    class string;
      45    class literal;
      46  
      47  /* An enum for discriminating the subclasses of json::value.  */
      48  
      49  enum kind
      50  {
      51    /* class json::object.  */
      52    JSON_OBJECT,
      53  
      54    /* class json::array.  */
      55    JSON_ARRAY,
      56  
      57    /* class json::integer_number.  */
      58    JSON_INTEGER,
      59  
      60    /* class json::float_number.  */
      61    JSON_FLOAT,
      62  
      63    /* class json::string.  */
      64    JSON_STRING,
      65  
      66    /* class json::literal uses these three values to identify the
      67       particular literal.  */
      68    JSON_TRUE,
      69    JSON_FALSE,
      70    JSON_NULL
      71  };
      72  
      73  /* Base class of JSON value.  */
      74  
      75  class value
      76  {
      77   public:
      78    virtual ~value () {}
      79    virtual enum kind get_kind () const = 0;
      80    virtual void print (pretty_printer *pp) const = 0;
      81  
      82    void dump (FILE *) const;
      83  };
      84  
      85  /* Subclass of value for objects: a collection of key/value pairs
      86     preserving the ordering in which keys were inserted.
      87  
      88     Preserving the order eliminates non-determinism in the output,
      89     making it easier for the user to compare repeated invocations.  */
      90  
      91  class object : public value
      92  {
      93   public:
      94    ~object ();
      95  
      96    enum kind get_kind () const final override { return JSON_OBJECT; }
      97    void print (pretty_printer *pp) const final override;
      98  
      99    void set (const char *key, value *v);
     100    value *get (const char *key) const;
     101  
     102   private:
     103    typedef hash_map <char *, value *,
     104      simple_hashmap_traits<nofree_string_hash, value *> > map_t;
     105    map_t m_map;
     106  
     107    /* Keep track of order in which keys were inserted.  */
     108    auto_vec <const char *> m_keys;
     109  };
     110  
     111  /* Subclass of value for arrays.  */
     112  
     113  class array : public value
     114  {
     115   public:
     116    ~array ();
     117  
     118    enum kind get_kind () const final override { return JSON_ARRAY; }
     119    void print (pretty_printer *pp) const final override;
     120  
     121    void append (value *v);
     122  
     123   private:
     124    auto_vec<value *> m_elements;
     125  };
     126  
     127  /* Subclass of value for floating-point numbers.  */
     128  
     129  class float_number : public value
     130  {
     131   public:
     132    float_number (double value) : m_value (value) {}
     133  
     134    enum kind get_kind () const final override { return JSON_FLOAT; }
     135    void print (pretty_printer *pp) const final override;
     136  
     137    double get () const { return m_value; }
     138  
     139   private:
     140    double m_value;
     141  };
     142  
     143  /* Subclass of value for integer-valued numbers.  */
     144  
     145  class integer_number : public value
     146  {
     147   public:
     148    integer_number (long value) : m_value (value) {}
     149  
     150    enum kind get_kind () const final override { return JSON_INTEGER; }
     151    void print (pretty_printer *pp) const final override;
     152  
     153    long get () const { return m_value; }
     154  
     155   private:
     156    long m_value;
     157  };
     158  
     159  
     160  /* Subclass of value for strings.  */
     161  
     162  class string : public value
     163  {
     164   public:
     165    explicit string (const char *utf8);
     166    string (const char *utf8, size_t len);
     167    ~string () { free (m_utf8); }
     168  
     169    enum kind get_kind () const final override { return JSON_STRING; }
     170    void print (pretty_printer *pp) const final override;
     171  
     172    const char *get_string () const { return m_utf8; }
     173    size_t get_length () const { return m_len; }
     174  
     175   private:
     176    char *m_utf8;
     177    size_t m_len;
     178  };
     179  
     180  /* Subclass of value for the three JSON literals "true", "false",
     181     and "null".  */
     182  
     183  class literal : public value
     184  {
     185   public:
     186    literal (enum kind kind) : m_kind (kind) {}
     187  
     188    /* Construct literal for a boolean value.  */
     189    literal (bool value): m_kind (value ? JSON_TRUE : JSON_FALSE) {}
     190  
     191    enum kind get_kind () const final override { return m_kind; }
     192    void print (pretty_printer *pp) const final override;
     193  
     194   private:
     195    enum kind m_kind;
     196  };
     197  
     198  } // namespace json
     199  
     200  #endif  /* GCC_JSON_H  */