Skip to content

Commit c54abcd

Browse files
committed
Cleanup ast containers
1 parent ee8b5b4 commit c54abcd

26 files changed

+113
-119
lines changed

src/ast_callable.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace Sass {
2222
typedef sass::vector<SassFnPair> SassFnPairs;
2323

2424
/////////////////////////////////////////////////////////////////////////
25+
// Base class for everything that can be called on demand.
2526
/////////////////////////////////////////////////////////////////////////
26-
2727
class Callable : public AstNode
2828
{
2929
public:
@@ -46,13 +46,12 @@ namespace Sass {
4646
// Declare up-casting methods
4747
DECLARE_ISA_CASTER(BuiltInCallable);
4848
DECLARE_ISA_CASTER(BuiltInCallables);
49-
DECLARE_ISA_CASTER(PlainCssCallable2);
5049
DECLARE_ISA_CASTER(UserDefinedCallable);
5150
DECLARE_ISA_CASTER(ExternalCallable);
5251
};
5352

5453
/////////////////////////////////////////////////////////////////////////
55-
// Individual argument objects for mixin and function calls.
54+
// Individual argument object for function signatures.
5655
/////////////////////////////////////////////////////////////////////////
5756
class Argument final : public AstNode
5857
{
@@ -76,7 +75,7 @@ namespace Sass {
7675

7776
//////////////////////////////////////////////////////////////////////
7877
// Object for the function signature holding which parameters a
79-
// callable can have or expects and to which variable it's assigned.
78+
// callable can have or expects, with optional rest arguments.
8079
//////////////////////////////////////////////////////////////////////
8180
class CallableSignature final : public AstNode
8281
{
@@ -86,7 +85,6 @@ namespace Sass {
8685
ADD_REF(sass::vector<ArgumentObj>, arguments);
8786

8887
// The name of the rest argument (as in `$args...`),
89-
// or `null` if none was declared.
9088
ADD_CONSTREF(EnvKey, restArg);
9189

9290
// This is only used for debugging

src/ast_callables.cpp

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

2323
BuiltInCallable::BuiltInCallable(
2424
const EnvKey& envkey,
25-
CallableSignature* parameters,
25+
CallableSignature* signature,
2626
const SassFnSig& callback) :
2727
Callable(SourceSpan::internal("[BUILTIN]")),
2828
envkey_(envkey),
2929
// Create a single entry in overloaded function
30-
function_(SassFnPair{ parameters, callback })
30+
function_(SassFnPair{ signature, callback })
3131
{}
3232

3333
// Return callback with matching signature
@@ -170,7 +170,7 @@ namespace Sass {
170170
}
171171
}
172172

173-
// Parse source into arguments
173+
// Parse `source` into signature
174174
CallableSignature* CallableSignature::parse(
175175
Compiler& context, SourceData* source)
176176
{

src/ast_callables.hpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/*****************************************************************************/
22
/* Part of LibSass, released under the MIT license (See LICENSE.txt). */
33
/*****************************************************************************/
4+
// This file was deliberately split from `ast_callable.hpp`. It contains
5+
// all the specialized implementations for callables. These often need
6+
// access to higher level classes, which poses include dependency issues
7+
// if everything would be defined in a single header file.
8+
/*****************************************************************************/
49
#ifndef SASS_AST_CALLABLES_HPP
510
#define SASS_AST_CALLABLES_HPP
611

@@ -18,15 +23,14 @@
1823
namespace Sass {
1924

2025
/////////////////////////////////////////////////////////////////////////
26+
// Internal callables provided by LibSass itself.
2127
/////////////////////////////////////////////////////////////////////////
22-
2328
class BuiltInCallable final : public Callable {
2429

25-
// The function name
30+
// Name of this callable/function
2631
ADD_CONSTREF(EnvKey, envkey);
2732

28-
ADD_CONSTREF(CallableSignatureObj, parameters);
29-
33+
// Pair of signature and callback
3034
ADD_REF(SassFnPair, function);
3135

3236
public:
@@ -37,7 +41,7 @@ namespace Sass {
3741
// Throws a [SassFormatException] if parsing fails.
3842
BuiltInCallable(
3943
const EnvKey& fname,
40-
CallableSignature* parameters,
44+
CallableSignature* signature,
4145
const SassFnSig& callback);
4246

4347
// Return callback with matching signature
@@ -53,15 +57,16 @@ namespace Sass {
5357
// Equality comparator (needed for `get-function` value)
5458
bool operator==(const Callable& rhs) const override final;
5559

60+
// Define isaBuiltInCallable up-cast function
5661
IMPLEMENT_ISA_CASTER(BuiltInCallable);
5762
};
5863

5964
/////////////////////////////////////////////////////////////////////////
65+
// Internal callable with multiple signatures to choose from.
6066
/////////////////////////////////////////////////////////////////////////
61-
6267
class BuiltInCallables final : public Callable {
6368

64-
// The function name
69+
// Name of this callable/function
6570
ADD_CONSTREF(EnvKey, envkey);
6671

6772
// The overloads declared for this callable.
@@ -91,12 +96,13 @@ namespace Sass {
9196
// Equality comparator (needed for `get-function` value)
9297
bool operator==(const Callable& rhs) const override final;
9398

99+
// Define isaBuiltInCallables up-cast function
94100
IMPLEMENT_ISA_CASTER(BuiltInCallables);
95101
};
96102

97103
/////////////////////////////////////////////////////////////////////////
104+
// User defined callable from sass code (functions and mixins)
98105
/////////////////////////////////////////////////////////////////////////
99-
100106
class UserDefinedCallable final : public Callable
101107
{
102108
private:
@@ -126,13 +132,13 @@ namespace Sass {
126132
// Equality comparator (needed for `get-function` value)
127133
bool operator==(const Callable& rhs) const override final;
128134

135+
// Define isaUserDefinedCallable up-cast function
129136
IMPLEMENT_ISA_CASTER(UserDefinedCallable);
130137
};
131138

132139
/////////////////////////////////////////////////////////////////////////
140+
// External callable defined on the C-API side.
133141
/////////////////////////////////////////////////////////////////////////
134-
135-
// ToDo: this could be struct SassFunction!?
136142
class ExternalCallable final : public Callable
137143
{
138144
private:
@@ -154,11 +160,6 @@ namespace Sass {
154160
CallableSignature* parameters,
155161
SassFunctionLambda function);
156162

157-
// Destructor
158-
~ExternalCallable() override final {
159-
// sass_delete_function(function_);
160-
}
161-
162163
// The main entry point to execute the function (implemented in each specialization)
163164
Value* execute(Eval& eval, CallableArguments* arguments, const SourceSpan& pstate) override final;
164165

@@ -168,6 +169,7 @@ namespace Sass {
168169
// Equality comparator (needed for `get-function` value)
169170
bool operator==(const Callable& rhs) const override final;
170171

172+
// Define isaExternalCallable up-cast function
171173
IMPLEMENT_ISA_CASTER(ExternalCallable);
172174
};
173175

src/ast_containers.hpp

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,34 @@ namespace Sass {
5252
if (!childless) elements_ = std::move(vec);
5353
}
5454

55-
// Allow destructor overloading
56-
// virtual ~Vectorized() {};
57-
5855
// Some simple method delegations
59-
T& last() { return elements_.back(); }
60-
T& first() { return elements_.front(); }
61-
const T& last() const { return elements_.back(); }
62-
const T& first() const { return elements_.front(); }
63-
bool empty() const { return elements_.empty(); }
6456
void clear() { return elements_.clear(); }
6557
size_t size() const { return elements_.size(); }
58+
bool empty() const { return elements_.empty(); }
59+
const T& at(size_t i) const { return elements_.at(i); }
60+
const T& get(size_t i) const { return elements_[i]; }
61+
const T& last() const { return elements_.back(); }
62+
const T& first() const { return elements_.front(); }
63+
64+
// Setter to ensure we reset hash value
65+
void set(size_t i, const T& value) {
66+
hash_ = 0; // reset hash
67+
elements_.at(i) = value;
68+
}
69+
70+
// Setter to ensure we reset hash value
71+
void set_last(const T& value) {
72+
hash_ = 0; // reset hash
73+
elements_.back() = value;
74+
}
6675

6776
// Check underlying containers for equality
68-
// Note: maybe we could gain some speed by checking
69-
// the computed hash first, before doing full test?
7077
bool operator== (const Vectorized<V>& rhs) const
7178
{
7279
// Abort early if sizes do not match
7380
if (size() != rhs.size()) return false;
81+
// Abort early if hashes exist and don't match
82+
if (hash_ && rhs.hash_ && hash_ != rhs.hash_) return false;
7483
// Otherwise test each node for object equality in order
7584
return std::equal(begin(), end(), rhs.begin(), ObjEqualityFn<T>);
7685
}
@@ -81,15 +90,6 @@ namespace Sass {
8190
return !(*this == rhs);
8291
}
8392

84-
T& at(size_t i) { return elements_.at(i); }
85-
T& get(size_t i) { return elements_[i]; }
86-
T& operator[](size_t i) { return elements_[i]; }
87-
88-
const T& at(size_t i) const { return elements_.at(i); }
89-
const T& get(size_t i) const { return elements_[i]; }
90-
// ToDo: might insert am item? (update ordered list)
91-
const T& operator[](size_t i) const { return elements_[i]; }
92-
9393
// Implicitly get the sass::vector from our object
9494
// Makes the Vector directly assignable to sass::vector
9595
// You are responsible to make a copy if needed
@@ -102,13 +102,14 @@ namespace Sass {
102102
// You are responsible to make a copy if needed
103103
// Note: since this returns the real object, we can't
104104
// Note: guarantee that the hash will not get out of sync
105-
sass::vector<T>& elements() { return elements_; }
105+
sass::vector<T>& elements43() { return elements_; }
106106
const sass::vector<T>& elements() const { return elements_; }
107107

108108
// Insert all items from compatible vector
109109
void concat(const sass::vector<T>& v)
110110
{
111111
if (v.empty()) return;
112+
hash_ = 0; // reset hash
112113
elements_.insert(end(),
113114
v.begin(), v.end());
114115
}
@@ -117,6 +118,7 @@ namespace Sass {
117118
void concat(sass::vector<T>&& v)
118119
{
119120
if (v.empty()) return;
121+
hash_ = 0; // reset hash
120122
elements_.insert(elements_.end(),
121123
std::make_move_iterator(v.begin()),
122124
std::make_move_iterator(v.end()));
@@ -133,27 +135,31 @@ namespace Sass {
133135
// Insert one item on the front
134136
void unshift(const T& element)
135137
{
138+
hash_ = 0; // reset hash
136139
elements_.insert(begin(),
137140
std::copy(element));
138141
}
139142

140143
// Insert one item on the front
141144
void unshift(T&& element)
142145
{
146+
hash_ = 0; // reset hash
143147
elements_.insert(begin(),
144148
std::move(element));
145149
}
146150

147151
// Remove and return item on the front
148152
T shift() {
149153
T head = first();
154+
hash_ = 0; // reset hash
150155
elements_.erase(begin());
151156
return head;
152157
}
153158

154159
// Remove and return item on the back
155160
T pop() {
156161
T tail = last();
162+
hash_ = 0; // reset hash
157163
elements_.pop_back();
158164
return tail;
159165
}
@@ -162,13 +168,15 @@ namespace Sass {
162168
// ToDo: rename this to push?
163169
void append(const T& element)
164170
{
171+
hash_ = 0; // reset hash
165172
elements_.emplace_back(element);
166173
}
167174

168175
// Insert one item on the back
169176
// ToDo: rename this to push?
170177
void append(T&& element)
171178
{
179+
hash_ = 0; // reset hash
172180
elements_.emplace_back(std::move(element));
173181
}
174182

@@ -202,18 +210,6 @@ namespace Sass {
202210
return false;
203211
}
204212

205-
// This might be better implemented as `operator=`?
206-
void elementsM(const sass::vector<T>& e)
207-
{
208-
elements_ = e;
209-
}
210-
211-
// This might be better implemented as `operator=`?
212-
void elementsM(sass::vector<T>&& e)
213-
{
214-
elements_ = std::move(e);
215-
}
216-
217213
template <typename P>
218214
typename sass::vector<T>::iterator insert(P position, const T& val) {
219215
return elements_.insert(position, val);
@@ -374,14 +370,14 @@ namespace Sass {
374370
return elements_;
375371
}
376372

377-
const sass::vector<K> keys() const {
373+
sass::vector<K> keys() const {
378374
sass::vector<T> list;
379375
for (auto kv : elements_) {
380376
list.emplace_back(kv.first);
381377
}
382378
return list;
383379
}
384-
const sass::vector<T> values() const {
380+
sass::vector<T> values() const {
385381
sass::vector<T> list;
386382
for (auto kv : elements_) {
387383
list.emplace_back(kv.second);

src/ast_css.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ namespace Sass {
308308
return SASS_MEMORY_NEW_DBG(CssAtRule, this);
309309
}
310310

311+
// Define isaCssAtRule up-cast function
311312
IMPLEMENT_ISA_CASTER(CssAtRule);
312313
};
313314

@@ -390,6 +391,7 @@ namespace Sass {
390391
return SASS_MEMORY_NEW_DBG(CssStyleRule, this, childless);
391392
}
392393

394+
// Define isaCssStyleRule up-cast function
393395
IMPLEMENT_ISA_CASTER(CssStyleRule);
394396
};
395397

@@ -429,6 +431,7 @@ namespace Sass {
429431
return SASS_MEMORY_NEW_DBG(CssSupportsRule, this, childless);
430432
}
431433

434+
// Define isaCssSupportsRule up-cast function
432435
IMPLEMENT_ISA_CASTER(CssSupportsRule);
433436
};
434437

@@ -539,6 +542,7 @@ namespace Sass {
539542
return SASS_MEMORY_NEW_DBG(CssMediaRule, this, childless);
540543
}
541544

545+
// Define isaCssMediaRule up-cast function
542546
IMPLEMENT_ISA_CASTER(CssMediaRule);
543547
};
544548
// EO CssMediaRule

src/ast_expressions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace Sass {
114114
return containsDoubleQuote ? $apos : $quote;
115115
}
116116

117-
PlainCssCallable2::PlainCssCallable2(
117+
PlainCssFunction::PlainCssFunction(
118118
SourceSpan pstate,
119119
Interpolation* itpl,
120120
CallableArguments* args,

0 commit comments

Comments
 (0)