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

Function castArguments

src/cpp-syntax-utils.ts:173–195  ·  view source on GitHub ↗
(args: Expression[], parameters: Type[])

Source from the content-addressed store, hash-verified

171 * Convert args to parameter types.
172 */
173export function castArguments(args: Expression[], parameters: Type[]) {
174 for (let i = 0; i < args.length; ++i) {
175 let param: Type;
176 if (i > parameters.length - 1) {
177 if (!parameters[parameters.length - 1].isVariadic)
178 throw new Error('More arguments passed than the function can take.');
179 param = parameters[parameters.length - 1];
180 } else {
181 param = parameters[i];
182 }
183 if (param.isVariadic && !param.isExternal) {
184 // When meet a rest parameter, put remaining args in an array.
185 const callArgs = args.slice(i).map(a => castExpression(a, param.getElementType()));
186 args[i] = new CustomExpression(param, (ctx) => {
187 return `compilets::MakeArray<${param.getElementType().print(ctx)}>({${callArgs.map(a => a.print(ctx)).join(', ')}})`;
188 });
189 return args.slice(0, i + 1);
190 }
191 args[i] = castExpression(args[i], param.isVariadic ? param.getElementType()
192 : param);
193 }
194 return args;
195}
196
197/**
198 * Conversions involving unions.

Callers 1

constructorMethod · 0.90

Calls 5

castExpressionFunction · 0.85
sliceMethod · 0.80
getElementTypeMethod · 0.80
printMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected