]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgrust/libproc_macro_internal/tokenstream.cc
685f28424d24acb609f11eb63b84e6b0d1e0f942
1 // Copyright (C) 2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
23 #include "tokenstream.h"
24 #include "tokentree.h"
25 #include "registration.h"
32 TokenStream::make_tokenstream (std::vector
<TokenTree
> vec
)
34 auto stream
= make_tokenstream (vec
.size ());
43 TokenStream::make_tokenstream (std::uint64_t capacity
)
45 auto *data
= new TokenTree
[capacity
];
46 return {data
, 0, capacity
};
50 TokenStream::make_tokenstream (std::string
&source
, bool &has_error
)
52 return __gccrs_proc_macro_ts_from_str_ (source
, has_error
);
56 TokenStream::grow (std::uint64_t delta
)
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
));
67 TokenStream::push (TokenTree tree
)
76 TokenStream::drop (TokenStream
*stream
)
78 for (std::uint64_t i
= 0; i
< stream
->size
; i
++)
80 TokenTree::TokenTree::drop (&stream
->data
[i
]);
82 delete[] stream
->data
;
87 extern "C" TokenStream
90 return TokenStream::make_tokenstream ();
93 extern "C" TokenStream
94 TokenStream__with_capacity (std::uint64_t capacity
)
96 return TokenStream::make_tokenstream (capacity
);
100 TokenSream__push (TokenStream
*stream
, TokenTree tree
)
106 TokenStream__from_string (FFIString str
, TokenStream
*ts
)
109 auto source
= str
.to_string ();
111 *ts
= TokenStream::make_tokenstream (source
, result
);
115 extern "C" TokenStream
116 TokenStream__clone (const TokenStream
*ts
)
118 auto *data
= new TokenTree
[ts
->capacity
];
119 std::memcpy (data
, ts
->data
, ts
->size
* sizeof (TokenTree
));
120 return {data
, ts
->size
, ts
->capacity
};
124 TokenStream__drop (TokenStream
*stream
)
126 TokenStream::drop (stream
);
129 } // namespace ProcMacro