MCPcopy Create free account
hub / github.com/compilets/compilets / createTraceMethod

Function createTraceMethod

src/cpp-syntax-utils.ts:36–68  ·  view source on GitHub ↗
(type: Type, members: ClassElement[])

Source from the content-addressed store, hash-verified

34 * Create the Trace method required by cppgc::GarbageCollected.
35 */
36export function createTraceMethod(type: Type, members: ClassElement[]): MethodDeclaration | undefined {
37 // Collect all the GCed members from the class.
38 const body = new Block();
39 for (const member of members) {
40 if (member instanceof PropertyDeclaration) {
41 let traceMethod: string | undefined;
42 if (member.type.hasObject())
43 traceMethod = 'compilets::TraceMember';
44 else if (member.type.hasTemplate())
45 traceMethod = 'compilets::TracePossibleMember';
46 if (traceMethod) {
47 body.statements.push(
48 new ExpressionStatement(
49 new RawExpression(Type.createVoidType(),
50 `${traceMethod}(visitor, ${member.name})`)));
51 }
52 }
53 }
54 if (body.statements.length == 0)
55 return;
56 if (type.base) {
57 body.statements.push(
58 new ExpressionStatement(
59 new RawExpression(new Type('void', 'void'),
60 `${type.base.name}::Trace(visitor)`)));
61 }
62 // Create the visitor parameter.
63 const visitorType = new Type('cppgc::Visitor*', 'external');
64 const visitor = new ParameterDeclaration('visitor', visitorType);
65 // Create the method.
66 const methodType = new FunctionType('method', Type.createVoidType(), [ visitorType ]);
67 return new MethodDeclaration(methodType, 'Trace', [ 'public', 'override', 'const' ], [ visitor ], body);
68}
69
70/**
71 * Convert the expression of source type to target type if necessary.

Callers 2

constructorMethod · 0.90
printDeclarationMethod · 0.90

Calls 4

hasObjectMethod · 0.80
hasTemplateMethod · 0.80
pushMethod · 0.80
createVoidTypeMethod · 0.80

Tested by

no test coverage detected