Skip to content

Commit ee8b5b4

Browse files
committed
Cleanup ast callables
1 parent 9141751 commit ee8b5b4

18 files changed

+143
-158
lines changed

src/ast_callable.hpp

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Sass {
1818
/////////////////////////////////////////////////////////////////////////
1919

2020
typedef Value* (*SassFnSig)(FN_PROTOTYPE2);
21-
typedef std::pair<ArgumentDeclarationObj, SassFnSig> SassFnPair;
21+
typedef std::pair<CallableSignatureObj, SassFnSig> SassFnPair;
2222
typedef sass::vector<SassFnPair> SassFnPairs;
2323

2424
/////////////////////////////////////////////////////////////////////////
@@ -31,10 +31,13 @@ namespace Sass {
3131
// Value constructor
3232
Callable(const SourceSpan& pstate);
3333

34-
// The main entry point to execute the function (implemented in each specialization)
35-
virtual Value* execute(Eval& eval, ArgumentInvocation* arguments, const SourceSpan& pstate) = 0;
34+
// The main entry point to execute the function
35+
// Must be implemented in each specialization
36+
virtual Value* execute(Eval& eval,
37+
CallableArguments* arguments,
38+
const SourceSpan& pstate) = 0;
3639

37-
// Return the function name
40+
// Return name of this callable/function
3841
virtual const sass::string& name() const = 0;
3942

4043
// Equality comparator (needed for `get-function` value)
@@ -72,9 +75,10 @@ namespace Sass {
7275
};
7376

7477
//////////////////////////////////////////////////////////////////////
78+
// Object for the function signature holding which parameters a
79+
// callable can have or expects and to which variable it's assigned.
7580
//////////////////////////////////////////////////////////////////////
76-
77-
class ArgumentDeclaration final : public AstNode
81+
class CallableSignature final : public AstNode
7882
{
7983
private:
8084

@@ -91,18 +95,18 @@ namespace Sass {
9195
public:
9296

9397
// Value constructor
94-
ArgumentDeclaration(SourceSpan&& pstate,
98+
CallableSignature(SourceSpan&& pstate,
9599
sass::vector<ArgumentObj>&& arguments = {},
96100
EnvKey&& restArg = {});
97101

98-
// Check if signature is void
102+
// Checks if signature is void
99103
bool isEmpty() const {
100104
return arguments_.empty()
101105
&& restArg_.empty();
102106
}
103107

104-
// Parse source into arguments
105-
static ArgumentDeclaration* parse(
108+
// Parse `source` into signature
109+
static CallableSignature* parse(
106110
Compiler& context, SourceData* source);
107111

108112
// Throws a [SassScriptException] if [positional] and
@@ -120,19 +124,23 @@ namespace Sass {
120124
};
121125

122126
/////////////////////////////////////////////////////////////////////////
127+
// Object for the actual function arguments to pass to the function
128+
// invocation. It must be valid in regard to the callable signature
129+
// of the invoked function (will throw an error otherwise).
123130
/////////////////////////////////////////////////////////////////////////
124-
125-
class ArgumentInvocation final : public AstNode
131+
class CallableArguments final : public AstNode
126132
{
127133
private:
128134

129135
// The arguments passed by position.
130136
ADD_REF(ExpressionVector, positional);
131137

132-
// The argument expressions passed by name.
138+
// The arguments passed by name.
133139
ADD_REF(ExpressionFlatMap, named);
134140

135-
// The first rest argument (as in `$args...`).
141+
// Optional rest argument (as in `$args...`).
142+
// Supports only one rest arg and it must be last.
143+
// ToDo: explain difference between restArg and kwdRest.
136144
ADD_CONSTREF(ExpressionObj, restArg);
137145

138146
// The second rest argument, which is expected to only contain a keyword map.
@@ -142,15 +150,15 @@ namespace Sass {
142150

143151
public:
144152

145-
// Value constructor
146-
ArgumentInvocation(const SourceSpan& pstate,
153+
// Value move constructor
154+
CallableArguments(SourceSpan&& pstate,
147155
ExpressionVector&& positional,
148156
ExpressionFlatMap&& named,
149157
Expression* restArgs = nullptr,
150158
Expression* kwdRest = nullptr);
151159

152-
// Value constructor
153-
ArgumentInvocation(SourceSpan&& pstate,
160+
// Partial value move constructor
161+
CallableArguments(const SourceSpan& pstate,
154162
ExpressionVector&& positional,
155163
ExpressionFlatMap&& named,
156164
Expression* restArgs = nullptr,
@@ -163,8 +171,10 @@ namespace Sass {
163171

164172
/////////////////////////////////////////////////////////////////////////
165173
// The result of evaluating arguments to a function or mixin.
174+
// It's basically the same as `CallableArguments` but with all
175+
// based values already evaluated in order to check compliance
176+
// with the expected callable signature.
166177
/////////////////////////////////////////////////////////////////////////
167-
168178
class ArgumentResults final {
169179

170180
// Arguments passed by position.
@@ -173,10 +183,10 @@ namespace Sass {
173183
// Arguments passed by name.
174184
// A list implementation is often more efficient
175185
// I don't expect any function to have many arguments
176-
// Normally the trade-off is around 8 items in the list
186+
// Normally trade-off starts around 8 items in the list
177187
ADD_REF(ValueFlatMap, named);
178188

179-
// The separator used for the rest argument list, if any.
189+
// Separator used for rest argument list, if any.
180190
ADD_REF(SassSeparator, separator);
181191

182192
public:
@@ -211,27 +221,6 @@ namespace Sass {
211221
/////////////////////////////////////////////////////////////////////////
212222
/////////////////////////////////////////////////////////////////////////
213223

214-
class CallableInvocation
215-
{
216-
217-
private:
218-
219-
// The arguments passed to the callable.
220-
ADD_CONSTREF(ArgumentInvocationObj, arguments);
221-
222-
public:
223-
224-
// Value constructor
225-
CallableInvocation(
226-
ArgumentInvocation* arguments) :
227-
arguments_(arguments)
228-
{}
229-
230-
};
231-
232-
/////////////////////////////////////////////////////////////////////////
233-
/////////////////////////////////////////////////////////////////////////
234-
235224
}
236225

237226
#endif

src/ast_callables.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Sass {
2222

2323
BuiltInCallable::BuiltInCallable(
2424
const EnvKey& envkey,
25-
ArgumentDeclaration* parameters,
25+
CallableSignature* parameters,
2626
const SassFnSig& callback) :
2727
Callable(SourceSpan::internal("[BUILTIN]")),
2828
envkey_(envkey),
@@ -119,7 +119,7 @@ namespace Sass {
119119

120120
ExternalCallable::ExternalCallable(
121121
const EnvKey& fname,
122-
ArgumentDeclaration* parameters,
122+
CallableSignature* parameters,
123123
SassFunctionLambda lambda) :
124124
Callable(SourceSpan::internal("[EXTERNAL]")),
125125
envkey_(fname),
@@ -156,7 +156,7 @@ namespace Sass {
156156
/////////////////////////////////////////////////////////////////////////
157157
/////////////////////////////////////////////////////////////////////////
158158

159-
ArgumentDeclaration::ArgumentDeclaration(
159+
CallableSignature::CallableSignature(
160160
SourceSpan&& pstate,
161161
sass::vector<ArgumentObj>&& arguments,
162162
EnvKey&& restArg) :
@@ -171,7 +171,7 @@ namespace Sass {
171171
}
172172

173173
// Parse source into arguments
174-
ArgumentDeclaration* ArgumentDeclaration::parse(
174+
CallableSignature* CallableSignature::parse(
175175
Compiler& context, SourceData* source)
176176
{
177177
ScssParser parser(context, source);
@@ -180,7 +180,7 @@ namespace Sass {
180180

181181
// Throws a [SassScriptException] if [positional] and
182182
// [names] aren't valid for this argument declaration.
183-
void ArgumentDeclaration::verify(
183+
void CallableSignature::verify(
184184
size_t positional,
185185
const ValueFlatMap& names,
186186
const SourceSpan& pstate,
@@ -237,7 +237,7 @@ namespace Sass {
237237

238238
// Returns whether [positional] and [names]
239239
// are valid for this argument declaration.
240-
bool ArgumentDeclaration::matches(
240+
bool CallableSignature::matches(
241241
const ArgumentResults& evaluated) const
242242
{
243243
size_t namedUsed = 0; Argument* argument;
@@ -265,7 +265,7 @@ namespace Sass {
265265
/////////////////////////////////////////////////////////////////////////
266266
/////////////////////////////////////////////////////////////////////////
267267

268-
ArgumentInvocation::ArgumentInvocation(
268+
CallableArguments::CallableArguments(
269269
const SourceSpan& pstate,
270270
ExpressionVector&& positional,
271271
ExpressionFlatMap&& named,
@@ -278,7 +278,7 @@ namespace Sass {
278278
kwdRest_(kwdRest)
279279
{}
280280

281-
ArgumentInvocation::ArgumentInvocation(
281+
CallableArguments::CallableArguments(
282282
SourceSpan&& pstate,
283283
ExpressionVector&& positional,
284284
ExpressionFlatMap&& named,
@@ -292,7 +292,7 @@ namespace Sass {
292292
{}
293293

294294
// Returns whether this invocation passes no arguments.
295-
bool ArgumentInvocation::isEmpty() const
295+
bool CallableArguments::isEmpty() const
296296
{
297297
return positional_.empty()
298298
&& named_.empty()
@@ -332,22 +332,22 @@ namespace Sass {
332332
// Implement the execute dispatch to evaluator
333333
/////////////////////////////////////////////////////////////////////////
334334

335-
Value* BuiltInCallable::execute(Eval& eval, ArgumentInvocation* arguments, const SourceSpan& pstate)
335+
Value* BuiltInCallable::execute(Eval& eval, CallableArguments* arguments, const SourceSpan& pstate)
336336
{
337337
return eval.execute(this, arguments, pstate);
338338
}
339339

340-
Value* BuiltInCallables::execute(Eval& eval, ArgumentInvocation* arguments, const SourceSpan& pstate)
340+
Value* BuiltInCallables::execute(Eval& eval, CallableArguments* arguments, const SourceSpan& pstate)
341341
{
342342
return eval.execute(this, arguments, pstate);
343343
}
344344

345-
Value* UserDefinedCallable::execute(Eval& eval, ArgumentInvocation* arguments, const SourceSpan& pstate)
345+
Value* UserDefinedCallable::execute(Eval& eval, CallableArguments* arguments, const SourceSpan& pstate)
346346
{
347347
return eval.execute(this, arguments, pstate);
348348
}
349349

350-
Value* ExternalCallable::execute(Eval& eval, ArgumentInvocation* arguments, const SourceSpan& pstate)
350+
Value* ExternalCallable::execute(Eval& eval, CallableArguments* arguments, const SourceSpan& pstate)
351351
{
352352
return eval.execute(this, arguments, pstate);
353353
}

src/ast_callables.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Sass {
2525
// The function name
2626
ADD_CONSTREF(EnvKey, envkey);
2727

28-
ADD_CONSTREF(ArgumentDeclarationObj, parameters);
28+
ADD_CONSTREF(CallableSignatureObj, parameters);
2929

3030
ADD_REF(SassFnPair, function);
3131

@@ -37,15 +37,15 @@ namespace Sass {
3737
// Throws a [SassFormatException] if parsing fails.
3838
BuiltInCallable(
3939
const EnvKey& fname,
40-
ArgumentDeclaration* parameters,
40+
CallableSignature* parameters,
4141
const SassFnSig& callback);
4242

4343
// Return callback with matching signature
4444
const SassFnPair& callbackFor(
4545
const ArgumentResults& evaluated);
4646

4747
// The main entry point to execute the function (implemented in each specialization)
48-
Value* execute(Eval& eval, ArgumentInvocation* arguments, const SourceSpan& pstate) override final;
48+
Value* execute(Eval& eval, CallableArguments* arguments, const SourceSpan& pstate) override final;
4949

5050
// Return the function name
5151
const sass::string& name() const override final { return envkey_.norm(); }
@@ -83,7 +83,7 @@ namespace Sass {
8383
const ArgumentResults& evaluated);
8484

8585
// The main entry point to execute the function (implemented in each specialization)
86-
Value* execute(Eval& eval, ArgumentInvocation* arguments, const SourceSpan& pstate) override final;
86+
Value* execute(Eval& eval, CallableArguments* arguments, const SourceSpan& pstate) override final;
8787

8888
// Return the function name
8989
const sass::string& name() const override final { return envkey_.norm(); }
@@ -118,7 +118,7 @@ namespace Sass {
118118
UserDefinedCallable* content);
119119

120120
// The main entry point to execute the function (implemented in each specialization)
121-
Value* execute(Eval& eval, ArgumentInvocation* arguments, const SourceSpan& pstate) override final;
121+
Value* execute(Eval& eval, CallableArguments* arguments, const SourceSpan& pstate) override final;
122122

123123
// Return the function name
124124
const sass::string& name() const override final { return envkey_.norm(); }
@@ -140,7 +140,7 @@ namespace Sass {
140140
// Name of this callable (used for reporting)
141141
ADD_CONSTREF(EnvKey, envkey);
142142
// The declaration (parameters this function takes).
143-
ADD_CONSTREF(ArgumentDeclarationObj, declaration);
143+
ADD_CONSTREF(CallableSignatureObj, declaration);
144144
// The attached external callback reference
145145
ADD_PROPERTY(SassFunctionLambda, lambda);
146146
// The attached external data cookie
@@ -151,7 +151,7 @@ namespace Sass {
151151
// Value constructor
152152
ExternalCallable(
153153
const EnvKey& fname,
154-
ArgumentDeclaration* parameters,
154+
CallableSignature* parameters,
155155
SassFunctionLambda function);
156156

157157
// Destructor
@@ -160,7 +160,7 @@ namespace Sass {
160160
}
161161

162162
// The main entry point to execute the function (implemented in each specialization)
163-
Value* execute(Eval& eval, ArgumentInvocation* arguments, const SourceSpan& pstate) override final;
163+
Value* execute(Eval& eval, CallableArguments* arguments, const SourceSpan& pstate) override final;
164164

165165
// Return the function name
166166
const sass::string& name() const override final { return envkey_.norm(); }

src/ast_expressions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ namespace Sass {
117117
PlainCssCallable2::PlainCssCallable2(
118118
SourceSpan pstate,
119119
Interpolation* itpl,
120-
ArgumentInvocation* args,
120+
CallableArguments* args,
121121
const sass::string& ns) :
122122
Expression(std::move(pstate)),
123123
itpl_(itpl),
@@ -267,7 +267,7 @@ namespace Sass {
267267
FunctionExpression::FunctionExpression(
268268
SourceSpan pstate,
269269
const sass::string& name,
270-
ArgumentInvocation* arguments,
270+
CallableArguments* arguments,
271271
bool withinLoop,
272272
const sass::string& ns) :
273273
InvocationExpression(

0 commit comments

Comments
 (0)