• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/sksl/SkSLDehydrator.h"
9 
10 #include <map>
11 
12 #include "include/private/SkSLProgramElement.h"
13 #include "include/private/SkSLStatement.h"
14 #include "include/private/SkSLSymbol.h"
15 #include "src/sksl/SkSLRehydrator.h"
16 #include "src/sksl/ir/SkSLBinaryExpression.h"
17 #include "src/sksl/ir/SkSLBreakStatement.h"
18 #include "src/sksl/ir/SkSLConstructor.h"
19 #include "src/sksl/ir/SkSLConstructorArray.h"
20 #include "src/sksl/ir/SkSLConstructorCompound.h"
21 #include "src/sksl/ir/SkSLConstructorCompoundCast.h"
22 #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
23 #include "src/sksl/ir/SkSLConstructorMatrixResize.h"
24 #include "src/sksl/ir/SkSLConstructorScalarCast.h"
25 #include "src/sksl/ir/SkSLConstructorSplat.h"
26 #include "src/sksl/ir/SkSLConstructorStruct.h"
27 #include "src/sksl/ir/SkSLContinueStatement.h"
28 #include "src/sksl/ir/SkSLDiscardStatement.h"
29 #include "src/sksl/ir/SkSLDoStatement.h"
30 #include "src/sksl/ir/SkSLEnum.h"
31 #include "src/sksl/ir/SkSLExpressionStatement.h"
32 #include "src/sksl/ir/SkSLField.h"
33 #include "src/sksl/ir/SkSLFieldAccess.h"
34 #include "src/sksl/ir/SkSLForStatement.h"
35 #include "src/sksl/ir/SkSLFunctionCall.h"
36 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
37 #include "src/sksl/ir/SkSLFunctionDefinition.h"
38 #include "src/sksl/ir/SkSLIfStatement.h"
39 #include "src/sksl/ir/SkSLIndexExpression.h"
40 #include "src/sksl/ir/SkSLInlineMarker.h"
41 #include "src/sksl/ir/SkSLIntLiteral.h"
42 #include "src/sksl/ir/SkSLInterfaceBlock.h"
43 #include "src/sksl/ir/SkSLPostfixExpression.h"
44 #include "src/sksl/ir/SkSLPrefixExpression.h"
45 #include "src/sksl/ir/SkSLReturnStatement.h"
46 #include "src/sksl/ir/SkSLSetting.h"
47 #include "src/sksl/ir/SkSLStructDefinition.h"
48 #include "src/sksl/ir/SkSLSwitchCase.h"
49 #include "src/sksl/ir/SkSLSwitchStatement.h"
50 #include "src/sksl/ir/SkSLSwizzle.h"
51 #include "src/sksl/ir/SkSLSymbolAlias.h"
52 #include "src/sksl/ir/SkSLSymbolTable.h"
53 #include "src/sksl/ir/SkSLTernaryExpression.h"
54 #include "src/sksl/ir/SkSLUnresolvedFunction.h"
55 #include "src/sksl/ir/SkSLVarDeclarations.h"
56 #include "src/sksl/ir/SkSLVariable.h"
57 
58 #ifdef SKSL_STANDALONE
59 
60 namespace SkSL {
61 
62 static constexpr int HEADER_SIZE = 2;
63 
64 class AutoDehydratorSymbolTable {
65 public:
AutoDehydratorSymbolTable(Dehydrator * dehydrator,const std::shared_ptr<SymbolTable> & symbols)66     AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
67         : fDehydrator(dehydrator) {
68         dehydrator->fSymbolMap.emplace_back();
69         if (symbols) {
70             dehydrator->write(*symbols);
71         } else {
72             dehydrator->writeCommand(Rehydrator::kVoid_Command);
73         }
74     }
75 
~AutoDehydratorSymbolTable()76     ~AutoDehydratorSymbolTable() {
77         fDehydrator->fSymbolMap.pop_back();
78     }
79 
80 private:
81     Dehydrator* fDehydrator;
82 };
83 
write(Layout l)84 void Dehydrator::write(Layout l) {
85     if (l == Layout()) {
86         this->writeCommand(Rehydrator::kDefaultLayout_Command);
87     } else if (l == Layout::builtin(l.fBuiltin)) {
88         this->writeCommand(Rehydrator::kBuiltinLayout_Command);
89         this->writeS16(l.fBuiltin);
90     } else {
91         this->writeCommand(Rehydrator::kLayout_Command);
92         fBody.write32(l.fFlags);
93         this->writeS8(l.fLocation);
94         this->writeS8(l.fOffset);
95         this->writeS8(l.fBinding);
96         this->writeS8(l.fIndex);
97         this->writeS8(l.fSet);
98         this->writeS16(l.fBuiltin);
99         this->writeS8(l.fInputAttachmentIndex);
100         this->writeS8(l.fPrimitive);
101         this->writeS8(l.fMaxVertices);
102         this->writeS8(l.fInvocations);
103         this->write(l.fWhen);
104         this->writeS8((int) l.fCType);
105     }
106 }
107 
write(Modifiers m)108 void Dehydrator::write(Modifiers m) {
109     if (m == Modifiers()) {
110         this->writeCommand(Rehydrator::kDefaultModifiers_Command);
111     } else {
112         if (m.fFlags <= 255) {
113             this->writeCommand(Rehydrator::kModifiers8Bit_Command);
114             this->write(m.fLayout);
115             this->writeU8(m.fFlags);
116         } else {
117             this->writeCommand(Rehydrator::kModifiers_Command);
118             this->write(m.fLayout);
119             this->writeS32(m.fFlags);
120         }
121     }
122 }
123 
write(StringFragment s)124 void Dehydrator::write(StringFragment s) {
125     this->write(String(s));
126 }
127 
write(String s)128 void Dehydrator::write(String s) {
129     auto found = fStrings.find(s);
130     int offset;
131     if (found == fStrings.end()) {
132         offset = fStringBuffer.str().length() + HEADER_SIZE;
133         fStrings.insert({ s, offset });
134         SkASSERT(s.length() <= 255);
135         fStringBreaks.add(fStringBuffer.bytesWritten());
136         fStringBuffer.write8(s.length());
137         fStringBuffer.writeString(s);
138     } else {
139         offset = found->second;
140     }
141     this->writeU16(offset);
142 }
143 
write(const Symbol & s)144 void Dehydrator::write(const Symbol& s) {
145     uint16_t id = this->symbolId(&s, false);
146     if (id) {
147         this->writeCommand(Rehydrator::kSymbolRef_Command);
148         this->writeU16(id);
149         return;
150     }
151     switch (s.kind()) {
152         case Symbol::Kind::kFunctionDeclaration: {
153             const FunctionDeclaration& f = s.as<FunctionDeclaration>();
154             this->writeCommand(Rehydrator::kFunctionDeclaration_Command);
155             this->writeId(&f);
156             this->write(f.modifiers());
157             this->write(f.name());
158             this->writeU8(f.parameters().size());
159             for (const Variable* p : f.parameters()) {
160                 this->writeU16(this->symbolId(p));
161             }
162             this->write(f.returnType());
163             break;
164         }
165         case Symbol::Kind::kSymbolAlias: {
166             const SymbolAlias& alias = s.as<SymbolAlias>();
167             this->writeCommand(Rehydrator::kSymbolAlias_Command);
168             this->writeId(&alias);
169             this->write(alias.name());
170             this->write(*alias.origSymbol());
171             break;
172         }
173         case Symbol::Kind::kUnresolvedFunction: {
174             const UnresolvedFunction& f = s.as<UnresolvedFunction>();
175             this->writeCommand(Rehydrator::kUnresolvedFunction_Command);
176             this->writeId(&f);
177             this->writeU8(f.functions().size());
178             for (const FunctionDeclaration* funcDecl : f.functions()) {
179                 this->write(*funcDecl);
180             }
181             break;
182         }
183         case Symbol::Kind::kType: {
184             const Type& t = s.as<Type>();
185             switch (t.typeKind()) {
186                 case Type::TypeKind::kArray:
187                     this->writeCommand(Rehydrator::kArrayType_Command);
188                     this->writeId(&t);
189                     this->write(t.componentType());
190                     this->writeS8(t.columns());
191                     break;
192                 case Type::TypeKind::kEnum:
193                     this->writeCommand(Rehydrator::kEnumType_Command);
194                     this->writeId(&t);
195                     this->write(t.name());
196                     break;
197                 case Type::TypeKind::kStruct:
198                     this->writeCommand(Rehydrator::kStructType_Command);
199                     this->writeId(&t);
200                     this->write(t.name());
201                     this->writeU8(t.fields().size());
202                     for (const Type::Field& f : t.fields()) {
203                         this->write(f.fModifiers);
204                         this->write(f.fName);
205                         this->write(*f.fType);
206                     }
207                     break;
208                 default:
209                     this->writeCommand(Rehydrator::kSystemType_Command);
210                     this->writeId(&t);
211                     this->write(t.name());
212             }
213             break;
214         }
215         case Symbol::Kind::kVariable: {
216             const Variable& v = s.as<Variable>();
217             this->writeCommand(Rehydrator::kVariable_Command);
218             this->writeId(&v);
219             this->write(v.modifiers());
220             this->write(v.name());
221             this->write(v.type());
222             this->writeU8((int8_t) v.storage());
223             break;
224         }
225         case Symbol::Kind::kField: {
226             const Field& f = s.as<Field>();
227             this->writeCommand(Rehydrator::kField_Command);
228             this->writeU16(this->symbolId(&f.owner()));
229             this->writeU8(f.fieldIndex());
230             break;
231         }
232         case Symbol::Kind::kExternal:
233             SkASSERT(false);
234             break;
235     }
236 }
237 
write(const SymbolTable & symbols)238 void Dehydrator::write(const SymbolTable& symbols) {
239     this->writeCommand(Rehydrator::kSymbolTable_Command);
240     this->writeU16(symbols.fOwnedSymbols.size());
241     for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
242         this->write(*s);
243     }
244     this->writeU16(symbols.fSymbols.count());
245     std::map<StringFragment, const Symbol*> ordered;
246     symbols.foreach([&](StringFragment name, const Symbol* symbol) {
247         ordered.insert({name, symbol});
248     });
249     for (std::pair<StringFragment, const Symbol*> p : ordered) {
250         bool found = false;
251         for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
252             if (symbols.fOwnedSymbols[i].get() == p.second) {
253                 fCommandBreaks.add(fBody.bytesWritten());
254                 this->writeU16(i);
255                 found = true;
256                 break;
257             }
258         }
259         SkASSERT(found);
260     }
261 }
262 
writeExpressionSpan(const SkSpan<const std::unique_ptr<Expression>> & span)263 void Dehydrator::writeExpressionSpan(const SkSpan<const std::unique_ptr<Expression>>& span) {
264     this->writeU8(span.size());
265     for (const auto& expr : span) {
266         this->write(expr.get());
267     }
268 }
269 
write(const Expression * e)270 void Dehydrator::write(const Expression* e) {
271     if (e) {
272         switch (e->kind()) {
273             case Expression::Kind::kBinary: {
274                 const BinaryExpression& b = e->as<BinaryExpression>();
275                 this->writeCommand(Rehydrator::kBinary_Command);
276                 this->write(b.left().get());
277                 this->writeU8((int) b.getOperator().kind());
278                 this->write(b.right().get());
279                 break;
280             }
281             case Expression::Kind::kBoolLiteral: {
282                 const BoolLiteral& b = e->as<BoolLiteral>();
283                 this->writeCommand(Rehydrator::kBoolLiteral_Command);
284                 this->writeU8(b.value());
285                 break;
286             }
287             case Expression::Kind::kCodeString:
288                 SkDEBUGFAIL("shouldn't be able to receive kCodeString here");
289                 break;
290 
291             case Expression::Kind::kConstructorArray:
292                 this->writeCommand(Rehydrator::kConstructorArray_Command);
293                 this->write(e->type());
294                 this->writeExpressionSpan(e->as<ConstructorArray>().argumentSpan());
295                 break;
296 
297             case Expression::Kind::kConstructorCompound:
298                 this->writeCommand(Rehydrator::kConstructorCompound_Command);
299                 this->write(e->type());
300                 this->writeExpressionSpan(e->as<ConstructorCompound>().argumentSpan());
301                 break;
302 
303             case Expression::Kind::kConstructorCompoundCast:
304                 this->writeCommand(Rehydrator::kConstructorCompoundCast_Command);
305                 this->write(e->type());
306                 this->writeExpressionSpan(e->as<ConstructorCompoundCast>().argumentSpan());
307                 break;
308 
309             case Expression::Kind::kConstructorDiagonalMatrix:
310                 this->writeCommand(Rehydrator::kConstructorDiagonalMatrix_Command);
311                 this->write(e->type());
312                 this->writeExpressionSpan(e->as<ConstructorDiagonalMatrix>().argumentSpan());
313                 break;
314 
315             case Expression::Kind::kConstructorMatrixResize:
316                 this->writeCommand(Rehydrator::kConstructorMatrixResize_Command);
317                 this->write(e->type());
318                 this->writeExpressionSpan(e->as<ConstructorMatrixResize>().argumentSpan());
319                 break;
320 
321             case Expression::Kind::kConstructorScalarCast:
322                 this->writeCommand(Rehydrator::kConstructorScalarCast_Command);
323                 this->write(e->type());
324                 this->writeExpressionSpan(e->as<ConstructorScalarCast>().argumentSpan());
325                 break;
326 
327             case Expression::Kind::kConstructorSplat:
328                 this->writeCommand(Rehydrator::kConstructorSplat_Command);
329                 this->write(e->type());
330                 this->writeExpressionSpan(e->as<ConstructorSplat>().argumentSpan());
331                 break;
332 
333             case Expression::Kind::kConstructorStruct:
334                 this->writeCommand(Rehydrator::kConstructorStruct_Command);
335                 this->write(e->type());
336                 this->writeExpressionSpan(e->as<ConstructorStruct>().argumentSpan());
337                 break;
338 
339             case Expression::Kind::kExternalFunctionCall:
340             case Expression::Kind::kExternalFunctionReference:
341                 SkDEBUGFAIL("unimplemented--not expected to be used from within an include file");
342                 break;
343 
344             case Expression::Kind::kFieldAccess: {
345                 const FieldAccess& f = e->as<FieldAccess>();
346                 this->writeCommand(Rehydrator::kFieldAccess_Command);
347                 this->write(f.base().get());
348                 this->writeU8(f.fieldIndex());
349                 this->writeU8((int8_t) f.ownerKind());
350                 break;
351             }
352             case Expression::Kind::kFloatLiteral: {
353                 const FloatLiteral& f = e->as<FloatLiteral>();
354                 this->writeCommand(Rehydrator::kFloatLiteral_Command);
355                 this->write(f.type());
356                 FloatIntUnion u;
357                 u.fFloat = f.value();
358                 this->writeS32(u.fInt);
359                 break;
360             }
361             case Expression::Kind::kFunctionCall: {
362                 const FunctionCall& f = e->as<FunctionCall>();
363                 this->writeCommand(Rehydrator::kFunctionCall_Command);
364                 this->write(f.type());
365                 this->writeId(&f.function());
366                 this->writeU8(f.arguments().size());
367                 for (const auto& a : f.arguments()) {
368                     this->write(a.get());
369                 }
370                 break;
371             }
372             case Expression::Kind::kIndex: {
373                 const IndexExpression& i = e->as<IndexExpression>();
374                 this->writeCommand(Rehydrator::kIndex_Command);
375                 this->write(i.base().get());
376                 this->write(i.index().get());
377                 break;
378             }
379             case Expression::Kind::kIntLiteral: {
380                 const IntLiteral& i = e->as<IntLiteral>();
381                 this->writeCommand(Rehydrator::kIntLiteral_Command);
382                 this->write(i.type());
383                 this->writeS32(i.value());
384                 break;
385             }
386             case Expression::Kind::kPostfix: {
387                 const PostfixExpression& p = e->as<PostfixExpression>();
388                 this->writeCommand(Rehydrator::kPostfix_Command);
389                 this->writeU8((int) p.getOperator().kind());
390                 this->write(p.operand().get());
391                 break;
392             }
393             case Expression::Kind::kPrefix: {
394                 const PrefixExpression& p = e->as<PrefixExpression>();
395                 this->writeCommand(Rehydrator::kPrefix_Command);
396                 this->writeU8((int) p.getOperator().kind());
397                 this->write(p.operand().get());
398                 break;
399             }
400             case Expression::Kind::kSetting: {
401                 const Setting& s = e->as<Setting>();
402                 this->writeCommand(Rehydrator::kSetting_Command);
403                 this->write(s.name());
404                 break;
405             }
406             case Expression::Kind::kSwizzle: {
407                 const Swizzle& s = e->as<Swizzle>();
408                 this->writeCommand(Rehydrator::kSwizzle_Command);
409                 this->write(s.base().get());
410                 this->writeU8(s.components().size());
411                 for (int c : s.components()) {
412                     this->writeU8(c);
413                 }
414                 break;
415             }
416             case Expression::Kind::kTernary: {
417                 const TernaryExpression& t = e->as<TernaryExpression>();
418                 this->writeCommand(Rehydrator::kTernary_Command);
419                 this->write(t.test().get());
420                 this->write(t.ifTrue().get());
421                 this->write(t.ifFalse().get());
422                 break;
423             }
424             case Expression::Kind::kVariableReference: {
425                 const VariableReference& v = e->as<VariableReference>();
426                 this->writeCommand(Rehydrator::kVariableReference_Command);
427                 this->writeId(v.variable());
428                 this->writeU8((int8_t) v.refKind());
429                 break;
430             }
431             case Expression::Kind::kFunctionReference:
432             case Expression::Kind::kTypeReference:
433                 SkDEBUGFAIL("this expression shouldn't appear in finished code");
434                 break;
435         }
436     } else {
437         this->writeCommand(Rehydrator::kVoid_Command);
438     }
439 }
440 
write(const Statement * s)441 void Dehydrator::write(const Statement* s) {
442     if (s) {
443         switch (s->kind()) {
444             case Statement::Kind::kBlock: {
445                 const Block& b = s->as<Block>();
446                 this->writeCommand(Rehydrator::kBlock_Command);
447                 AutoDehydratorSymbolTable symbols(this, b.symbolTable());
448                 this->writeU8(b.children().size());
449                 for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
450                     this->write(blockStmt.get());
451                 }
452                 this->writeU8(b.isScope());
453                 break;
454             }
455             case Statement::Kind::kBreak:
456                 this->writeCommand(Rehydrator::kBreak_Command);
457                 break;
458             case Statement::Kind::kContinue:
459                 this->writeCommand(Rehydrator::kContinue_Command);
460                 break;
461             case Statement::Kind::kDiscard:
462                 this->writeCommand(Rehydrator::kDiscard_Command);
463                 break;
464             case Statement::Kind::kDo: {
465                 const DoStatement& d = s->as<DoStatement>();
466                 this->writeCommand(Rehydrator::kDo_Command);
467                 this->write(d.statement().get());
468                 this->write(d.test().get());
469                 break;
470             }
471             case Statement::Kind::kExpression: {
472                 const ExpressionStatement& e = s->as<ExpressionStatement>();
473                 this->writeCommand(Rehydrator::kExpressionStatement_Command);
474                 this->write(e.expression().get());
475                 break;
476             }
477             case Statement::Kind::kFor: {
478                 const ForStatement& f = s->as<ForStatement>();
479                 this->writeCommand(Rehydrator::kFor_Command);
480                 this->write(f.initializer().get());
481                 this->write(f.test().get());
482                 this->write(f.next().get());
483                 this->write(f.statement().get());
484                 this->write(*f.symbols());
485                 break;
486             }
487             case Statement::Kind::kIf: {
488                 const IfStatement& i = s->as<IfStatement>();
489                 this->writeCommand(Rehydrator::kIf_Command);
490                 this->writeU8(i.isStatic());
491                 this->write(i.test().get());
492                 this->write(i.ifTrue().get());
493                 this->write(i.ifFalse().get());
494                 break;
495             }
496             case Statement::Kind::kInlineMarker: {
497                 const InlineMarker& i = s->as<InlineMarker>();
498                 this->writeCommand(Rehydrator::kInlineMarker_Command);
499                 this->writeId(&i.function());
500                 break;
501             }
502             case Statement::Kind::kNop:
503                 SkDEBUGFAIL("unexpected--nop statement in finished code");
504                 break;
505             case Statement::Kind::kReturn: {
506                 const ReturnStatement& r = s->as<ReturnStatement>();
507                 this->writeCommand(Rehydrator::kReturn_Command);
508                 this->write(r.expression().get());
509                 break;
510             }
511             case Statement::Kind::kSwitch: {
512                 const SwitchStatement& ss = s->as<SwitchStatement>();
513                 this->writeCommand(Rehydrator::kSwitch_Command);
514                 this->writeU8(ss.isStatic());
515                 AutoDehydratorSymbolTable symbols(this, ss.symbols());
516                 this->write(ss.value().get());
517                 this->writeU8(ss.cases().size());
518                 for (const std::unique_ptr<Statement>& stmt : ss.cases()) {
519                     const SwitchCase& sc = stmt->as<SwitchCase>();
520                     this->write(sc.value().get());
521                     this->write(sc.statement().get());
522                 }
523                 break;
524             }
525             case Statement::Kind::kSwitchCase:
526                 SkDEBUGFAIL("SwitchCase statements shouldn't appear here");
527                 break;
528             case Statement::Kind::kVarDeclaration: {
529                 const VarDeclaration& v = s->as<VarDeclaration>();
530                 this->writeCommand(Rehydrator::kVarDeclaration_Command);
531                 this->writeU16(this->symbolId(&v.var()));
532                 this->write(v.baseType());
533                 this->writeS8(v.arraySize());
534                 this->write(v.value().get());
535                 break;
536             }
537         }
538     } else {
539         this->writeCommand(Rehydrator::kVoid_Command);
540     }
541 }
542 
write(const ProgramElement & e)543 void Dehydrator::write(const ProgramElement& e) {
544     switch (e.kind()) {
545         case ProgramElement::Kind::kEnum: {
546             const Enum& en = e.as<Enum>();
547             this->writeCommand(Rehydrator::kEnum_Command);
548             this->write(en.typeName());
549             AutoDehydratorSymbolTable symbols(this, en.symbols());
550             for (const std::unique_ptr<const Symbol>& s : en.symbols()->fOwnedSymbols) {
551                 const Variable& v = s->as<Variable>();
552                 SkASSERT(v.initialValue());
553                 const IntLiteral& i = v.initialValue()->as<IntLiteral>();
554                 this->writeS32(i.value());
555             }
556             break;
557         }
558         case ProgramElement::Kind::kExtension:
559             SkASSERT(false);
560             break;
561         case ProgramElement::Kind::kFunction: {
562             const FunctionDefinition& f = e.as<FunctionDefinition>();
563             this->writeCommand(Rehydrator::kFunctionDefinition_Command);
564             this->writeU16(this->symbolId(&f.declaration()));
565             this->write(f.body().get());
566             this->writeU8(f.referencedIntrinsics().size());
567             std::set<uint16_t> ordered;
568             for (const FunctionDeclaration* ref : f.referencedIntrinsics()) {
569                 ordered.insert(this->symbolId(ref));
570             }
571             for (uint16_t ref : ordered) {
572                 this->writeU16(ref);
573             }
574             break;
575         }
576         case ProgramElement::Kind::kFunctionPrototype: {
577             // We don't need to emit function prototypes into the dehydrated data, because we don't
578             // ever need to re-emit the intrinsics files as raw GLSL/Metal. As long as the symbols
579             // exist in the symbol table, we're in good shape.
580             break;
581         }
582         case ProgramElement::Kind::kInterfaceBlock: {
583             const InterfaceBlock& i = e.as<InterfaceBlock>();
584             this->writeCommand(Rehydrator::kInterfaceBlock_Command);
585             this->write(i.variable());
586             this->write(i.typeName());
587             this->write(i.instanceName());
588             this->writeS8(i.arraySize());
589             break;
590         }
591         case ProgramElement::Kind::kModifiers:
592             SkASSERT(false);
593             break;
594         case ProgramElement::Kind::kSection:
595             SkASSERT(false);
596             break;
597         case ProgramElement::Kind::kStructDefinition: {
598             const StructDefinition& structDef = e.as<StructDefinition>();
599             this->writeCommand(Rehydrator::kStructDefinition_Command);
600             this->write(structDef.type());
601             break;
602         }
603         case ProgramElement::Kind::kGlobalVar: {
604             const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>();
605             this->writeCommand(Rehydrator::kVarDeclarations_Command);
606             this->write(v.declaration().get());
607             break;
608         }
609     }
610 }
611 
write(const std::vector<std::unique_ptr<ProgramElement>> & elements)612 void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
613     this->writeCommand(Rehydrator::kElements_Command);
614     for (const auto& e : elements) {
615         this->write(*e);
616     }
617     this->writeCommand(Rehydrator::kElementsComplete_Command);
618 }
619 
finish(OutputStream & out)620 void Dehydrator::finish(OutputStream& out) {
621     String stringBuffer = fStringBuffer.str();
622     String commandBuffer = fBody.str();
623 
624     out.write16(fStringBuffer.str().size());
625     fStringBufferStart = 2;
626     out.writeString(stringBuffer);
627     fCommandStart = fStringBufferStart + stringBuffer.size();
628     out.writeString(commandBuffer);
629 }
630 
prefixAtOffset(size_t byte)631 const char* Dehydrator::prefixAtOffset(size_t byte) {
632     if (byte >= fCommandStart) {
633         return fCommandBreaks.contains(byte - fCommandStart) ? "\n" : "";
634     }
635     if (byte >= fStringBufferStart) {
636         return fStringBreaks.contains(byte - fStringBufferStart) ? "\n" : "";
637     }
638     return "";
639 }
640 
641 } // namespace
642 
643 #endif
644