@@ -150,7 +150,7 @@ typedef unsigned SyntaxNodeId;
150
150
// /
151
151
// / This is implementation detail - do not expose it in public API.
152
152
class RawSyntax final
153
- : private llvm::TrailingObjects<RawSyntax, RC<RawSyntax>, OwnedString > {
153
+ : private llvm::TrailingObjects<RawSyntax, RC<RawSyntax>> {
154
154
friend TrailingObjects;
155
155
156
156
// / The ID that shall be used for the next node that is created and does not
@@ -160,15 +160,17 @@ class RawSyntax final
160
160
// / An ID of this node that is stable across incremental parses
161
161
SyntaxNodeId NodeId;
162
162
163
+ mutable std::atomic<int > RefCount;
164
+
163
165
// / If this node was allocated using a \c SyntaxArena's bump allocator, a
164
166
// / reference to the arena to keep the underlying memory buffer of this node
165
167
// / alive. If this is a \c nullptr, the node owns its own memory buffer.
166
168
RC<SyntaxArena> Arena;
167
169
168
170
union {
169
171
struct {
170
- // FIXME: Reduce TextLength to 30 bits so that common fits in 4 bytes?
171
- // / Number of bytes this node takes up spelled out in the source code
172
+ // / Number of bytes this node takes up spelled out in the source code.
173
+ // / Always 0 if the node is missing.
172
174
unsigned TextLength : 32 ;
173
175
// / Whether this piece of syntax was actually present in the source.
174
176
unsigned Presence : 1 ;
@@ -196,18 +198,19 @@ class RawSyntax final
196
198
" Only 64 bits reserved for standard syntax bits" );
197
199
uint64_t : bitmax(NumRawSyntaxBits, 64 ); // align to 16 bits
198
200
// / The kind of token this "token" node represents.
201
+ const char *LeadingTrivia;
202
+ const char *TokenText;
203
+ const char *TrailingTrivia;
204
+ unsigned LeadingTriviaLength : 32 ;
205
+ unsigned TokenLength : 32 ;
206
+ unsigned TrailingTriviaLength : 32 ;
199
207
unsigned TokenKind : 16 ;
200
- StringRef LeadingTrivia;
201
- StringRef TrailingTrivia;
202
208
} Token;
203
209
} Bits;
204
210
205
211
size_t numTrailingObjects (OverloadToken<RC<RawSyntax>>) const {
206
212
return isToken () ? 0 : Bits.Layout .NumChildren ;
207
213
}
208
- size_t numTrailingObjects (OverloadToken<OwnedString>) const {
209
- return isToken () ? 1 : 0 ;
210
- }
211
214
212
215
// / Constructor for creating layout nodes.
213
216
// / If the node has been allocated inside the bump allocator of a
@@ -223,7 +226,7 @@ class RawSyntax final
223
226
// / underlying storage.
224
227
// / If \p NodeId is \c None, the next free NodeId is used, if it is passed,
225
228
// / the caller needs to assure that the NodeId has not been used yet.
226
- RawSyntax (tok TokKind, OwnedString Text, size_t TextLength,
229
+ RawSyntax (tok TokKind, StringRef Text, size_t TextLength,
227
230
StringRef LeadingTrivia, StringRef TrailingTrivia,
228
231
SourcePresence Presence, const RC<SyntaxArena> &Arena,
229
232
llvm::Optional<SyntaxNodeId> NodeId);
@@ -240,8 +243,6 @@ class RawSyntax final
240
243
return TextLength;
241
244
}
242
245
243
- mutable std::atomic<int > RefCount;
244
-
245
246
public:
246
247
~RawSyntax ();
247
248
@@ -286,15 +287,15 @@ class RawSyntax final
286
287
}
287
288
288
289
// / Make a raw "token" syntax node.
289
- static RC<RawSyntax> make (tok TokKind, OwnedString Text, size_t TextLength,
290
+ static RC<RawSyntax> make (tok TokKind, StringRef Text, size_t TextLength,
290
291
StringRef LeadingTrivia, StringRef TrailingTrivia,
291
292
SourcePresence Presence,
292
293
const RC<SyntaxArena> &Arena = SyntaxArena::make(),
293
294
llvm::Optional<SyntaxNodeId> NodeId = llvm::None);
294
295
295
296
// / Make a raw "token" syntax node that was allocated in \p Arena.
296
297
static RC<RawSyntax>
297
- makeAndCalcLength (tok TokKind, OwnedString Text, StringRef LeadingTrivia,
298
+ makeAndCalcLength (tok TokKind, StringRef Text, StringRef LeadingTrivia,
298
299
StringRef TrailingTrivia, SourcePresence Presence,
299
300
const RC<SyntaxArena> &Arena = SyntaxArena::make(),
300
301
llvm::Optional<SyntaxNodeId> NodeId = llvm::None) {
@@ -316,7 +317,7 @@ class RawSyntax final
316
317
317
318
// / Make a missing raw "token" syntax node.
318
319
static RC<RawSyntax>
319
- missing (tok TokKind, OwnedString Text,
320
+ missing (tok TokKind, StringRef Text,
320
321
const RC<SyntaxArena> &Arena = SyntaxArena::make()) {
321
322
return make (TokKind, Text, /* TextLength=*/ 0 , {}, {},
322
323
SourcePresence::Missing, Arena);
@@ -390,27 +391,24 @@ class RawSyntax final
390
391
return static_cast <tok>(Bits.Token .TokenKind );
391
392
}
392
393
393
- // / Return the text of the token as an \c OwnedString. Keeping a reference to
394
- // / this string will keep it alive even if the syntax node gets freed.
395
- OwnedString getOwnedTokenText () const {
396
- assert (isToken ());
397
- return *getTrailingObjects<OwnedString>();
398
- }
399
-
400
394
// / Return the text of the token as a reference. The referenced buffer may
401
395
// / disappear when the syntax node gets freed.
402
- StringRef getTokenText () const { return getOwnedTokenText ().str (); }
396
+ StringRef getTokenText () const {
397
+ assert (isToken ());
398
+ return StringRef (Bits.Token .TokenText , Bits.Token .TokenLength );
399
+ }
403
400
404
401
// / Return the unparsed leading trivia of the token.
405
402
StringRef getLeadingTrivia () const {
406
403
assert (isToken ());
407
- return Bits.Token .LeadingTrivia ;
404
+ return StringRef ( Bits.Token .LeadingTrivia , Bits. Token . LeadingTriviaLength ) ;
408
405
}
409
406
410
407
// / Return the unparsed trailing trivia of the token.
411
408
StringRef getTrailingTrivia () const {
412
409
assert (isToken ());
413
- return Bits.Token .TrailingTrivia ;
410
+ return StringRef (Bits.Token .TrailingTrivia ,
411
+ Bits.Token .TrailingTriviaLength );
414
412
}
415
413
416
414
// / Return pieces that make up the leading trivia of the token.
@@ -434,17 +432,15 @@ class RawSyntax final
434
432
// / Return a new token like this one, but with the given leading
435
433
// / trivia instead.
436
434
RC<RawSyntax> withLeadingTrivia (StringRef NewLeadingTrivia) const {
437
- return makeAndCalcLength (getTokenKind (), getOwnedTokenText (),
438
- NewLeadingTrivia, getTrailingTrivia (),
439
- getPresence ());
435
+ return makeAndCalcLength (getTokenKind (), getTokenText (), NewLeadingTrivia,
436
+ getTrailingTrivia (), getPresence ());
440
437
}
441
438
442
439
// / Return a new token like this one, but with the given trailing
443
440
// / trivia instead.
444
441
RC<RawSyntax> withTrailingTrivia (StringRef NewTrailingTrivia) const {
445
- return makeAndCalcLength (getTokenKind (), getOwnedTokenText (),
446
- getLeadingTrivia (), NewTrailingTrivia,
447
- getPresence ());
442
+ return makeAndCalcLength (getTokenKind (), getTokenText (), getLeadingTrivia (),
443
+ NewTrailingTrivia, getPresence ());
448
444
}
449
445
450
446
// / @}
@@ -503,7 +499,7 @@ class RawSyntax final
503
499
// / Dump this piece of syntax recursively.
504
500
void dump (llvm::raw_ostream &OS, unsigned Indent = 0 ) const ;
505
501
506
- static void Profile (llvm::FoldingSetNodeID &ID, tok TokKind, OwnedString Text,
502
+ static void Profile (llvm::FoldingSetNodeID &ID, tok TokKind, StringRef Text,
507
503
StringRef LeadingTrivia, StringRef TrailingTrivia);
508
504
};
509
505
0 commit comments