| 1 |
// Copyright (C) 2023-2024 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU Proc Macro Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// Under Section 7 of GPL version 3, you are granted additional |
| 15 |
// permissions described in the GCC Runtime Library Exception, version |
| 16 |
// 3.1, as published by the Free Software Foundation. |
| 17 |
|
| 18 |
// You should have received a copy of the GNU General Public License and |
| 19 |
// a copy of the GCC Runtime Library Exception along with this program; |
| 20 |
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 21 |
// <http://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
#include "tokenstream.h" |
| 24 |
#include "tokentree.h" |
| 25 |
#include "registration.h" |
| 26 |
|
| 27 |
#include <cstring> |
| 28 |
|
| 29 |
namespace ProcMacro { |
| 30 |
|
| 31 |
TokenStream |
| 32 |
TokenStream::make_tokenstream (std::vector<TokenTree> vec) |
| 33 |
{ |
| 34 |
auto stream = make_tokenstream (vec.size ()); |
| 35 |
for (auto tt : vec) |
| 36 |
{ |
| 37 |
stream.push (tt); |
| 38 |
} |
| 39 |
return stream; |
| 40 |
} |
| 41 |
|
| 42 |
TokenStream |
| 43 |
TokenStream::make_tokenstream (std::uint64_t capacity) |
| 44 |
{ |
| 45 |
auto *data = new TokenTree[capacity]; |
| 46 |
return {data, 0, capacity}; |
| 47 |
} |
| 48 |
|
| 49 |
TokenStream |
| 50 |
TokenStream::make_tokenstream (std::string &source, bool &has_error) |
| 51 |
{ |
| 52 |
return __gccrs_proc_macro_ts_from_str_ (source, has_error); |
| 53 |
} |
| 54 |
|
| 55 |
void |
| 56 |
TokenStream::grow (std::uint64_t delta) |
| 57 |
{ |
| 58 |
auto new_capacity = capacity + (delta != 0 ? delta : 1); |
| 59 |
auto *new_data = new TokenTree[new_capacity]; |
| 60 |
capacity = new_capacity; |
| 61 |
std::memcpy (new_data, data, size * sizeof (TokenTree)); |
| 62 |
delete[] data; |
| 63 |
data = new_data; |
| 64 |
} |
| 65 |
|
| 66 |
void |
| 67 |
TokenStream::push (TokenTree tree) |
| 68 |
{ |
| 69 |
if (size >= capacity) |
| 70 |
grow (capacity); |
| 71 |
data[size] = tree; |
| 72 |
size++; |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
TokenStream::drop (TokenStream *stream) |
| 77 |
{ |
| 78 |
for (std::uint64_t i = 0; i < stream->size; i++) |
| 79 |
{ |
| 80 |
TokenTree::TokenTree::drop (&stream->data[i]); |
| 81 |
} |
| 82 |
delete[] stream->data; |
| 83 |
stream->capacity = 0; |
| 84 |
stream->size = 0; |
| 85 |
} |
| 86 |
|
| 87 |
extern "C" TokenStream |
| 88 |
TokenStream__new () |
| 89 |
{ |
| 90 |
return TokenStream::make_tokenstream (); |
| 91 |
} |
| 92 |
|
| 93 |
extern "C" TokenStream |
| 94 |
TokenStream__with_capacity (std::uint64_t capacity) |
| 95 |
{ |
| 96 |
return TokenStream::make_tokenstream (capacity); |
| 97 |
} |
| 98 |
|
| 99 |
extern "C" void |
| 100 |
TokenSream__push (TokenStream *stream, TokenTree tree) |
| 101 |
{ |
| 102 |
stream->push (tree); |
| 103 |
} |
| 104 |
|
| 105 |
extern "C" bool |
| 106 |
TokenStream__from_string (FFIString str, TokenStream *ts) |
| 107 |
{ |
| 108 |
bool result; |
| 109 |
auto source = str.to_string (); |
| 110 |
|
| 111 |
*ts = TokenStream::make_tokenstream (source, result); |
| 112 |
return result; |
| 113 |
} |
| 114 |
|
| 115 |
extern "C" TokenStream |
| 116 |
TokenStream__clone (const TokenStream *ts) |
| 117 |
{ |
| 118 |
auto *data = new TokenTree[ts->capacity]; |
| 119 |
std::memcpy (data, ts->data, ts->size * sizeof (TokenTree)); |
| 120 |
return {data, ts->size, ts->capacity}; |
| 121 |
} |
| 122 |
|
| 123 |
extern "C" void |
| 124 |
TokenStream__drop (TokenStream *stream) |
| 125 |
{ |
| 126 |
TokenStream::drop (stream); |
| 127 |
} |
| 128 |
|
| 129 |
} // namespace ProcMacro |