Skip to content

Commit

Permalink
Constantify a few AST query methods
Browse files Browse the repository at this point in the history
This exposed some ambiguity with the `type` method.
Renamed some methods to avoid the name clash for now.
  • Loading branch information
mgreter committed Jan 21, 2017
1 parent d755478 commit 083e92c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
20 changes: 10 additions & 10 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,29 @@ namespace Sass {
return length() < rhs.length();
}

bool Compound_Selector::has_parent_ref()
bool Compound_Selector::has_parent_ref() const
{
for (Simple_Selector_Obj s : *this) {
if (s && s->has_parent_ref()) return true;
}
return false;
}

bool Compound_Selector::has_real_parent_ref()
bool Compound_Selector::has_real_parent_ref() const
{
for (Simple_Selector_Obj s : *this) {
if (s && s->has_real_parent_ref()) return true;
}
return false;
}

bool Complex_Selector::has_parent_ref()
bool Complex_Selector::has_parent_ref() const
{
return (head() && head()->has_parent_ref()) ||
(tail() && tail()->has_parent_ref());
}

bool Complex_Selector::has_real_parent_ref()
bool Complex_Selector::has_real_parent_ref() const
{
return (head() && head()->has_real_parent_ref()) ||
(tail() && tail()->has_real_parent_ref());
Expand Down Expand Up @@ -1488,12 +1488,12 @@ namespace Sass {
}
return hash_;
}
bool Wrapped_Selector::has_parent_ref() {
bool Wrapped_Selector::has_parent_ref() const {
// if (has_reference()) return true;
if (!selector()) return false;
return selector()->has_parent_ref();
}
bool Wrapped_Selector::has_real_parent_ref() {
bool Wrapped_Selector::has_real_parent_ref() const {
// if (has_reference()) return true;
if (!selector()) return false;
return selector()->has_real_parent_ref();
Expand All @@ -1504,31 +1504,31 @@ namespace Sass {
}


bool Selector_List::has_parent_ref()
bool Selector_List::has_parent_ref() const
{
for (Complex_Selector_Obj s : elements()) {
if (s && s->has_parent_ref()) return true;
}
return false;
}

bool Selector_List::has_real_parent_ref()
bool Selector_List::has_real_parent_ref() const
{
for (Complex_Selector_Obj s : elements()) {
if (s && s->has_real_parent_ref()) return true;
}
return false;
}

bool Selector_Schema::has_parent_ref()
bool Selector_Schema::has_parent_ref() const
{
if (String_Schema_Obj schema = Cast<String_Schema>(contents())) {
return schema->length() > 0 && Cast<Parent_Selector>(schema->at(0)) != NULL;
}
return false;
}

bool Selector_Schema::has_real_parent_ref()
bool Selector_Schema::has_real_parent_ref() const
{
if (String_Schema_Obj schema = Cast<String_Schema>(contents())) {
Parent_Selector_Obj p = Cast<Parent_Selector>(schema->at(0));
Expand Down
82 changes: 41 additions & 41 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ namespace Sass {
{ }
virtual operator bool() { return true; }
virtual ~Expression() { }
virtual std::string type() { return ""; /* TODO: raise an error? */ }
virtual std::string type() const { return ""; /* TODO: raise an error? */ }
virtual bool is_invisible() const { return false; }
static std::string type_name() { return ""; }
virtual bool is_false() { return false; }
Expand Down Expand Up @@ -1073,7 +1073,7 @@ namespace Sass {
is_bracketed_(ptr->is_bracketed_),
from_selector_(ptr->from_selector_)
{ concrete_type(LIST); }
std::string type() { return is_arglist_ ? "arglist" : "list"; }
std::string type() const { return is_arglist_ ? "arglist" : "list"; }
static std::string type_name() { return "list"; }
const char* sep_string(bool compressed = false) const {
return separator() == SASS_SPACE ?
Expand Down Expand Up @@ -1122,7 +1122,7 @@ namespace Sass {
: Value(ptr),
Hashed(*ptr)
{ concrete_type(MAP); }
std::string type() { return "map"; }
std::string type() const { return "map"; }
static std::string type_name() { return "map"; }
bool is_invisible() const { return empty(); }
List_Obj to_list(Context& ctx, ParserState& pstate);
Expand Down Expand Up @@ -1190,7 +1190,7 @@ namespace Sass {
hash_(ptr->hash_)
{ }
const std::string type_name() {
switch (type()) {
switch (optype()) {
case AND: return "and"; break;
case OR: return "or"; break;
case EQ: return "eq"; break;
Expand All @@ -1210,7 +1210,7 @@ namespace Sass {
}
}
const std::string separator() {
switch (type()) {
switch (optype()) {
case AND: return "&&"; break;
case OR: return "||"; break;
case EQ: return "=="; break;
Expand Down Expand Up @@ -1261,13 +1261,13 @@ namespace Sass {
virtual size_t hash()
{
if (hash_ == 0) {
hash_ = std::hash<size_t>()(type());
hash_ = std::hash<size_t>()(optype());
hash_combine(hash_, left()->hash());
hash_combine(hash_, right()->hash());
}
return hash_;
}
enum Sass_OP type() const { return op_.operand; }
enum Sass_OP optype() const { return op_.operand; }
ATTACH_AST_OPERATIONS(Binary_Expression)
ATTACH_OPERATIONS()
};
Expand All @@ -1279,21 +1279,21 @@ namespace Sass {
public:
enum Type { PLUS, MINUS, NOT };
private:
HASH_PROPERTY(Type, type)
HASH_PROPERTY(Type, optype)
HASH_PROPERTY(Expression_Obj, operand)
size_t hash_;
public:
Unary_Expression(ParserState pstate, Type t, Expression_Obj o)
: Expression(pstate), type_(t), operand_(o), hash_(0)
: Expression(pstate), optype_(t), operand_(o), hash_(0)
{ }
Unary_Expression(const Unary_Expression* ptr)
: Expression(ptr),
type_(ptr->type_),
optype_(ptr->optype_),
operand_(ptr->operand_),
hash_(ptr->hash_)
{ }
const std::string type_name() {
switch (type_) {
switch (optype_) {
case PLUS: return "plus"; break;
case MINUS: return "minus"; break;
case NOT: return "not"; break;
Expand All @@ -1318,7 +1318,7 @@ namespace Sass {
virtual size_t hash()
{
if (hash_ == 0) {
hash_ = std::hash<size_t>()(type_);
hash_ = std::hash<size_t>()(optype_);
hash_combine(hash_, operand()->hash());
};
return hash_;
Expand Down Expand Up @@ -1541,17 +1541,17 @@ namespace Sass {
public:
enum Type { NUMBER, PERCENTAGE, DIMENSION, HEX };
private:
HASH_PROPERTY(Type, type)
HASH_PROPERTY(Type, valtype)
HASH_CONSTREF(std::string, value)
size_t hash_;
public:
Textual(ParserState pstate, Type t, std::string val)
: Expression(pstate, DELAYED), type_(t), value_(val),
: Expression(pstate, DELAYED), valtype_(t), value_(val),
hash_(0)
{ }
Textual(const Textual* ptr)
: Expression(ptr),
type_(ptr->type_),
valtype_(ptr->valtype_),
value_(ptr->value_),
hash_(ptr->hash_)
{ }
Expand All @@ -1574,7 +1574,7 @@ namespace Sass {
{
if (hash_ == 0) {
hash_ = std::hash<std::string>()(value_);
hash_combine(hash_, std::hash<int>()(type_));
hash_combine(hash_, std::hash<int>()(valtype_));
}
return hash_;
}
Expand Down Expand Up @@ -1609,7 +1609,7 @@ namespace Sass {
std::vector<std::string>& denominator_units() { return denominator_units_; }
const std::vector<std::string>& numerator_units() const { return numerator_units_; }
const std::vector<std::string>& denominator_units() const { return denominator_units_; }
std::string type() { return "number"; }
std::string type() const { return "number"; }
static std::string type_name() { return "number"; }
std::string unit() const;

Expand Down Expand Up @@ -1662,7 +1662,7 @@ namespace Sass {
disp_(ptr->disp_),
hash_(ptr->hash_)
{ concrete_type(COLOR); }
std::string type() { return "color"; }
std::string type() const { return "color"; }
static std::string type_name() { return "color"; }

virtual size_t hash()
Expand Down Expand Up @@ -1733,7 +1733,7 @@ namespace Sass {
hash_(ptr->hash_)
{ concrete_type(BOOLEAN); }
virtual operator bool() { return value_; }
std::string type() { return "bool"; }
std::string type() const { return "bool"; }
static std::string type_name() { return "bool"; }
virtual bool is_false() { return !value_; }

Expand Down Expand Up @@ -1792,7 +1792,7 @@ namespace Sass {
hash_(ptr->hash_)
{ concrete_type(STRING); }

std::string type() { return "string"; }
std::string type() const { return "string"; }
static std::string type_name() { return "string"; }

bool is_left_interpolant(void) const;
Expand Down Expand Up @@ -1853,7 +1853,7 @@ namespace Sass {
String_Constant(ParserState pstate, const Token& tok)
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(std::string(tok.begin, tok.end))), hash_(0)
{ }
std::string type() { return "string"; }
std::string type() const { return "string"; }
static std::string type_name() { return "string"; }
virtual bool is_invisible() const;
virtual void rtrim();
Expand Down Expand Up @@ -2142,7 +2142,7 @@ namespace Sass {
public:
Null(ParserState pstate) : Value(pstate) { concrete_type(NULL_VAL); }
Null(const Null* ptr) : Value(ptr) { concrete_type(NULL_VAL); }
std::string type() { return "null"; }
std::string type() const { return "null"; }
static std::string type_name() { return "null"; }
bool is_invisible() const { return true; }
operator bool() { return false; }
Expand Down Expand Up @@ -2296,10 +2296,10 @@ namespace Sass {
virtual void set_media_block(Media_Block_Ptr mb) {
media_block(mb);
}
virtual bool has_parent_ref() {
virtual bool has_parent_ref() const {
return false;
}
virtual bool has_real_parent_ref() {
virtual bool has_real_parent_ref() const {
return false;
}
// dispatch to correct handlers
Expand Down Expand Up @@ -2334,8 +2334,8 @@ namespace Sass {
connect_parent_(ptr->connect_parent_),
media_block_(ptr->media_block_)
{ }
virtual bool has_parent_ref();
virtual bool has_real_parent_ref();
virtual bool has_parent_ref() const;
virtual bool has_real_parent_ref() const;
virtual bool operator<(const Selector& rhs) const;
virtual bool operator==(const Selector& rhs) const;
// selector schema is not yet a final selector, so we do not
Expand Down Expand Up @@ -2429,8 +2429,8 @@ namespace Sass {

virtual ~Simple_Selector() = 0;
virtual Compound_Selector_Ptr unify_with(Compound_Selector_Ptr, Context&);
virtual bool has_parent_ref() { return false; };
virtual bool has_real_parent_ref() { return false; };
virtual bool has_parent_ref() const { return false; };
virtual bool has_real_parent_ref() const { return false; };
virtual bool is_pseudo_element() const { return false; }

virtual bool is_superselector_of(Compound_Selector_Obj sub) { return false; }
Expand Down Expand Up @@ -2463,14 +2463,14 @@ namespace Sass {
Parent_Selector(const Parent_Selector* ptr)
: Simple_Selector(ptr), real_(ptr->real_)
{ /* has_reference(true); */ }
bool is_real_parent_ref() { return real(); };
virtual bool has_parent_ref() { return true; };
virtual bool has_real_parent_ref() { return is_real_parent_ref(); };
bool is_real_parent_ref() const { return real(); };
virtual bool has_parent_ref() const { return true; };
virtual bool has_real_parent_ref() const { return is_real_parent_ref(); };
virtual unsigned long specificity() const
{
return 0;
}
std::string type() { return "selector"; }
std::string type() const { return "selector"; }
static std::string type_name() { return "selector"; }
ATTACH_AST_OPERATIONS(Parent_Selector)
ATTACH_OPERATIONS()
Expand Down Expand Up @@ -2676,8 +2676,8 @@ namespace Sass {
// Selectors inside the negation pseudo-class are counted like any
// other, but the negation itself does not count as a pseudo-class.
virtual size_t hash();
virtual bool has_parent_ref();
virtual bool has_real_parent_ref();
virtual bool has_parent_ref() const;
virtual bool has_real_parent_ref() const;
virtual unsigned long specificity() const;
virtual bool operator==(const Simple_Selector& rhs) const;
virtual bool operator==(const Wrapped_Selector& rhs) const;
Expand Down Expand Up @@ -2733,8 +2733,8 @@ namespace Sass {
Complex_Selector_Obj to_complex();
Compound_Selector_Ptr unify_with(Compound_Selector_Ptr rhs, Context& ctx);
// virtual Placeholder_Selector_Ptr find_placeholder();
virtual bool has_parent_ref();
virtual bool has_real_parent_ref();
virtual bool has_parent_ref() const;
virtual bool has_real_parent_ref() const;
Simple_Selector_Ptr base() const {
if (length() == 0) return 0;
// ToDo: why is this needed?
Expand Down Expand Up @@ -2827,8 +2827,8 @@ namespace Sass {
head_(ptr->head_), tail_(ptr->tail_),
reference_(ptr->reference_)
{};
virtual bool has_parent_ref();
virtual bool has_real_parent_ref();
virtual bool has_parent_ref() const;
virtual bool has_real_parent_ref() const;

Complex_Selector_Obj skip_empty_reference()
{
Expand Down Expand Up @@ -2977,11 +2977,11 @@ namespace Sass {
schema_(ptr->schema_),
wspace_(ptr->wspace_)
{ }
std::string type() { return "list"; }
std::string type() const { return "list"; }
// remove parent selector references
// basically unwraps parsed selectors
virtual bool has_parent_ref();
virtual bool has_real_parent_ref();
virtual bool has_parent_ref() const;
virtual bool has_real_parent_ref() const;
void remove_parent_selectors();
Selector_List_Ptr resolve_parent_refs(Context& ctx, std::vector<Selector_List_Obj>& pstack, bool implicit_parent = true);
virtual bool is_superselector_of(Compound_Selector_Obj sub, std::string wrapping = "");
Expand Down
Loading

0 comments on commit 083e92c

Please sign in to comment.