Blame src/functions.cpp

Packit Service 7770af
#include "sass.hpp"
Packit Service 7770af
#include "functions.hpp"
Packit Service 7770af
#include "ast.hpp"
Packit Service 7770af
#include "context.hpp"
Packit Service 7770af
#include "backtrace.hpp"
Packit Service 7770af
#include "parser.hpp"
Packit Service 7770af
#include "constants.hpp"
Packit Service 7770af
#include "inspect.hpp"
Packit Service 7770af
#include "extend.hpp"
Packit Service 7770af
#include "eval.hpp"
Packit Service 7770af
#include "util.hpp"
Packit Service 7770af
#include "expand.hpp"
Packit Service 7770af
#include "utf8_string.hpp"
Packit Service 7770af
#include "sass/base.h"
Packit Service 7770af
#include "utf8.h"
Packit Service 7770af
Packit Service 7770af
#include <cstdint>
Packit Service 7770af
#include <cstdlib>
Packit Service 7770af
#include <cmath>
Packit Service 7770af
#include <cctype>
Packit Service 7770af
#include <sstream>
Packit Service 7770af
#include <string>
Packit Service 7770af
#include <iomanip>
Packit Service 7770af
#include <iostream>
Packit Service 7770af
#include <random>
Packit Service 7770af
#include <set>
Packit Service 7770af
Packit Service 7770af
#ifdef __MINGW32__
Packit Service 7770af
#include "windows.h"
Packit Service 7770af
#include "wincrypt.h"
Packit Service 7770af
#endif
Packit Service 7770af
Packit Service 7770af
#define ARG(argname, argtype) get_arg<argtype>(argname, env, sig, pstate, backtrace)
Packit Service 7770af
#define ARGR(argname, argtype, lo, hi) get_arg_r(argname, env, sig, pstate, lo, hi, backtrace)
Packit Service 7770af
#define ARGM(argname, argtype, ctx) get_arg_m(argname, env, sig, pstate, backtrace, ctx)
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
  using std::stringstream;
Packit Service 7770af
  using std::endl;
Packit Service 7770af
Packit Service 7770af
  Definition_Ptr make_native_function(Signature sig, Native_Function func, Context& ctx)
Packit Service 7770af
  {
Packit Service 7770af
    Parser sig_parser = Parser::from_c_str(sig, ctx, ParserState("[built-in function]"));
Packit Service 7770af
    sig_parser.lex<Prelexer::identifier>();
Packit Service 7770af
    std::string name(Util::normalize_underscores(sig_parser.lexed));
Packit Service 7770af
    Parameters_Obj params = sig_parser.parse_parameters();
Packit Service 7770af
    return SASS_MEMORY_NEW(Definition,
Packit Service 7770af
                           ParserState("[built-in function]"),
Packit Service 7770af
                           sig,
Packit Service 7770af
                           name,
Packit Service 7770af
                           params,
Packit Service 7770af
                           func,
Packit Service 7770af
                           false);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  Definition_Ptr make_c_function(Sass_Function_Entry c_func, Context& ctx)
Packit Service 7770af
  {
Packit Service 7770af
    using namespace Prelexer;
Packit Service 7770af
Packit Service 7770af
    const char* sig = sass_function_get_signature(c_func);
Packit Service 7770af
    Parser sig_parser = Parser::from_c_str(sig, ctx, ParserState("[c function]"));
Packit Service 7770af
    // allow to overload generic callback plus @warn, @error and @debug with custom functions
Packit Service 7770af
    sig_parser.lex < alternatives < identifier, exactly <'*'>,
Packit Service 7770af
                                    exactly < Constants::warn_kwd >,
Packit Service 7770af
                                    exactly < Constants::error_kwd >,
Packit Service 7770af
                                    exactly < Constants::debug_kwd >
Packit Service 7770af
                   >              >();
Packit Service 7770af
    std::string name(Util::normalize_underscores(sig_parser.lexed));
Packit Service 7770af
    Parameters_Obj params = sig_parser.parse_parameters();
Packit Service 7770af
    return SASS_MEMORY_NEW(Definition,
Packit Service 7770af
                           ParserState("[c function]"),
Packit Service 7770af
                           sig,
Packit Service 7770af
                           name,
Packit Service 7770af
                           params,
Packit Service 7770af
                           c_func,
Packit Service 7770af
                           false, true);
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  std::string function_name(Signature sig)
Packit Service 7770af
  {
Packit Service 7770af
    std::string str(sig);
Packit Service 7770af
    return str.substr(0, str.find('('));
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  namespace Functions {
Packit Service 7770af
Packit Service 7770af
    inline void handle_utf8_error (const ParserState& pstate, Backtrace* backtrace)
Packit Service 7770af
    {
Packit Service 7770af
      try {
Packit Service 7770af
       throw;
Packit Service 7770af
      }
Packit Service 7770af
      catch (utf8::invalid_code_point) {
Packit Service 7770af
        std::string msg("utf8::invalid_code_point");
Packit Service 7770af
        error(msg, pstate, backtrace);
Packit Service 7770af
      }
Packit Service 7770af
      catch (utf8::not_enough_room) {
Packit Service 7770af
        std::string msg("utf8::not_enough_room");
Packit Service 7770af
        error(msg, pstate, backtrace);
Packit Service 7770af
      }
Packit Service 7770af
      catch (utf8::invalid_utf8) {
Packit Service 7770af
        std::string msg("utf8::invalid_utf8");
Packit Service 7770af
        error(msg, pstate, backtrace);
Packit Service 7770af
      }
Packit Service 7770af
      catch (...) { throw; }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    template <typename T>
Packit Service 7770af
    T* get_arg(const std::string& argname, Env& env, Signature sig, ParserState pstate, Backtrace* backtrace)
Packit Service 7770af
    {
Packit Service 7770af
      // Minimal error handling -- the expectation is that built-ins will be written correctly!
Packit Service 7770af
      T* val = Cast<T>(env[argname]);
Packit Service 7770af
      if (!val) {
Packit Service 7770af
        std::string msg("argument `");
Packit Service 7770af
        msg += argname;
Packit Service 7770af
        msg += "` of `";
Packit Service 7770af
        msg += sig;
Packit Service 7770af
        msg += "` must be a ";
Packit Service 7770af
        msg += T::type_name();
Packit Service 7770af
        error(msg, pstate, backtrace);
Packit Service 7770af
      }
Packit Service 7770af
      return val;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Map_Ptr get_arg_m(const std::string& argname, Env& env, Signature sig, ParserState pstate, Backtrace* backtrace, Context& ctx)
Packit Service 7770af
    {
Packit Service 7770af
      // Minimal error handling -- the expectation is that built-ins will be written correctly!
Packit Service 7770af
      Map_Ptr val = Cast<Map>(env[argname]);
Packit Service 7770af
      if (val) return val;
Packit Service 7770af
Packit Service 7770af
      List_Ptr lval = Cast<List>(env[argname]);
Packit Service 7770af
      if (lval && lval->length() == 0) return SASS_MEMORY_NEW(Map, pstate, 0);
Packit Service 7770af
Packit Service 7770af
      // fallback on get_arg for error handling
Packit Service 7770af
      val = get_arg<Map>(argname, env, sig, pstate, backtrace);
Packit Service 7770af
      return val;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Number_Ptr get_arg_r(const std::string& argname, Env& env, Signature sig, ParserState pstate, double lo, double hi, Backtrace* backtrace)
Packit Service 7770af
    {
Packit Service 7770af
      // Minimal error handling -- the expectation is that built-ins will be written correctly!
Packit Service 7770af
      Number_Ptr val = get_arg<Number>(argname, env, sig, pstate, backtrace);
Packit Service 7770af
      double v = val->value();
Packit Service 7770af
      if (!(lo <= v && v <= hi)) {
Packit Service 7770af
        std::stringstream msg;
Packit Service 7770af
        msg << "argument `" << argname << "` of `" << sig << "` must be between ";
Packit Service 7770af
        msg << lo << " and " << hi;
Packit Service 7770af
        error(msg.str(), pstate, backtrace);
Packit Service 7770af
      }
Packit Service 7770af
      return val;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    #define ARGSEL(argname, seltype, contextualize) get_arg_sel<seltype>(argname, env, sig, pstate, backtrace, ctx)
Packit Service 7770af
Packit Service 7770af
    template <typename T>
Packit Service 7770af
    T get_arg_sel(const std::string& argname, Env& env, Signature sig, ParserState pstate, Backtrace* backtrace, Context& ctx);
Packit Service 7770af
Packit Service 7770af
    template <>
Packit Service 7770af
    Selector_List_Obj get_arg_sel(const std::string& argname, Env& env, Signature sig, ParserState pstate, Backtrace* backtrace, Context& ctx) {
Packit Service 7770af
      Expression_Obj exp = ARG(argname, Expression);
Packit Service 7770af
      if (exp->concrete_type() == Expression::NULL_VAL) {
Packit Service 7770af
        std::stringstream msg;
Packit Service 7770af
        msg << argname << ": null is not a valid selector: it must be a string,\n";
Packit Service 7770af
        msg << "a list of strings, or a list of lists of strings for `" << function_name(sig) << "'";
Packit Service 7770af
        error(msg.str(), pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (String_Constant_Ptr str = Cast<String_Constant>(exp)) {
Packit Service 7770af
        str->quote_mark(0);
Packit Service 7770af
      }
Packit Service 7770af
      std::string exp_src = exp->to_string(ctx.c_options);
Packit Service 7770af
      return Parser::parse_selector(exp_src.c_str(), ctx);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    template <>
Packit Service 7770af
    Compound_Selector_Obj get_arg_sel(const std::string& argname, Env& env, Signature sig, ParserState pstate, Backtrace* backtrace, Context& ctx) {
Packit Service 7770af
      Expression_Obj exp = ARG(argname, Expression);
Packit Service 7770af
      if (exp->concrete_type() == Expression::NULL_VAL) {
Packit Service 7770af
        std::stringstream msg;
Packit Service 7770af
        msg << argname << ": null is not a string for `" << function_name(sig) << "'";
Packit Service 7770af
        error(msg.str(), pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (String_Constant_Ptr str = Cast<String_Constant>(exp)) {
Packit Service 7770af
        str->quote_mark(0);
Packit Service 7770af
      }
Packit Service 7770af
      std::string exp_src = exp->to_string(ctx.c_options);
Packit Service 7770af
      Selector_List_Obj sel_list = Parser::parse_selector(exp_src.c_str(), ctx);
Packit Service 7770af
      if (sel_list->length() == 0) return NULL;
Packit Service 7770af
      Complex_Selector_Obj first = sel_list->first();
Packit Service 7770af
      if (!first->tail()) return first->head();
Packit Service 7770af
      return first->tail()->head();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    #ifdef __MINGW32__
Packit Service 7770af
    uint64_t GetSeed()
Packit Service 7770af
    {
Packit Service 7770af
      HCRYPTPROV hp = 0;
Packit Service 7770af
      BYTE rb[8];
Packit Service 7770af
      CryptAcquireContext(&hp, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
Packit Service 7770af
      CryptGenRandom(hp, sizeof(rb), rb);
Packit Service 7770af
      CryptReleaseContext(hp, 0);
Packit Service 7770af
Packit Service 7770af
      uint64_t seed;
Packit Service 7770af
      memcpy(&seed, &rb[0], sizeof(seed));
Packit Service 7770af
Packit Service 7770af
      return seed;
Packit Service 7770af
    }
Packit Service 7770af
    #else
Packit Service 7770af
    uint64_t GetSeed()
Packit Service 7770af
    {
Packit Service 7770af
      std::random_device rd;
Packit Service 7770af
      return rd();
Packit Service 7770af
    }
Packit Service 7770af
    #endif
Packit Service 7770af
Packit Service 7770af
    // note: the performance of many  implementations of
Packit Service 7770af
    // random_device degrades sharply once the entropy pool
Packit Service 7770af
    // is exhausted. For practical use, random_device is
Packit Service 7770af
    // generally only used to seed a PRNG such as mt19937.
Packit Service 7770af
    static std::mt19937 rand(static_cast<unsigned int>(GetSeed()));
Packit Service 7770af
Packit Service 7770af
    // features
Packit Service 7770af
    static std::set<std::string> features {
Packit Service 7770af
      "global-variable-shadowing",
Packit Service 7770af
      "extend-selector-pseudoclass",
Packit Service 7770af
      "at-error",
Packit Service 7770af
      "units-level-3"
Packit Service 7770af
    };
Packit Service 7770af
Packit Service 7770af
    ////////////////
Packit Service 7770af
    // RGB FUNCTIONS
Packit Service 7770af
    ////////////////
Packit Service 7770af
Packit Service 7770af
    inline double color_num(Number_Ptr n) {
Packit Service 7770af
      if (n->unit() == "%") {
Packit Service 7770af
        return std::min(std::max(n->value() * 255 / 100.0, 0.0), 255.0);
Packit Service 7770af
      } else {
Packit Service 7770af
        return std::min(std::max(n->value(), 0.0), 255.0);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    inline double alpha_num(Number_Ptr n) {
Packit Service 7770af
      if (n->unit() == "%") {
Packit Service 7770af
        return std::min(std::max(n->value(), 0.0), 100.0);
Packit Service 7770af
      } else {
Packit Service 7770af
        return std::min(std::max(n->value(), 0.0), 1.0);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature rgb_sig = "rgb($red, $green, $blue)";
Packit Service 7770af
    BUILT_IN(rgb)
Packit Service 7770af
    {
Packit Service 7770af
      return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                             pstate,
Packit Service 7770af
                             color_num(ARG("$red",   Number)),
Packit Service 7770af
                             color_num(ARG("$green", Number)),
Packit Service 7770af
                             color_num(ARG("$blue",  Number)));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature rgba_4_sig = "rgba($red, $green, $blue, $alpha)";
Packit Service 7770af
    BUILT_IN(rgba_4)
Packit Service 7770af
    {
Packit Service 7770af
      return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                             pstate,
Packit Service 7770af
                             color_num(ARG("$red",   Number)),
Packit Service 7770af
                             color_num(ARG("$green", Number)),
Packit Service 7770af
                             color_num(ARG("$blue",  Number)),
Packit Service 7770af
                             alpha_num(ARG("$alpha", Number)));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature rgba_2_sig = "rgba($color, $alpha)";
Packit Service 7770af
    BUILT_IN(rgba_2)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr c_arg = ARG("$color", Color);
Packit Service 7770af
      Color_Ptr new_c = SASS_MEMORY_COPY(c_arg);
Packit Service 7770af
      new_c->a(alpha_num(ARG("$alpha", Number)));
Packit Service 7770af
      new_c->disp("");
Packit Service 7770af
      return new_c;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature red_sig = "red($color)";
Packit Service 7770af
    BUILT_IN(red)
Packit Service 7770af
    { return SASS_MEMORY_NEW(Number, pstate, ARG("$color", Color)->r()); }
Packit Service 7770af
Packit Service 7770af
    Signature green_sig = "green($color)";
Packit Service 7770af
    BUILT_IN(green)
Packit Service 7770af
    { return SASS_MEMORY_NEW(Number, pstate, ARG("$color", Color)->g()); }
Packit Service 7770af
Packit Service 7770af
    Signature blue_sig = "blue($color)";
Packit Service 7770af
    BUILT_IN(blue)
Packit Service 7770af
    { return SASS_MEMORY_NEW(Number, pstate, ARG("$color", Color)->b()); }
Packit Service 7770af
Packit Service 7770af
    Signature mix_sig = "mix($color-1, $color-2, $weight: 50%)";
Packit Service 7770af
    BUILT_IN(mix)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Obj  color1 = ARG("$color-1", Color);
Packit Service 7770af
      Color_Obj  color2 = ARG("$color-2", Color);
Packit Service 7770af
      Number_Obj weight = ARGR("$weight", Number, 0, 100);
Packit Service 7770af
Packit Service 7770af
      double p = weight->value()/100;
Packit Service 7770af
      double w = 2*p - 1;
Packit Service 7770af
      double a = color1->a() - color2->a();
Packit Service 7770af
Packit Service 7770af
      double w1 = (((w * a == -1) ? w : (w + a)/(1 + w*a)) + 1)/2.0;
Packit Service 7770af
      double w2 = 1 - w1;
Packit Service 7770af
Packit Service 7770af
      return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                             pstate,
Packit Service 7770af
                             Sass::round(w1*color1->r() + w2*color2->r(), ctx.c_options.precision),
Packit Service 7770af
                             Sass::round(w1*color1->g() + w2*color2->g(), ctx.c_options.precision),
Packit Service 7770af
                             Sass::round(w1*color1->b() + w2*color2->b(), ctx.c_options.precision),
Packit Service 7770af
                             color1->a()*p + color2->a()*(1-p));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    ////////////////
Packit Service 7770af
    // HSL FUNCTIONS
Packit Service 7770af
    ////////////////
Packit Service 7770af
Packit Service 7770af
    // RGB to HSL helper function
Packit Service 7770af
    struct HSL { double h; double s; double l; };
Packit Service 7770af
    HSL rgb_to_hsl(double r, double g, double b)
Packit Service 7770af
    {
Packit Service 7770af
Packit Service 7770af
      // Algorithm from http://en.wikipedia.org/wiki/wHSL_and_HSV#Conversion_from_RGB_to_HSL_or_HSV
Packit Service 7770af
      r /= 255.0; g /= 255.0; b /= 255.0;
Packit Service 7770af
Packit Service 7770af
      double max = std::max(r, std::max(g, b));
Packit Service 7770af
      double min = std::min(r, std::min(g, b));
Packit Service 7770af
      double delta = max - min;
Packit Service 7770af
Packit Service 7770af
      double h = 0;
Packit Service 7770af
      double s;
Packit Service 7770af
      double l = (max + min) / 2.0;
Packit Service 7770af
Packit Service 7770af
      if (max == min) {
Packit Service 7770af
        h = s = 0; // achromatic
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        if (l < 0.5) s = delta / (max + min);
Packit Service 7770af
        else         s = delta / (2.0 - max - min);
Packit Service 7770af
Packit Service 7770af
        if      (r == max) h = (g - b) / delta + (g < b ? 6 : 0);
Packit Service 7770af
        else if (g == max) h = (b - r) / delta + 2;
Packit Service 7770af
        else if (b == max) h = (r - g) / delta + 4;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      HSL hsl_struct;
Packit Service 7770af
      hsl_struct.h = h / 6 * 360;
Packit Service 7770af
      hsl_struct.s = s * 100;
Packit Service 7770af
      hsl_struct.l = l * 100;
Packit Service 7770af
Packit Service 7770af
      return hsl_struct;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    // hue to RGB helper function
Packit Service 7770af
    double h_to_rgb(double m1, double m2, double h) {
Packit Service 7770af
      while (h < 0) h += 1;
Packit Service 7770af
      while (h > 1) h -= 1;
Packit Service 7770af
      if (h*6.0 < 1) return m1 + (m2 - m1)*h*6;
Packit Service 7770af
      if (h*2.0 < 1) return m2;
Packit Service 7770af
      if (h*3.0 < 2) return m1 + (m2 - m1) * (2.0/3.0 - h)*6;
Packit Service 7770af
      return m1;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Color_Ptr hsla_impl(double h, double s, double l, double a, Context& ctx, ParserState pstate)
Packit Service 7770af
    {
Packit Service 7770af
      h /= 360.0;
Packit Service 7770af
      s /= 100.0;
Packit Service 7770af
      l /= 100.0;
Packit Service 7770af
Packit Service 7770af
      if (l < 0) l = 0;
Packit Service 7770af
      if (s < 0) s = 0;
Packit Service 7770af
      if (l > 1) l = 1;
Packit Service 7770af
      if (s > 1) s = 1;
Packit Service 7770af
      while (h < 0) h += 1;
Packit Service 7770af
      while (h > 1) h -= 1;
Packit Service 7770af
Packit Service 7770af
      // if saturation is exacly zero, we loose
Packit Service 7770af
      // information for hue, since it will evaluate
Packit Service 7770af
      // to zero if converted back from rgb. Setting
Packit Service 7770af
      // saturation to a very tiny number solves this.
Packit Service 7770af
      if (s == 0) s = 1e-10;
Packit Service 7770af
Packit Service 7770af
      // Algorithm from the CSS3 spec: http://www.w3.org/TR/css3-color/#hsl-color.
Packit Service 7770af
      double m2;
Packit Service 7770af
      if (l <= 0.5) m2 = l*(s+1.0);
Packit Service 7770af
      else m2 = (l+s)-(l*s);
Packit Service 7770af
      double m1 = (l*2.0)-m2;
Packit Service 7770af
      // round the results -- consider moving this into the Color constructor
Packit Service 7770af
      double r = (h_to_rgb(m1, m2, h + 1.0/3.0) * 255.0);
Packit Service 7770af
      double g = (h_to_rgb(m1, m2, h) * 255.0);
Packit Service 7770af
      double b = (h_to_rgb(m1, m2, h - 1.0/3.0) * 255.0);
Packit Service 7770af
Packit Service 7770af
      return SASS_MEMORY_NEW(Color, pstate, r, g, b, a);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature hsl_sig = "hsl($hue, $saturation, $lightness)";
Packit Service 7770af
    BUILT_IN(hsl)
Packit Service 7770af
    {
Packit Service 7770af
      return hsla_impl(ARG("$hue", Number)->value(),
Packit Service 7770af
                       ARG("$saturation", Number)->value(),
Packit Service 7770af
                       ARG("$lightness",  Number)->value(),
Packit Service 7770af
                       1.0,
Packit Service 7770af
                       ctx,
Packit Service 7770af
                       pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature hsla_sig = "hsla($hue, $saturation, $lightness, $alpha)";
Packit Service 7770af
    BUILT_IN(hsla)
Packit Service 7770af
    {
Packit Service 7770af
      return hsla_impl(ARG("$hue", Number)->value(),
Packit Service 7770af
                       ARG("$saturation", Number)->value(),
Packit Service 7770af
                       ARG("$lightness",  Number)->value(),
Packit Service 7770af
                       ARG("$alpha",  Number)->value(),
Packit Service 7770af
                       ctx,
Packit Service 7770af
                       pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature hue_sig = "hue($color)";
Packit Service 7770af
    BUILT_IN(hue)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
      return SASS_MEMORY_NEW(Number, pstate, hsl_color.h, "deg");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature saturation_sig = "saturation($color)";
Packit Service 7770af
    BUILT_IN(saturation)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
      return SASS_MEMORY_NEW(Number, pstate, hsl_color.s, "%");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature lightness_sig = "lightness($color)";
Packit Service 7770af
    BUILT_IN(lightness)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
      return SASS_MEMORY_NEW(Number, pstate, hsl_color.l, "%");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature adjust_hue_sig = "adjust-hue($color, $degrees)";
Packit Service 7770af
    BUILT_IN(adjust_hue)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      Number_Ptr degrees = ARG("$degrees", Number);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
      return hsla_impl(hsl_color.h + degrees->value(),
Packit Service 7770af
                       hsl_color.s,
Packit Service 7770af
                       hsl_color.l,
Packit Service 7770af
                       rgb_color->a(),
Packit Service 7770af
                       ctx,
Packit Service 7770af
                       pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature lighten_sig = "lighten($color, $amount)";
Packit Service 7770af
    BUILT_IN(lighten)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      Number_Ptr amount = ARGR("$amount", Number, 0, 100);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
      //Check lightness is not negative before lighten it
Packit Service 7770af
      double hslcolorL = hsl_color.l;
Packit Service 7770af
      if (hslcolorL < 0) {
Packit Service 7770af
        hslcolorL = 0;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      return hsla_impl(hsl_color.h,
Packit Service 7770af
                       hsl_color.s,
Packit Service 7770af
                       hslcolorL + amount->value(),
Packit Service 7770af
                       rgb_color->a(),
Packit Service 7770af
                       ctx,
Packit Service 7770af
                       pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature darken_sig = "darken($color, $amount)";
Packit Service 7770af
    BUILT_IN(darken)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      Number_Ptr amount = ARGR("$amount", Number, 0, 100);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
Packit Service 7770af
      //Check lightness if not over 100, before darken it
Packit Service 7770af
      double hslcolorL = hsl_color.l;
Packit Service 7770af
      if (hslcolorL > 100) {
Packit Service 7770af
        hslcolorL = 100;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      return hsla_impl(hsl_color.h,
Packit Service 7770af
                       hsl_color.s,
Packit Service 7770af
                       hslcolorL - amount->value(),
Packit Service 7770af
                       rgb_color->a(),
Packit Service 7770af
                       ctx,
Packit Service 7770af
                       pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature saturate_sig = "saturate($color, $amount: false)";
Packit Service 7770af
    BUILT_IN(saturate)
Packit Service 7770af
    {
Packit Service 7770af
      // CSS3 filter function overload: pass literal through directly
Packit Service 7770af
      Number_Ptr amount = Cast<Number>(env["$amount"]);
Packit Service 7770af
      if (!amount) {
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, "saturate(" + env["$color"]->to_string(ctx.c_options) + ")");
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      ARGR("$amount", Number, 0, 100);
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
Packit Service 7770af
      double hslcolorS = hsl_color.s + amount->value();
Packit Service 7770af
Packit Service 7770af
      // Saturation cannot be below 0 or above 100
Packit Service 7770af
      if (hslcolorS < 0) {
Packit Service 7770af
        hslcolorS = 0;
Packit Service 7770af
      }
Packit Service 7770af
      if (hslcolorS > 100) {
Packit Service 7770af
        hslcolorS = 100;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      return hsla_impl(hsl_color.h,
Packit Service 7770af
                       hslcolorS,
Packit Service 7770af
                       hsl_color.l,
Packit Service 7770af
                       rgb_color->a(),
Packit Service 7770af
                       ctx,
Packit Service 7770af
                       pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature desaturate_sig = "desaturate($color, $amount)";
Packit Service 7770af
    BUILT_IN(desaturate)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      Number_Ptr amount = ARGR("$amount", Number, 0, 100);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
Packit Service 7770af
      double hslcolorS = hsl_color.s - amount->value();
Packit Service 7770af
Packit Service 7770af
      // Saturation cannot be below 0 or above 100
Packit Service 7770af
      if (hslcolorS <= 0) {
Packit Service 7770af
        hslcolorS = 0;
Packit Service 7770af
      }
Packit Service 7770af
      if (hslcolorS > 100) {
Packit Service 7770af
        hslcolorS = 100;
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      return hsla_impl(hsl_color.h,
Packit Service 7770af
                       hslcolorS,
Packit Service 7770af
                       hsl_color.l,
Packit Service 7770af
                       rgb_color->a(),
Packit Service 7770af
                       ctx,
Packit Service 7770af
                       pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature grayscale_sig = "grayscale($color)";
Packit Service 7770af
    BUILT_IN(grayscale)
Packit Service 7770af
    {
Packit Service 7770af
      // CSS3 filter function overload: pass literal through directly
Packit Service 7770af
      Number_Ptr amount = Cast<Number>(env["$color"]);
Packit Service 7770af
      if (amount) {
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, "grayscale(" + amount->to_string(ctx.c_options) + ")");
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
      return hsla_impl(hsl_color.h,
Packit Service 7770af
                       0.0,
Packit Service 7770af
                       hsl_color.l,
Packit Service 7770af
                       rgb_color->a(),
Packit Service 7770af
                       ctx,
Packit Service 7770af
                       pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature complement_sig = "complement($color)";
Packit Service 7770af
    BUILT_IN(complement)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      HSL hsl_color = rgb_to_hsl(rgb_color->r(),
Packit Service 7770af
                                 rgb_color->g(),
Packit Service 7770af
                                 rgb_color->b());
Packit Service 7770af
      return hsla_impl(hsl_color.h - 180.0,
Packit Service 7770af
                       hsl_color.s,
Packit Service 7770af
                       hsl_color.l,
Packit Service 7770af
                       rgb_color->a(),
Packit Service 7770af
                       ctx,
Packit Service 7770af
                       pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature invert_sig = "invert($color)";
Packit Service 7770af
    BUILT_IN(invert)
Packit Service 7770af
    {
Packit Service 7770af
      // CSS3 filter function overload: pass literal through directly
Packit Service 7770af
      Number_Ptr amount = Cast<Number>(env["$color"]);
Packit Service 7770af
      if (amount) {
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, "invert(" + amount->to_string(ctx.c_options) + ")");
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      Color_Ptr rgb_color = ARG("$color", Color);
Packit Service 7770af
      return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                             pstate,
Packit Service 7770af
                             255 - rgb_color->r(),
Packit Service 7770af
                             255 - rgb_color->g(),
Packit Service 7770af
                             255 - rgb_color->b(),
Packit Service 7770af
                             rgb_color->a());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    ////////////////////
Packit Service 7770af
    // OPACITY FUNCTIONS
Packit Service 7770af
    ////////////////////
Packit Service 7770af
    Signature alpha_sig = "alpha($color)";
Packit Service 7770af
    Signature opacity_sig = "opacity($color)";
Packit Service 7770af
    BUILT_IN(alpha)
Packit Service 7770af
    {
Packit Service 7770af
      String_Constant_Ptr ie_kwd = Cast<String_Constant>(env["$color"]);
Packit Service 7770af
      if (ie_kwd) {
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, "alpha(" + ie_kwd->value() + ")");
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      // CSS3 filter function overload: pass literal through directly
Packit Service 7770af
      Number_Ptr amount = Cast<Number>(env["$color"]);
Packit Service 7770af
      if (amount) {
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, "opacity(" + amount->to_string(ctx.c_options) + ")");
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      return SASS_MEMORY_NEW(Number, pstate, ARG("$color", Color)->a());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature opacify_sig = "opacify($color, $amount)";
Packit Service 7770af
    Signature fade_in_sig = "fade-in($color, $amount)";
Packit Service 7770af
    BUILT_IN(opacify)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr color = ARG("$color", Color);
Packit Service 7770af
      double amount = ARGR("$amount", Number, 0, 1)->value();
Packit Service 7770af
      double alpha = std::min(color->a() + amount, 1.0);
Packit Service 7770af
      return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                             pstate,
Packit Service 7770af
                             color->r(),
Packit Service 7770af
                             color->g(),
Packit Service 7770af
                             color->b(),
Packit Service 7770af
                             alpha);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature transparentize_sig = "transparentize($color, $amount)";
Packit Service 7770af
    Signature fade_out_sig = "fade-out($color, $amount)";
Packit Service 7770af
    BUILT_IN(transparentize)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr color = ARG("$color", Color);
Packit Service 7770af
      double amount = ARGR("$amount", Number, 0, 1)->value();
Packit Service 7770af
      double alpha = std::max(color->a() - amount, 0.0);
Packit Service 7770af
      return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                             pstate,
Packit Service 7770af
                             color->r(),
Packit Service 7770af
                             color->g(),
Packit Service 7770af
                             color->b(),
Packit Service 7770af
                             alpha);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    ////////////////////////
Packit Service 7770af
    // OTHER COLOR FUNCTIONS
Packit Service 7770af
    ////////////////////////
Packit Service 7770af
Packit Service 7770af
    Signature adjust_color_sig = "adjust-color($color, $red: false, $green: false, $blue: false, $hue: false, $saturation: false, $lightness: false, $alpha: false)";
Packit Service 7770af
    BUILT_IN(adjust_color)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr color = ARG("$color", Color);
Packit Service 7770af
      Number_Ptr r = Cast<Number>(env["$red"]);
Packit Service 7770af
      Number_Ptr g = Cast<Number>(env["$green"]);
Packit Service 7770af
      Number_Ptr b = Cast<Number>(env["$blue"]);
Packit Service 7770af
      Number_Ptr h = Cast<Number>(env["$hue"]);
Packit Service 7770af
      Number_Ptr s = Cast<Number>(env["$saturation"]);
Packit Service 7770af
      Number_Ptr l = Cast<Number>(env["$lightness"]);
Packit Service 7770af
      Number_Ptr a = Cast<Number>(env["$alpha"]);
Packit Service 7770af
Packit Service 7770af
      bool rgb = r || g || b;
Packit Service 7770af
      bool hsl = h || s || l;
Packit Service 7770af
Packit Service 7770af
      if (rgb && hsl) {
Packit Service 7770af
        error("Cannot specify HSL and RGB values for a color at the same time for `adjust-color'", pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (rgb) {
Packit Service 7770af
        double rr = r ? ARGR("$red",   Number, -255, 255)->value() : 0;
Packit Service 7770af
        double gg = g ? ARGR("$green", Number, -255, 255)->value() : 0;
Packit Service 7770af
        double bb = b ? ARGR("$blue",  Number, -255, 255)->value() : 0;
Packit Service 7770af
        double aa = a ? ARGR("$alpha", Number, -1, 1)->value() : 0;
Packit Service 7770af
        return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               color->r() + rr,
Packit Service 7770af
                               color->g() + gg,
Packit Service 7770af
                               color->b() + bb,
Packit Service 7770af
                               color->a() + aa);
Packit Service 7770af
      }
Packit Service 7770af
      if (hsl) {
Packit Service 7770af
        HSL hsl_struct = rgb_to_hsl(color->r(), color->g(), color->b());
Packit Service 7770af
        double ss = s ? ARGR("$saturation", Number, -100, 100)->value() : 0;
Packit Service 7770af
        double ll = l ? ARGR("$lightness",  Number, -100, 100)->value() : 0;
Packit Service 7770af
        double aa = a ? ARGR("$alpha",      Number, -1, 1)->value() : 0;
Packit Service 7770af
        return hsla_impl(hsl_struct.h + (h ? h->value() : 0),
Packit Service 7770af
                         hsl_struct.s + ss,
Packit Service 7770af
                         hsl_struct.l + ll,
Packit Service 7770af
                         color->a() + aa,
Packit Service 7770af
                         ctx,
Packit Service 7770af
                         pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (a) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               color->r(),
Packit Service 7770af
                               color->g(),
Packit Service 7770af
                               color->b(),
Packit Service 7770af
                               color->a() + (a ? a->value() : 0));
Packit Service 7770af
      }
Packit Service 7770af
      error("not enough arguments for `adjust-color'", pstate);
Packit Service 7770af
      // unreachable
Packit Service 7770af
      return color;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature scale_color_sig = "scale-color($color, $red: false, $green: false, $blue: false, $hue: false, $saturation: false, $lightness: false, $alpha: false)";
Packit Service 7770af
    BUILT_IN(scale_color)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr color = ARG("$color", Color);
Packit Service 7770af
      Number_Ptr r = Cast<Number>(env["$red"]);
Packit Service 7770af
      Number_Ptr g = Cast<Number>(env["$green"]);
Packit Service 7770af
      Number_Ptr b = Cast<Number>(env["$blue"]);
Packit Service 7770af
      Number_Ptr h = Cast<Number>(env["$hue"]);
Packit Service 7770af
      Number_Ptr s = Cast<Number>(env["$saturation"]);
Packit Service 7770af
      Number_Ptr l = Cast<Number>(env["$lightness"]);
Packit Service 7770af
      Number_Ptr a = Cast<Number>(env["$alpha"]);
Packit Service 7770af
Packit Service 7770af
      bool rgb = r || g || b;
Packit Service 7770af
      bool hsl = h || s || l;
Packit Service 7770af
Packit Service 7770af
      if (rgb && hsl) {
Packit Service 7770af
        error("Cannot specify HSL and RGB values for a color at the same time for `scale-color'", pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (rgb) {
Packit Service 7770af
        double rscale = (r ? ARGR("$red",   Number, -100.0, 100.0)->value() : 0.0) / 100.0;
Packit Service 7770af
        double gscale = (g ? ARGR("$green", Number, -100.0, 100.0)->value() : 0.0) / 100.0;
Packit Service 7770af
        double bscale = (b ? ARGR("$blue",  Number, -100.0, 100.0)->value() : 0.0) / 100.0;
Packit Service 7770af
        double ascale = (a ? ARGR("$alpha", Number, -100.0, 100.0)->value() : 0.0) / 100.0;
Packit Service 7770af
        return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               color->r() + rscale * (rscale > 0.0 ? 255 - color->r() : color->r()),
Packit Service 7770af
                               color->g() + gscale * (gscale > 0.0 ? 255 - color->g() : color->g()),
Packit Service 7770af
                               color->b() + bscale * (bscale > 0.0 ? 255 - color->b() : color->b()),
Packit Service 7770af
                               color->a() + ascale * (ascale > 0.0 ? 1.0 - color->a() : color->a()));
Packit Service 7770af
      }
Packit Service 7770af
      if (hsl) {
Packit Service 7770af
        double hscale = (h ? ARGR("$hue",        Number, -100.0, 100.0)->value() : 0.0) / 100.0;
Packit Service 7770af
        double sscale = (s ? ARGR("$saturation", Number, -100.0, 100.0)->value() : 0.0) / 100.0;
Packit Service 7770af
        double lscale = (l ? ARGR("$lightness",  Number, -100.0, 100.0)->value() : 0.0) / 100.0;
Packit Service 7770af
        double ascale = (a ? ARGR("$alpha",      Number, -100.0, 100.0)->value() : 0.0) / 100.0;
Packit Service 7770af
        HSL hsl_struct = rgb_to_hsl(color->r(), color->g(), color->b());
Packit Service 7770af
        hsl_struct.h += hscale * (hscale > 0.0 ? 360.0 - hsl_struct.h : hsl_struct.h);
Packit Service 7770af
        hsl_struct.s += sscale * (sscale > 0.0 ? 100.0 - hsl_struct.s : hsl_struct.s);
Packit Service 7770af
        hsl_struct.l += lscale * (lscale > 0.0 ? 100.0 - hsl_struct.l : hsl_struct.l);
Packit Service 7770af
        double alpha = color->a() + ascale * (ascale > 0.0 ? 1.0 - color->a() : color->a());
Packit Service 7770af
        return hsla_impl(hsl_struct.h, hsl_struct.s, hsl_struct.l, alpha, ctx, pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (a) {
Packit Service 7770af
        double ascale = (ARGR("$alpha", Number, -100.0, 100.0)->value()) / 100.0;
Packit Service 7770af
        return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               color->r(),
Packit Service 7770af
                               color->g(),
Packit Service 7770af
                               color->b(),
Packit Service 7770af
                               color->a() + ascale * (ascale > 0.0 ? 1.0 - color->a() : color->a()));
Packit Service 7770af
      }
Packit Service 7770af
      error("not enough arguments for `scale-color'", pstate);
Packit Service 7770af
      // unreachable
Packit Service 7770af
      return color;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature change_color_sig = "change-color($color, $red: false, $green: false, $blue: false, $hue: false, $saturation: false, $lightness: false, $alpha: false)";
Packit Service 7770af
    BUILT_IN(change_color)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr color = ARG("$color", Color);
Packit Service 7770af
      Number_Ptr r = Cast<Number>(env["$red"]);
Packit Service 7770af
      Number_Ptr g = Cast<Number>(env["$green"]);
Packit Service 7770af
      Number_Ptr b = Cast<Number>(env["$blue"]);
Packit Service 7770af
      Number_Ptr h = Cast<Number>(env["$hue"]);
Packit Service 7770af
      Number_Ptr s = Cast<Number>(env["$saturation"]);
Packit Service 7770af
      Number_Ptr l = Cast<Number>(env["$lightness"]);
Packit Service 7770af
      Number_Ptr a = Cast<Number>(env["$alpha"]);
Packit Service 7770af
Packit Service 7770af
      bool rgb = r || g || b;
Packit Service 7770af
      bool hsl = h || s || l;
Packit Service 7770af
Packit Service 7770af
      if (rgb && hsl) {
Packit Service 7770af
        error("Cannot specify HSL and RGB values for a color at the same time for `change-color'", pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (rgb) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               r ? ARGR("$red",   Number, 0, 255)->value() : color->r(),
Packit Service 7770af
                               g ? ARGR("$green", Number, 0, 255)->value() : color->g(),
Packit Service 7770af
                               b ? ARGR("$blue",  Number, 0, 255)->value() : color->b(),
Packit Service 7770af
                               a ? ARGR("$alpha", Number, 0, 255)->value() : color->a());
Packit Service 7770af
      }
Packit Service 7770af
      if (hsl) {
Packit Service 7770af
        HSL hsl_struct = rgb_to_hsl(color->r(), color->g(), color->b());
Packit Service 7770af
        if (h) hsl_struct.h = std::fmod(h->value(), 360.0);
Packit Service 7770af
        if (s) hsl_struct.s = ARGR("$saturation", Number, 0, 100)->value();
Packit Service 7770af
        if (l) hsl_struct.l = ARGR("$lightness",  Number, 0, 100)->value();
Packit Service 7770af
        double alpha = a ? ARGR("$alpha", Number, 0, 1.0)->value() : color->a();
Packit Service 7770af
        return hsla_impl(hsl_struct.h, hsl_struct.s, hsl_struct.l, alpha, ctx, pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (a) {
Packit Service 7770af
        double alpha = ARGR("$alpha", Number, 0, 1.0)->value();
Packit Service 7770af
        return SASS_MEMORY_NEW(Color,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               color->r(),
Packit Service 7770af
                               color->g(),
Packit Service 7770af
                               color->b(),
Packit Service 7770af
                               alpha);
Packit Service 7770af
      }
Packit Service 7770af
      error("not enough arguments for `change-color'", pstate);
Packit Service 7770af
      // unreachable
Packit Service 7770af
      return color;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    template <size_t range>
Packit Service 7770af
    static double cap_channel(double c) {
Packit Service 7770af
      if      (c > range) return range;
Packit Service 7770af
      else if (c < 0)     return 0;
Packit Service 7770af
      else                return c;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature ie_hex_str_sig = "ie-hex-str($color)";
Packit Service 7770af
    BUILT_IN(ie_hex_str)
Packit Service 7770af
    {
Packit Service 7770af
      Color_Ptr c = ARG("$color", Color);
Packit Service 7770af
      double r = cap_channel<0xff>(c->r());
Packit Service 7770af
      double g = cap_channel<0xff>(c->g());
Packit Service 7770af
      double b = cap_channel<0xff>(c->b());
Packit Service 7770af
      double a = cap_channel<1>   (c->a()) * 255;
Packit Service 7770af
Packit Service 7770af
      std::stringstream ss;
Packit Service 7770af
      ss << '#' << std::setw(2) << std::setfill('0');
Packit Service 7770af
      ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(a, ctx.c_options.precision));
Packit Service 7770af
      ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(r, ctx.c_options.precision));
Packit Service 7770af
      ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(g, ctx.c_options.precision));
Packit Service 7770af
      ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(b, ctx.c_options.precision));
Packit Service 7770af
Packit Service 7770af
      std::string result(ss.str());
Packit Service 7770af
      for (size_t i = 0, L = result.length(); i < L; ++i) {
Packit Service 7770af
        result[i] = std::toupper(result[i]);
Packit Service 7770af
      }
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Quoted, pstate, result);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    ///////////////////
Packit Service 7770af
    // STRING FUNCTIONS
Packit Service 7770af
    ///////////////////
Packit Service 7770af
Packit Service 7770af
    Signature unquote_sig = "unquote($string)";
Packit Service 7770af
    BUILT_IN(sass_unquote)
Packit Service 7770af
    {
Packit Service 7770af
      AST_Node_Obj arg = env["$string"];
Packit Service 7770af
      if (String_Quoted_Ptr string_quoted = Cast<String_Quoted>(arg)) {
Packit Service 7770af
        String_Constant_Ptr result = SASS_MEMORY_NEW(String_Constant, pstate, string_quoted->value());
Packit Service 7770af
        // remember if the string was quoted (color tokens)
Packit Service 7770af
        result->is_delayed(true); // delay colors
Packit Service 7770af
        return result;
Packit Service 7770af
      }
Packit Service 7770af
      else if (String_Constant_Ptr str = Cast<String_Constant>(arg)) {
Packit Service 7770af
        return str;
Packit Service 7770af
      }
Packit Service 7770af
      else if (Expression_Ptr ex = Cast<Expression>(arg)) {
Packit Service 7770af
        Sass_Output_Style oldstyle = ctx.c_options.output_style;
Packit Service 7770af
        ctx.c_options.output_style = SASS_STYLE_NESTED;
Packit Service 7770af
        std::string val(arg->to_string(ctx.c_options));
Packit Service 7770af
        val = Cast<Null>(arg) ? "null" : val;
Packit Service 7770af
        ctx.c_options.output_style = oldstyle;
Packit Service 7770af
Packit Service 7770af
        deprecated_function("Passing " + val + ", a non-string value, to unquote()", pstate);
Packit Service 7770af
        return ex;
Packit Service 7770af
      }
Packit Service 7770af
      throw std::runtime_error("Invalid Data Type for unquote");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature quote_sig = "quote($string)";
Packit Service 7770af
    BUILT_IN(sass_quote)
Packit Service 7770af
    {
Packit Service 7770af
      AST_Node_Obj arg = env["$string"];
Packit Service 7770af
      // only set quote mark to true if already a string
Packit Service 7770af
      if (String_Quoted_Ptr qstr = Cast<String_Quoted>(arg)) {
Packit Service 7770af
        qstr->quote_mark('*');
Packit Service 7770af
        return qstr;
Packit Service 7770af
      }
Packit Service 7770af
      // all other nodes must be converted to a string node
Packit Service 7770af
      std::string str(quote(arg->to_string(ctx.c_options), String_Constant::double_quote()));
Packit Service 7770af
      String_Quoted_Ptr result = SASS_MEMORY_NEW(String_Quoted, pstate, str);
Packit Service 7770af
      result->quote_mark('*');
Packit Service 7770af
      return result;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
    Signature str_length_sig = "str-length($string)";
Packit Service 7770af
    BUILT_IN(str_length)
Packit Service 7770af
    {
Packit Service 7770af
      size_t len = std::string::npos;
Packit Service 7770af
      try {
Packit Service 7770af
        String_Constant_Ptr s = ARG("$string", String_Constant);
Packit Service 7770af
        len = UTF_8::code_point_count(s->value(), 0, s->value().size());
Packit Service 7770af
Packit Service 7770af
      }
Packit Service 7770af
      // handle any invalid utf8 errors
Packit Service 7770af
      // other errors will be re-thrown
Packit Service 7770af
      catch (...) { handle_utf8_error(pstate, backtrace); }
Packit Service 7770af
      // return something even if we had an error (-1)
Packit Service 7770af
      return SASS_MEMORY_NEW(Number, pstate, (double)len);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature str_insert_sig = "str-insert($string, $insert, $index)";
Packit Service 7770af
    BUILT_IN(str_insert)
Packit Service 7770af
    {
Packit Service 7770af
      std::string str;
Packit Service 7770af
      try {
Packit Service 7770af
        String_Constant_Ptr s = ARG("$string", String_Constant);
Packit Service 7770af
        str = s->value();
Packit Service 7770af
        str = unquote(str);
Packit Service 7770af
        String_Constant_Ptr i = ARG("$insert", String_Constant);
Packit Service 7770af
        std::string ins = i->value();
Packit Service 7770af
        ins = unquote(ins);
Packit Service 7770af
        Number_Ptr ind = ARG("$index", Number);
Packit Service 7770af
        double index = ind->value();
Packit Service 7770af
        size_t len = UTF_8::code_point_count(str, 0, str.size());
Packit Service 7770af
Packit Service 7770af
        if (index > 0 && index <= len) {
Packit Service 7770af
          // positive and within string length
Packit Service 7770af
          str.insert(UTF_8::offset_at_position(str, static_cast<size_t>(index) - 1), ins);
Packit Service 7770af
        }
Packit Service 7770af
        else if (index > len) {
Packit Service 7770af
          // positive and past string length
Packit Service 7770af
          str += ins;
Packit Service 7770af
        }
Packit Service 7770af
        else if (index == 0) {
Packit Service 7770af
          str = ins + str;
Packit Service 7770af
        }
Packit Service 7770af
        else if (std::abs(index) <= len) {
Packit Service 7770af
          // negative and within string length
Packit Service 7770af
          index += len + 1;
Packit Service 7770af
          str.insert(UTF_8::offset_at_position(str, static_cast<size_t>(index)), ins);
Packit Service 7770af
        }
Packit Service 7770af
        else {
Packit Service 7770af
          // negative and past string length
Packit Service 7770af
          str = ins + str;
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
        if (String_Quoted_Ptr ss = Cast<String_Quoted>(s)) {
Packit Service 7770af
          if (ss->quote_mark()) str = quote(str);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      // handle any invalid utf8 errors
Packit Service 7770af
      // other errors will be re-thrown
Packit Service 7770af
      catch (...) { handle_utf8_error(pstate, backtrace); }
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Quoted, pstate, str);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature str_index_sig = "str-index($string, $substring)";
Packit Service 7770af
    BUILT_IN(str_index)
Packit Service 7770af
    {
Packit Service 7770af
      size_t index = std::string::npos;
Packit Service 7770af
      try {
Packit Service 7770af
        String_Constant_Ptr s = ARG("$string", String_Constant);
Packit Service 7770af
        String_Constant_Ptr t = ARG("$substring", String_Constant);
Packit Service 7770af
        std::string str = s->value();
Packit Service 7770af
        str = unquote(str);
Packit Service 7770af
        std::string substr = t->value();
Packit Service 7770af
        substr = unquote(substr);
Packit Service 7770af
Packit Service 7770af
        size_t c_index = str.find(substr);
Packit Service 7770af
        if(c_index == std::string::npos) {
Packit Service 7770af
          return SASS_MEMORY_NEW(Null, pstate);
Packit Service 7770af
        }
Packit Service 7770af
        index = UTF_8::code_point_count(str, 0, c_index) + 1;
Packit Service 7770af
      }
Packit Service 7770af
      // handle any invalid utf8 errors
Packit Service 7770af
      // other errors will be re-thrown
Packit Service 7770af
      catch (...) { handle_utf8_error(pstate, backtrace); }
Packit Service 7770af
      // return something even if we had an error (-1)
Packit Service 7770af
      return SASS_MEMORY_NEW(Number, pstate, (double)index);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature str_slice_sig = "str-slice($string, $start-at, $end-at:-1)";
Packit Service 7770af
    BUILT_IN(str_slice)
Packit Service 7770af
    {
Packit Service 7770af
      std::string newstr;
Packit Service 7770af
      try {
Packit Service 7770af
        String_Constant_Ptr s = ARG("$string", String_Constant);
Packit Service 7770af
        double start_at = ARG("$start-at", Number)->value();
Packit Service 7770af
        double end_at = ARG("$end-at", Number)->value();
Packit Service 7770af
        String_Quoted_Ptr ss = Cast<String_Quoted>(s);
Packit Service 7770af
Packit Service 7770af
        std::string str = unquote(s->value());
Packit Service 7770af
Packit Service 7770af
        size_t size = utf8::distance(str.begin(), str.end());
Packit Service 7770af
Packit Service 7770af
        if (!Cast<Number>(env["$end-at"])) {
Packit Service 7770af
          end_at = -1;
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
        if (end_at == 0 || (end_at + size) < 0) {
Packit Service 7770af
          if (ss && ss->quote_mark()) newstr = quote("");
Packit Service 7770af
          return SASS_MEMORY_NEW(String_Quoted, pstate, newstr);
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
        if (end_at < 0) {
Packit Service 7770af
          end_at += size + 1;
Packit Service 7770af
          if (end_at == 0) end_at = 1;
Packit Service 7770af
        }
Packit Service 7770af
        if (end_at > size) { end_at = (double)size; }
Packit Service 7770af
        if (start_at < 0) {
Packit Service 7770af
          start_at += size + 1;
Packit Service 7770af
          if (start_at < 0)  start_at = 0;
Packit Service 7770af
        }
Packit Service 7770af
        else if (start_at == 0) { ++ start_at; }
Packit Service 7770af
Packit Service 7770af
        if (start_at <= end_at)
Packit Service 7770af
        {
Packit Service 7770af
          std::string::iterator start = str.begin();
Packit Service 7770af
          utf8::advance(start, start_at - 1, str.end());
Packit Service 7770af
          std::string::iterator end = start;
Packit Service 7770af
          utf8::advance(end, end_at - start_at + 1, str.end());
Packit Service 7770af
          newstr = std::string(start, end);
Packit Service 7770af
        }
Packit Service 7770af
        if (ss) {
Packit Service 7770af
          if(ss->quote_mark()) newstr = quote(newstr);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      // handle any invalid utf8 errors
Packit Service 7770af
      // other errors will be re-thrown
Packit Service 7770af
      catch (...) { handle_utf8_error(pstate, backtrace); }
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Quoted, pstate, newstr);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature to_upper_case_sig = "to-upper-case($string)";
Packit Service 7770af
    BUILT_IN(to_upper_case)
Packit Service 7770af
    {
Packit Service 7770af
      String_Constant_Ptr s = ARG("$string", String_Constant);
Packit Service 7770af
      std::string str = s->value();
Packit Service 7770af
Packit Service 7770af
      for (size_t i = 0, L = str.length(); i < L; ++i) {
Packit Service 7770af
        if (Sass::Util::isAscii(str[i])) {
Packit Service 7770af
          str[i] = std::toupper(str[i]);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      if (String_Quoted_Ptr ss = Cast<String_Quoted>(s)) {
Packit Service 7770af
        String_Quoted_Ptr cpy = SASS_MEMORY_COPY(ss);
Packit Service 7770af
        cpy->value(str);
Packit Service 7770af
        return cpy;
Packit Service 7770af
      } else {
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, str);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature to_lower_case_sig = "to-lower-case($string)";
Packit Service 7770af
    BUILT_IN(to_lower_case)
Packit Service 7770af
    {
Packit Service 7770af
      String_Constant_Ptr s = ARG("$string", String_Constant);
Packit Service 7770af
      std::string str = s->value();
Packit Service 7770af
Packit Service 7770af
      for (size_t i = 0, L = str.length(); i < L; ++i) {
Packit Service 7770af
        if (Sass::Util::isAscii(str[i])) {
Packit Service 7770af
          str[i] = std::tolower(str[i]);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      if (String_Quoted_Ptr ss = Cast<String_Quoted>(s)) {
Packit Service 7770af
        String_Quoted_Ptr cpy = SASS_MEMORY_COPY(ss);
Packit Service 7770af
        cpy->value(str);
Packit Service 7770af
        return cpy;
Packit Service 7770af
      } else {
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, str);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    ///////////////////
Packit Service 7770af
    // NUMBER FUNCTIONS
Packit Service 7770af
    ///////////////////
Packit Service 7770af
Packit Service 7770af
    Signature percentage_sig = "percentage($number)";
Packit Service 7770af
    BUILT_IN(percentage)
Packit Service 7770af
    {
Packit Service 7770af
      Number_Ptr n = ARG("$number", Number);
Packit Service 7770af
      if (!n->is_unitless()) error("argument $number of `" + std::string(sig) + "` must be unitless", pstate);
Packit Service 7770af
      return SASS_MEMORY_NEW(Number, pstate, n->value() * 100, "%");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature round_sig = "round($number)";
Packit Service 7770af
    BUILT_IN(round)
Packit Service 7770af
    {
Packit Service 7770af
      Number_Ptr n = ARG("$number", Number);
Packit Service 7770af
      Number_Ptr r = SASS_MEMORY_COPY(n);
Packit Service 7770af
      r->pstate(pstate);
Packit Service 7770af
      r->value(Sass::round(r->value(), ctx.c_options.precision));
Packit Service 7770af
      return r;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature ceil_sig = "ceil($number)";
Packit Service 7770af
    BUILT_IN(ceil)
Packit Service 7770af
    {
Packit Service 7770af
      Number_Ptr n = ARG("$number", Number);
Packit Service 7770af
      Number_Ptr r = SASS_MEMORY_COPY(n);
Packit Service 7770af
      r->pstate(pstate);
Packit Service 7770af
      r->value(std::ceil(r->value()));
Packit Service 7770af
      return r;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature floor_sig = "floor($number)";
Packit Service 7770af
    BUILT_IN(floor)
Packit Service 7770af
    {
Packit Service 7770af
      Number_Ptr n = ARG("$number", Number);
Packit Service 7770af
      Number_Ptr r = SASS_MEMORY_COPY(n);
Packit Service 7770af
      r->pstate(pstate);
Packit Service 7770af
      r->value(std::floor(r->value()));
Packit Service 7770af
      return r;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature abs_sig = "abs($number)";
Packit Service 7770af
    BUILT_IN(abs)
Packit Service 7770af
    {
Packit Service 7770af
      Number_Ptr n = ARG("$number", Number);
Packit Service 7770af
      Number_Ptr r = SASS_MEMORY_COPY(n);
Packit Service 7770af
      r->pstate(pstate);
Packit Service 7770af
      r->value(std::abs(r->value()));
Packit Service 7770af
      return r;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature min_sig = "min($numbers...)";
Packit Service 7770af
    BUILT_IN(min)
Packit Service 7770af
    {
Packit Service 7770af
      List_Ptr arglist = ARG("$numbers", List);
Packit Service 7770af
      Number_Obj least = NULL;
Packit Service 7770af
      for (size_t i = 0, L = arglist->length(); i < L; ++i) {
Packit Service 7770af
        Expression_Obj val = arglist->value_at_index(i);
Packit Service 7770af
        Number_Obj xi = Cast<Number>(val);
Packit Service 7770af
        if (!xi) {
Packit Service 7770af
          error("\"" + val->to_string(ctx.c_options) + "\" is not a number for `min'", pstate);
Packit Service 7770af
        }
Packit Service 7770af
        if (least) {
Packit Service 7770af
          if (*xi < *least) least = xi;
Packit Service 7770af
        } else least = xi;
Packit Service 7770af
      }
Packit Service 7770af
      return least.detach();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature max_sig = "max($numbers...)";
Packit Service 7770af
    BUILT_IN(max)
Packit Service 7770af
    {
Packit Service 7770af
      List_Ptr arglist = ARG("$numbers", List);
Packit Service 7770af
      Number_Obj greatest = NULL;
Packit Service 7770af
      for (size_t i = 0, L = arglist->length(); i < L; ++i) {
Packit Service 7770af
        Expression_Obj val = arglist->value_at_index(i);
Packit Service 7770af
        Number_Obj xi = Cast<Number>(val);
Packit Service 7770af
        if (!xi) {
Packit Service 7770af
          error("\"" + val->to_string(ctx.c_options) + "\" is not a number for `max'", pstate);
Packit Service 7770af
        }
Packit Service 7770af
        if (greatest) {
Packit Service 7770af
          if (*greatest < *xi) greatest = xi;
Packit Service 7770af
        } else greatest = xi;
Packit Service 7770af
      }
Packit Service 7770af
      return greatest.detach();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature random_sig = "random($limit:false)";
Packit Service 7770af
    BUILT_IN(random)
Packit Service 7770af
    {
Packit Service 7770af
      AST_Node_Obj arg = env["$limit"];
Packit Service 7770af
      Value_Ptr v = Cast<Value>(arg);
Packit Service 7770af
      Number_Ptr l = Cast<Number>(arg);
Packit Service 7770af
      Boolean_Ptr b = Cast<Boolean>(arg);
Packit Service 7770af
      if (l) {
Packit Service 7770af
        double lv = l->value();
Packit Service 7770af
        if (lv < 1) {
Packit Service 7770af
          stringstream err;
Packit Service 7770af
          err << "$limit " << lv << " must be greater than or equal to 1 for `random'";
Packit Service 7770af
          error(err.str(), pstate);
Packit Service 7770af
        }
Packit Service 7770af
        bool eq_int = std::fabs(trunc(lv) - lv) < NUMBER_EPSILON;
Packit Service 7770af
        if (!eq_int) {
Packit Service 7770af
          stringstream err;
Packit Service 7770af
          err << "Expected $limit to be an integer but got " << lv << " for `random'";
Packit Service 7770af
          error(err.str(), pstate);
Packit Service 7770af
        }
Packit Service 7770af
        std::uniform_real_distribution<> distributor(1, lv + 1);
Packit Service 7770af
        uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
Packit Service 7770af
        return SASS_MEMORY_NEW(Number, pstate, (double)distributed);
Packit Service 7770af
      }
Packit Service 7770af
      else if (b) {
Packit Service 7770af
        std::uniform_real_distribution<> distributor(0, 1);
Packit Service 7770af
        double distributed = static_cast<double>(distributor(rand));
Packit Service 7770af
        return SASS_MEMORY_NEW(Number, pstate, distributed);
Packit Service 7770af
      } else if (v) {
Packit Service 7770af
        throw Exception::InvalidArgumentType(pstate, "random", "$limit", "number", v);
Packit Service 7770af
      } else {
Packit Service 7770af
        throw Exception::InvalidArgumentType(pstate, "random", "$limit", "number");
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    /////////////////
Packit Service 7770af
    // LIST FUNCTIONS
Packit Service 7770af
    /////////////////
Packit Service 7770af
Packit Service 7770af
    Signature length_sig = "length($list)";
Packit Service 7770af
    BUILT_IN(length)
Packit Service 7770af
    {
Packit Service 7770af
      if (Selector_List_Ptr sl = Cast<Selector_List>(env["$list"])) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Number, pstate, (double)sl->length());
Packit Service 7770af
      }
Packit Service 7770af
      Expression_Ptr v = ARG("$list", Expression);
Packit Service 7770af
      if (v->concrete_type() == Expression::MAP) {
Packit Service 7770af
        Map_Ptr map = Cast<Map>(env["$list"]);
Packit Service 7770af
        return SASS_MEMORY_NEW(Number, pstate, (double)(map ? map->length() : 1));
Packit Service 7770af
      }
Packit Service 7770af
      if (v->concrete_type() == Expression::SELECTOR) {
Packit Service 7770af
        if (Compound_Selector_Ptr h = Cast<Compound_Selector>(v)) {
Packit Service 7770af
          return SASS_MEMORY_NEW(Number, pstate, (double)h->length());
Packit Service 7770af
        } else if (Selector_List_Ptr ls = Cast<Selector_List>(v)) {
Packit Service 7770af
          return SASS_MEMORY_NEW(Number, pstate, (double)ls->length());
Packit Service 7770af
        } else {
Packit Service 7770af
          return SASS_MEMORY_NEW(Number, pstate, 1);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      List_Ptr list = Cast<List>(env["$list"]);
Packit Service 7770af
      return SASS_MEMORY_NEW(Number,
Packit Service 7770af
                             pstate,
Packit Service 7770af
                             (double)(list ? list->size() : 1));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature nth_sig = "nth($list, $n)";
Packit Service 7770af
    BUILT_IN(nth)
Packit Service 7770af
    {
Packit Service 7770af
      Number_Ptr n = ARG("$n", Number);
Packit Service 7770af
      Map_Ptr m = Cast<Map>(env["$list"]);
Packit Service 7770af
      if (Selector_List_Ptr sl = Cast<Selector_List>(env["$list"])) {
Packit Service 7770af
        size_t len = m ? m->length() : sl->length();
Packit Service 7770af
        bool empty = m ? m->empty() : sl->empty();
Packit Service 7770af
        if (empty) error("argument `$list` of `" + std::string(sig) + "` must not be empty", pstate);
Packit Service 7770af
        double index = std::floor(n->value() < 0 ? len + n->value() : n->value() - 1);
Packit Service 7770af
        if (index < 0 || index > len - 1) error("index out of bounds for `" + std::string(sig) + "`", pstate);
Packit Service 7770af
        // return (*sl)[static_cast<int>(index)];
Packit Service 7770af
        Listize listize;
Packit Service 7770af
        return (*sl)[static_cast<int>(index)]->perform(&listize);
Packit Service 7770af
      }
Packit Service 7770af
      List_Obj l = Cast<List>(env["$list"]);
Packit Service 7770af
      if (n->value() == 0) error("argument `$n` of `" + std::string(sig) + "` must be non-zero", pstate);
Packit Service 7770af
      // if the argument isn't a list, then wrap it in a singleton list
Packit Service 7770af
      if (!m && !l) {
Packit Service 7770af
        l = SASS_MEMORY_NEW(List, pstate, 1);
Packit Service 7770af
        l->append(ARG("$list", Expression));
Packit Service 7770af
      }
Packit Service 7770af
      size_t len = m ? m->length() : l->length();
Packit Service 7770af
      bool empty = m ? m->empty() : l->empty();
Packit Service 7770af
      if (empty) error("argument `$list` of `" + std::string(sig) + "` must not be empty", pstate);
Packit Service 7770af
      double index = std::floor(n->value() < 0 ? len + n->value() : n->value() - 1);
Packit Service 7770af
      if (index < 0 || index > len - 1) error("index out of bounds for `" + std::string(sig) + "`", pstate);
Packit Service 7770af
Packit Service 7770af
      if (m) {
Packit Service 7770af
        l = SASS_MEMORY_NEW(List, pstate, 1);
Packit Service 7770af
        l->append(m->keys()[static_cast<unsigned int>(index)]);
Packit Service 7770af
        l->append(m->at(m->keys()[static_cast<unsigned int>(index)]));
Packit Service 7770af
        return l.detach();
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        Expression_Obj rv = l->value_at_index(static_cast<int>(index));
Packit Service 7770af
        rv->set_delayed(false);
Packit Service 7770af
        return rv.detach();
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature set_nth_sig = "set-nth($list, $n, $value)";
Packit Service 7770af
    BUILT_IN(set_nth)
Packit Service 7770af
    {
Packit Service 7770af
      Map_Obj m = Cast<Map>(env["$list"]);
Packit Service 7770af
      List_Obj l = Cast<List>(env["$list"]);
Packit Service 7770af
      Number_Obj n = ARG("$n", Number);
Packit Service 7770af
      Expression_Obj v = ARG("$value", Expression);
Packit Service 7770af
      if (!l) {
Packit Service 7770af
        l = SASS_MEMORY_NEW(List, pstate, 1);
Packit Service 7770af
        l->append(ARG("$list", Expression));
Packit Service 7770af
      }
Packit Service 7770af
      if (m) {
Packit Service 7770af
        l = m->to_list(pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (l->empty()) error("argument `$list` of `" + std::string(sig) + "` must not be empty", pstate);
Packit Service 7770af
      double index = std::floor(n->value() < 0 ? l->length() + n->value() : n->value() - 1);
Packit Service 7770af
      if (index < 0 || index > l->length() - 1) error("index out of bounds for `" + std::string(sig) + "`", pstate);
Packit Service 7770af
      List_Ptr result = SASS_MEMORY_NEW(List, pstate, l->length(), l->separator());
Packit Service 7770af
      for (size_t i = 0, L = l->length(); i < L; ++i) {
Packit Service 7770af
        result->append(((i == index) ? v : (*l)[i]));
Packit Service 7770af
      }
Packit Service 7770af
      return result;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature index_sig = "index($list, $value)";
Packit Service 7770af
    BUILT_IN(index)
Packit Service 7770af
    {
Packit Service 7770af
      Map_Obj m = Cast<Map>(env["$list"]);
Packit Service 7770af
      List_Obj l = Cast<List>(env["$list"]);
Packit Service 7770af
      Expression_Obj v = ARG("$value", Expression);
Packit Service 7770af
      if (!l) {
Packit Service 7770af
        l = SASS_MEMORY_NEW(List, pstate, 1);
Packit Service 7770af
        l->append(ARG("$list", Expression));
Packit Service 7770af
      }
Packit Service 7770af
      if (m) {
Packit Service 7770af
        l = m->to_list(pstate);
Packit Service 7770af
      }
Packit Service 7770af
      for (size_t i = 0, L = l->length(); i < L; ++i) {
Packit Service 7770af
        if (Eval::eq(l->value_at_index(i), v)) return SASS_MEMORY_NEW(Number, pstate, (double)(i+1));
Packit Service 7770af
      }
Packit Service 7770af
      return SASS_MEMORY_NEW(Null, pstate);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature join_sig = "join($list1, $list2, $separator: auto)";
Packit Service 7770af
    BUILT_IN(join)
Packit Service 7770af
    {
Packit Service 7770af
      Map_Obj m1 = Cast<Map>(env["$list1"]);
Packit Service 7770af
      Map_Obj m2 = Cast<Map>(env["$list2"]);
Packit Service 7770af
      List_Obj l1 = Cast<List>(env["$list1"]);
Packit Service 7770af
      List_Obj l2 = Cast<List>(env["$list2"]);
Packit Service 7770af
      String_Constant_Obj sep = ARG("$separator", String_Constant);
Packit Service 7770af
      enum Sass_Separator sep_val = (l1 ? l1->separator() : SASS_SPACE);
Packit Service 7770af
      if (!l1) {
Packit Service 7770af
        l1 = SASS_MEMORY_NEW(List, pstate, 1);
Packit Service 7770af
        l1->append(ARG("$list1", Expression));
Packit Service 7770af
        sep_val = (l2 ? l2->separator() : SASS_SPACE);
Packit Service 7770af
      }
Packit Service 7770af
      if (!l2) {
Packit Service 7770af
        l2 = SASS_MEMORY_NEW(List, pstate, 1);
Packit Service 7770af
        l2->append(ARG("$list2", Expression));
Packit Service 7770af
      }
Packit Service 7770af
      if (m1) {
Packit Service 7770af
        l1 = m1->to_list(pstate);
Packit Service 7770af
        sep_val = SASS_COMMA;
Packit Service 7770af
      }
Packit Service 7770af
      if (m2) {
Packit Service 7770af
        l2 = m2->to_list(pstate);
Packit Service 7770af
      }
Packit Service 7770af
      size_t len = l1->length() + l2->length();
Packit Service 7770af
      std::string sep_str = unquote(sep->value());
Packit Service 7770af
      if (sep_str == "space") sep_val = SASS_SPACE;
Packit Service 7770af
      else if (sep_str == "comma") sep_val = SASS_COMMA;
Packit Service 7770af
      else if (sep_str != "auto") error("argument `$separator` of `" + std::string(sig) + "` must be `space`, `comma`, or `auto`", pstate);
Packit Service 7770af
      List_Obj result = SASS_MEMORY_NEW(List, pstate, len, sep_val);
Packit Service 7770af
      result->concat(l1);
Packit Service 7770af
      result->concat(l2);
Packit Service 7770af
      return result.detach();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature append_sig = "append($list, $val, $separator: auto)";
Packit Service 7770af
    BUILT_IN(append)
Packit Service 7770af
    {
Packit Service 7770af
      Map_Obj m = Cast<Map>(env["$list"]);
Packit Service 7770af
      List_Obj l = Cast<List>(env["$list"]);
Packit Service 7770af
      Expression_Obj v = ARG("$val", Expression);
Packit Service 7770af
      if (Selector_List_Ptr sl = Cast<Selector_List>(env["$list"])) {
Packit Service 7770af
        Listize listize;
Packit Service 7770af
        l = Cast<List>(sl->perform(&listize));
Packit Service 7770af
      }
Packit Service 7770af
      String_Constant_Obj sep = ARG("$separator", String_Constant);
Packit Service 7770af
      if (!l) {
Packit Service 7770af
        l = SASS_MEMORY_NEW(List, pstate, 1);
Packit Service 7770af
        l->append(ARG("$list", Expression));
Packit Service 7770af
      }
Packit Service 7770af
      if (m) {
Packit Service 7770af
        l = m->to_list(pstate);
Packit Service 7770af
      }
Packit Service 7770af
      List_Ptr result = SASS_MEMORY_COPY(l);
Packit Service 7770af
      std::string sep_str(unquote(sep->value()));
Packit Service 7770af
      if (sep_str != "auto") { // check default first
Packit Service 7770af
        if (sep_str == "space") result->separator(SASS_SPACE);
Packit Service 7770af
        else if (sep_str == "comma") result->separator(SASS_COMMA);
Packit Service 7770af
        else error("argument `$separator` of `" + std::string(sig) + "` must be `space`, `comma`, or `auto`", pstate);
Packit Service 7770af
      }
Packit Service 7770af
      if (l->is_arglist()) {
Packit Service 7770af
        result->append(SASS_MEMORY_NEW(Argument,
Packit Service 7770af
                                       v->pstate(),
Packit Service 7770af
                                       v,
Packit Service 7770af
                                       "",
Packit Service 7770af
                                       false,
Packit Service 7770af
                                       false));
Packit Service 7770af
Packit Service 7770af
      } else {
Packit Service 7770af
        result->append(v);
Packit Service 7770af
      }
Packit Service 7770af
      return result;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature zip_sig = "zip($lists...)";
Packit Service 7770af
    BUILT_IN(zip)
Packit Service 7770af
    {
Packit Service 7770af
      List_Obj arglist = SASS_MEMORY_COPY(ARG("$lists", List));
Packit Service 7770af
      size_t shortest = 0;
Packit Service 7770af
      for (size_t i = 0, L = arglist->length(); i < L; ++i) {
Packit Service 7770af
        List_Obj ith = Cast<List>(arglist->value_at_index(i));
Packit Service 7770af
        Map_Obj mith = Cast<Map>(arglist->value_at_index(i));
Packit Service 7770af
        if (!ith) {
Packit Service 7770af
          if (mith) {
Packit Service 7770af
            ith = mith->to_list(pstate);
Packit Service 7770af
          } else {
Packit Service 7770af
            ith = SASS_MEMORY_NEW(List, pstate, 1);
Packit Service 7770af
            ith->append(arglist->value_at_index(i));
Packit Service 7770af
          }
Packit Service 7770af
          if (arglist->is_arglist()) {
Packit Service 7770af
            Argument_Obj arg = (Argument_Ptr)(arglist->at(i).ptr()); // XXX
Packit Service 7770af
            arg->value(ith);
Packit Service 7770af
          } else {
Packit Service 7770af
            (*arglist)[i] = ith;
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
        shortest = (i ? std::min(shortest, ith->length()) : ith->length());
Packit Service 7770af
      }
Packit Service 7770af
      List_Ptr zippers = SASS_MEMORY_NEW(List, pstate, shortest, SASS_COMMA);
Packit Service 7770af
      size_t L = arglist->length();
Packit Service 7770af
      for (size_t i = 0; i < shortest; ++i) {
Packit Service 7770af
        List_Ptr zipper = SASS_MEMORY_NEW(List, pstate, L);
Packit Service 7770af
        for (size_t j = 0; j < L; ++j) {
Packit Service 7770af
          zipper->append(Cast<List>(arglist->value_at_index(j))->at(i));
Packit Service 7770af
        }
Packit Service 7770af
        zippers->append(zipper);
Packit Service 7770af
      }
Packit Service 7770af
      return zippers;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature list_separator_sig = "list_separator($list)";
Packit Service 7770af
    BUILT_IN(list_separator)
Packit Service 7770af
    {
Packit Service 7770af
      List_Obj l = Cast<List>(env["$list"]);
Packit Service 7770af
      if (!l) {
Packit Service 7770af
        l = SASS_MEMORY_NEW(List, pstate, 1);
Packit Service 7770af
        l->append(ARG("$list", Expression));
Packit Service 7770af
      }
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Quoted,
Packit Service 7770af
                               pstate,
Packit Service 7770af
                               l->separator() == SASS_COMMA ? "comma" : "space");
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    /////////////////
Packit Service 7770af
    // MAP FUNCTIONS
Packit Service 7770af
    /////////////////
Packit Service 7770af
Packit Service 7770af
    Signature map_get_sig = "map-get($map, $key)";
Packit Service 7770af
    BUILT_IN(map_get)
Packit Service 7770af
    {
Packit Service 7770af
      // leaks for "map-get((), foo)" if not Obj
Packit Service 7770af
      // investigate why this is (unexpected)
Packit Service 7770af
      Map_Obj m = ARGM("$map", Map, ctx);
Packit Service 7770af
      Expression_Obj v = ARG("$key", Expression);
Packit Service 7770af
      try {
Packit Service 7770af
        Expression_Obj val = m->at(v);
Packit Service 7770af
        if (!val) return SASS_MEMORY_NEW(Null, pstate);
Packit Service 7770af
        val->set_delayed(false);
Packit Service 7770af
        return val.detach();
Packit Service 7770af
      } catch (const std::out_of_range&) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Null, pstate);
Packit Service 7770af
      }
Packit Service 7770af
      catch (...) { throw; }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature map_has_key_sig = "map-has-key($map, $key)";
Packit Service 7770af
    BUILT_IN(map_has_key)
Packit Service 7770af
    {
Packit Service 7770af
      Map_Obj m = ARGM("$map", Map, ctx);
Packit Service 7770af
      Expression_Obj v = ARG("$key", Expression);
Packit Service 7770af
      return SASS_MEMORY_NEW(Boolean, pstate, m->has(v));
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature map_keys_sig = "map-keys($map)";
Packit Service 7770af
    BUILT_IN(map_keys)
Packit Service 7770af
    {
Packit Service 7770af
      Map_Obj m = ARGM("$map", Map, ctx);
Packit Service 7770af
      List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA);
Packit Service 7770af
      for ( auto key : m->keys()) {
Packit Service 7770af
        result->append(key);
Packit Service 7770af
      }
Packit Service 7770af
      return result;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature map_values_sig = "map-values($map)";
Packit Service 7770af
    BUILT_IN(map_values)
Packit Service 7770af
    {
Packit Service 7770af
      Map_Obj m = ARGM("$map", Map, ctx);
Packit Service 7770af
      List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA);
Packit Service 7770af
      for ( auto key : m->keys()) {
Packit Service 7770af
        result->append(m->at(key));
Packit Service 7770af
      }
Packit Service 7770af
      return result;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature map_merge_sig = "map-merge($map1, $map2)";
Packit Service 7770af
    BUILT_IN(map_merge)
Packit Service 7770af
    {
Packit Service 7770af
      Map_Obj m1 = ARGM("$map1", Map, ctx);
Packit Service 7770af
      Map_Obj m2 = ARGM("$map2", Map, ctx);
Packit Service 7770af
Packit Service 7770af
      size_t len = m1->length() + m2->length();
Packit Service 7770af
      Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, len);
Packit Service 7770af
      // concat not implemented for maps
Packit Service 7770af
      *result += m1;
Packit Service 7770af
      *result += m2;
Packit Service 7770af
      return result;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature map_remove_sig = "map-remove($map, $keys...)";
Packit Service 7770af
    BUILT_IN(map_remove)
Packit Service 7770af
    {
Packit Service 7770af
      bool remove;
Packit Service 7770af
      Map_Obj m = ARGM("$map", Map, ctx);
Packit Service 7770af
      List_Obj arglist = ARG("$keys", List);
Packit Service 7770af
      Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, 1);
Packit Service 7770af
      for (auto key : m->keys()) {
Packit Service 7770af
        remove = false;
Packit Service 7770af
        for (size_t j = 0, K = arglist->length(); j < K && !remove; ++j) {
Packit Service 7770af
          remove = Eval::eq(key, arglist->value_at_index(j));
Packit Service 7770af
        }
Packit Service 7770af
        if (!remove) *result << std::make_pair(key, m->at(key));
Packit Service 7770af
      }
Packit Service 7770af
      return result;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature keywords_sig = "keywords($args)";
Packit Service 7770af
    BUILT_IN(keywords)
Packit Service 7770af
    {
Packit Service 7770af
      List_Obj arglist = SASS_MEMORY_COPY(ARG("$args", List)); // copy
Packit Service 7770af
      Map_Obj result = SASS_MEMORY_NEW(Map, pstate, 1);
Packit Service 7770af
      for (size_t i = arglist->size(), L = arglist->length(); i < L; ++i) {
Packit Service 7770af
        Expression_Obj obj = arglist->at(i);
Packit Service 7770af
        Argument_Obj arg = (Argument_Ptr) obj.ptr(); // XXX
Packit Service 7770af
        std::string name = std::string(arg->name());
Packit Service 7770af
        name = name.erase(0, 1); // sanitize name (remove dollar sign)
Packit Service 7770af
        *result << std::make_pair(SASS_MEMORY_NEW(String_Quoted,
Packit Service 7770af
                 pstate, name),
Packit Service 7770af
                 arg->value());
Packit Service 7770af
      }
Packit Service 7770af
      return result.detach();
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    //////////////////////////
Packit Service 7770af
    // INTROSPECTION FUNCTIONS
Packit Service 7770af
    //////////////////////////
Packit Service 7770af
Packit Service 7770af
    Signature type_of_sig = "type-of($value)";
Packit Service 7770af
    BUILT_IN(type_of)
Packit Service 7770af
    {
Packit Service 7770af
      Expression_Ptr v = ARG("$value", Expression);
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Quoted, pstate, v->type());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature unit_sig = "unit($number)";
Packit Service 7770af
    BUILT_IN(unit)
Packit Service 7770af
    { return SASS_MEMORY_NEW(String_Quoted, pstate, quote(ARG("$number", Number)->unit(), '"')); }
Packit Service 7770af
Packit Service 7770af
    Signature unitless_sig = "unitless($number)";
Packit Service 7770af
    BUILT_IN(unitless)
Packit Service 7770af
    { return SASS_MEMORY_NEW(Boolean, pstate, ARG("$number", Number)->is_unitless()); }
Packit Service 7770af
Packit Service 7770af
    Signature comparable_sig = "comparable($number-1, $number-2)";
Packit Service 7770af
    BUILT_IN(comparable)
Packit Service 7770af
    {
Packit Service 7770af
      Number_Ptr n1 = ARG("$number-1", Number);
Packit Service 7770af
      Number_Ptr n2 = ARG("$number-2", Number);
Packit Service 7770af
      if (n1->is_unitless() || n2->is_unitless()) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, true);
Packit Service 7770af
      }
Packit Service 7770af
      Number tmp_n2(n2); // copy
Packit Service 7770af
      tmp_n2.normalize(n1->find_convertible_unit());
Packit Service 7770af
      return SASS_MEMORY_NEW(Boolean, pstate, n1->unit() == tmp_n2.unit());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature variable_exists_sig = "variable-exists($name)";
Packit Service 7770af
    BUILT_IN(variable_exists)
Packit Service 7770af
    {
Packit Service 7770af
      std::string s = Util::normalize_underscores(unquote(ARG("$name", String_Constant)->value()));
Packit Service 7770af
Packit Service 7770af
      if(d_env.has("$"+s)) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, true);
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, false);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature global_variable_exists_sig = "global-variable-exists($name)";
Packit Service 7770af
    BUILT_IN(global_variable_exists)
Packit Service 7770af
    {
Packit Service 7770af
      std::string s = Util::normalize_underscores(unquote(ARG("$name", String_Constant)->value()));
Packit Service 7770af
Packit Service 7770af
      if(d_env.has_global("$"+s)) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, true);
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, false);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature function_exists_sig = "function-exists($name)";
Packit Service 7770af
    BUILT_IN(function_exists)
Packit Service 7770af
    {
Packit Service 7770af
      std::string s = Util::normalize_underscores(unquote(ARG("$name", String_Constant)->value()));
Packit Service 7770af
Packit Service 7770af
      if(d_env.has_global(s+"[f]")) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, true);
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, false);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature mixin_exists_sig = "mixin-exists($name)";
Packit Service 7770af
    BUILT_IN(mixin_exists)
Packit Service 7770af
    {
Packit Service 7770af
      std::string s = Util::normalize_underscores(unquote(ARG("$name", String_Constant)->value()));
Packit Service 7770af
Packit Service 7770af
      if(d_env.has_global(s+"[m]")) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, true);
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, false);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature feature_exists_sig = "feature-exists($name)";
Packit Service 7770af
    BUILT_IN(feature_exists)
Packit Service 7770af
    {
Packit Service 7770af
      std::string s = unquote(ARG("$name", String_Constant)->value());
Packit Service 7770af
Packit Service 7770af
      if(features.find(s) == features.end()) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, false);
Packit Service 7770af
      }
Packit Service 7770af
      else {
Packit Service 7770af
        return SASS_MEMORY_NEW(Boolean, pstate, true);
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature call_sig = "call($name, $args...)";
Packit Service 7770af
    BUILT_IN(call)
Packit Service 7770af
    {
Packit Service 7770af
      std::string name = Util::normalize_underscores(unquote(ARG("$name", String_Constant)->value()));
Packit Service 7770af
      List_Obj arglist = SASS_MEMORY_COPY(ARG("$args", List));
Packit Service 7770af
Packit Service 7770af
      Arguments_Obj args = SASS_MEMORY_NEW(Arguments, pstate);
Packit Service 7770af
      // std::string full_name(name + "[f]");
Packit Service 7770af
      // Definition_Ptr def = d_env.has(full_name) ? Cast<Definition>((d_env)[full_name]) : 0;
Packit Service 7770af
      // Parameters_Ptr params = def ? def->parameters() : 0;
Packit Service 7770af
      // size_t param_size = params ? params->length() : 0;
Packit Service 7770af
      for (size_t i = 0, L = arglist->length(); i < L; ++i) {
Packit Service 7770af
        Expression_Obj expr = arglist->value_at_index(i);
Packit Service 7770af
        // if (params && params->has_rest_parameter()) {
Packit Service 7770af
        //   Parameter_Obj p = param_size > i ? (*params)[i] : 0;
Packit Service 7770af
        //   List_Ptr list = Cast<List>(expr);
Packit Service 7770af
        //   if (list && p && !p->is_rest_parameter()) expr = (*list)[0];
Packit Service 7770af
        // }
Packit Service 7770af
        if (arglist->is_arglist()) {
Packit Service 7770af
          Expression_Obj obj = arglist->at(i);
Packit Service 7770af
          Argument_Obj arg = (Argument_Ptr) obj.ptr(); // XXX
Packit Service 7770af
          args->append(SASS_MEMORY_NEW(Argument,
Packit Service 7770af
                                       pstate,
Packit Service 7770af
                                       expr,
Packit Service 7770af
                                       arg ? arg->name() : "",
Packit Service 7770af
                                       arg ? arg->is_rest_argument() : false,
Packit Service 7770af
                                       arg ? arg->is_keyword_argument() : false));
Packit Service 7770af
        } else {
Packit Service 7770af
          args->append(SASS_MEMORY_NEW(Argument, pstate, expr));
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
      Function_Call_Obj func = SASS_MEMORY_NEW(Function_Call, pstate, name, args);
Packit Service 7770af
      Expand expand(ctx, &d_env, backtrace, &selector_stack);
Packit Service 7770af
      func->via_call(true); // calc invoke is allowed
Packit Service 7770af
      return func->perform(&expand.eval);
Packit Service 7770af
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    ////////////////////
Packit Service 7770af
    // BOOLEAN FUNCTIONS
Packit Service 7770af
    ////////////////////
Packit Service 7770af
Packit Service 7770af
    Signature not_sig = "not($value)";
Packit Service 7770af
    BUILT_IN(sass_not)
Packit Service 7770af
    {
Packit Service 7770af
      return SASS_MEMORY_NEW(Boolean, pstate, ARG("$value", Expression)->is_false());
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature if_sig = "if($condition, $if-true, $if-false)";
Packit Service 7770af
    // BUILT_IN(sass_if)
Packit Service 7770af
    // { return ARG("$condition", Expression)->is_false() ? ARG("$if-false", Expression) : ARG("$if-true", Expression); }
Packit Service 7770af
    BUILT_IN(sass_if)
Packit Service 7770af
    {
Packit Service 7770af
      Expand expand(ctx, &d_env, backtrace, &selector_stack);
Packit Service 7770af
      Expression_Obj cond = ARG("$condition", Expression)->perform(&expand.eval);
Packit Service 7770af
      bool is_true = !cond->is_false();
Packit Service 7770af
      Expression_Ptr res = ARG(is_true ? "$if-true" : "$if-false", Expression);
Packit Service 7770af
      res = res->perform(&expand.eval);
Packit Service 7770af
      res->set_delayed(false); // clone?
Packit Service 7770af
      return res;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    //////////////////////////
Packit Service 7770af
    // MISCELLANEOUS FUNCTIONS
Packit Service 7770af
    //////////////////////////
Packit Service 7770af
Packit Service 7770af
    // value.check_deprecated_interp if value.is_a?(Sass::Script::Value::String)
Packit Service 7770af
    // unquoted_string(value.to_sass)
Packit Service 7770af
Packit Service 7770af
    Signature inspect_sig = "inspect($value)";
Packit Service 7770af
    BUILT_IN(inspect)
Packit Service 7770af
    {
Packit Service 7770af
      Expression_Ptr v = ARG("$value", Expression);
Packit Service 7770af
      if (v->concrete_type() == Expression::NULL_VAL) {
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, "null");
Packit Service 7770af
      } else if (v->concrete_type() == Expression::BOOLEAN && v->is_false()) {
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, "false");
Packit Service 7770af
      } else if (v->concrete_type() == Expression::STRING) {
Packit Service 7770af
        return v;
Packit Service 7770af
      } else {
Packit Service 7770af
        // ToDo: fix to_sass for nested parentheses
Packit Service 7770af
        Sass_Output_Style old_style;
Packit Service 7770af
        old_style = ctx.c_options.output_style;
Packit Service 7770af
        ctx.c_options.output_style = TO_SASS;
Packit Service 7770af
        Emitter emitter(ctx.c_options);
Packit Service 7770af
        Inspect i(emitter);
Packit Service 7770af
        i.in_declaration = false;
Packit Service 7770af
        v->perform(&i);
Packit Service 7770af
        ctx.c_options.output_style = old_style;
Packit Service 7770af
        return SASS_MEMORY_NEW(String_Quoted, pstate, i.get_buffer());
Packit Service 7770af
      }
Packit Service 7770af
      // return v;
Packit Service 7770af
    }
Packit Service 7770af
    Signature selector_nest_sig = "selector-nest($selectors...)";
Packit Service 7770af
    BUILT_IN(selector_nest)
Packit Service 7770af
    {
Packit Service 7770af
      List_Ptr arglist = ARG("$selectors", List);
Packit Service 7770af
Packit Service 7770af
      // Not enough parameters
Packit Service 7770af
      if( arglist->length() == 0 )
Packit Service 7770af
        error("$selectors: At least one selector must be passed for `selector-nest'", pstate);
Packit Service 7770af
Packit Service 7770af
      // Parse args into vector of selectors
Packit Service 7770af
      std::vector<Selector_List_Obj> parsedSelectors;
Packit Service 7770af
      for (size_t i = 0, L = arglist->length(); i < L; ++i) {
Packit Service 7770af
        Expression_Obj exp = Cast<Expression>(arglist->value_at_index(i));
Packit Service 7770af
        if (exp->concrete_type() == Expression::NULL_VAL) {
Packit Service 7770af
          std::stringstream msg;
Packit Service 7770af
          msg << "$selectors: null is not a valid selector: it must be a string,\n";
Packit Service 7770af
          msg << "a list of strings, or a list of lists of strings for 'selector-nest'";
Packit Service 7770af
          error(msg.str(), pstate);
Packit Service 7770af
        }
Packit Service 7770af
        if (String_Constant_Obj str = Cast<String_Constant>(exp)) {
Packit Service 7770af
          str->quote_mark(0);
Packit Service 7770af
        }
Packit Service 7770af
        std::string exp_src = exp->to_string(ctx.c_options);
Packit Service 7770af
        Selector_List_Obj sel = Parser::parse_selector(exp_src.c_str(), ctx);
Packit Service 7770af
        parsedSelectors.push_back(sel);
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      // Nothing to do
Packit Service 7770af
      if( parsedSelectors.empty() ) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Null, pstate);
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      // Set the first element as the `result`, keep appending to as we go down the parsedSelector vector.
Packit Service 7770af
      std::vector<Selector_List_Obj>::iterator itr = parsedSelectors.begin();
Packit Service 7770af
      Selector_List_Obj result = *itr;
Packit Service 7770af
      ++itr;
Packit Service 7770af
Packit Service 7770af
      for(;itr != parsedSelectors.end(); ++itr) {
Packit Service 7770af
        Selector_List_Obj child = *itr;
Packit Service 7770af
        std::vector<Complex_Selector_Obj> exploded;
Packit Service 7770af
        selector_stack.push_back(result);
Packit Service 7770af
        Selector_List_Obj rv = child->resolve_parent_refs(selector_stack);
Packit Service 7770af
        selector_stack.pop_back();
Packit Service 7770af
        for (size_t m = 0, mLen = rv->length(); m < mLen; ++m) {
Packit Service 7770af
          exploded.push_back((*rv)[m]);
Packit Service 7770af
        }
Packit Service 7770af
        result->elements(exploded);
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      Listize listize;
Packit Service 7770af
      return result->perform(&listize);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature selector_append_sig = "selector-append($selectors...)";
Packit Service 7770af
    BUILT_IN(selector_append)
Packit Service 7770af
    {
Packit Service 7770af
      List_Ptr arglist = ARG("$selectors", List);
Packit Service 7770af
Packit Service 7770af
      // Not enough parameters
Packit Service 7770af
      if( arglist->length() == 0 )
Packit Service 7770af
        error("$selectors: At least one selector must be passed for `selector-append'", pstate);
Packit Service 7770af
Packit Service 7770af
      // Parse args into vector of selectors
Packit Service 7770af
      std::vector<Selector_List_Obj> parsedSelectors;
Packit Service 7770af
      for (size_t i = 0, L = arglist->length(); i < L; ++i) {
Packit Service 7770af
        Expression_Obj exp = Cast<Expression>(arglist->value_at_index(i));
Packit Service 7770af
        if (exp->concrete_type() == Expression::NULL_VAL) {
Packit Service 7770af
          std::stringstream msg;
Packit Service 7770af
          msg << "$selectors: null is not a valid selector: it must be a string,\n";
Packit Service 7770af
          msg << "a list of strings, or a list of lists of strings for 'selector-append'";
Packit Service 7770af
          error(msg.str(), pstate);
Packit Service 7770af
        }
Packit Service 7770af
        if (String_Constant_Ptr str = Cast<String_Constant>(exp)) {
Packit Service 7770af
          str->quote_mark(0);
Packit Service 7770af
        }
Packit Service 7770af
        std::string exp_src = exp->to_string();
Packit Service 7770af
        Selector_List_Obj sel = Parser::parse_selector(exp_src.c_str(), ctx);
Packit Service 7770af
        parsedSelectors.push_back(sel);
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      // Nothing to do
Packit Service 7770af
      if( parsedSelectors.empty() ) {
Packit Service 7770af
        return SASS_MEMORY_NEW(Null, pstate);
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      // Set the first element as the `result`, keep appending to as we go down the parsedSelector vector.
Packit Service 7770af
      std::vector<Selector_List_Obj>::iterator itr = parsedSelectors.begin();
Packit Service 7770af
      Selector_List_Obj result = *itr;
Packit Service 7770af
      ++itr;
Packit Service 7770af
Packit Service 7770af
      for(;itr != parsedSelectors.end(); ++itr) {
Packit Service 7770af
        Selector_List_Obj child = *itr;
Packit Service 7770af
        std::vector<Complex_Selector_Obj> newElements;
Packit Service 7770af
Packit Service 7770af
        // For every COMPLEX_SELECTOR in `result`
Packit Service 7770af
        // For every COMPLEX_SELECTOR in `child`
Packit Service 7770af
          // let parentSeqClone equal a copy of result->elements[i]
Packit Service 7770af
          // let childSeq equal child->elements[j]
Packit Service 7770af
          // Append all of childSeq head elements into parentSeqClone
Packit Service 7770af
          // Set the innermost tail of parentSeqClone, to childSeq's tail
Packit Service 7770af
        // Replace result->elements with newElements
Packit Service 7770af
        for (size_t i = 0, resultLen = result->length(); i < resultLen; ++i) {
Packit Service 7770af
          for (size_t j = 0, childLen = child->length(); j < childLen; ++j) {
Packit Service 7770af
            Complex_Selector_Obj parentSeqClone = SASS_MEMORY_CLONE((*result)[i]);
Packit Service 7770af
            Complex_Selector_Obj childSeq = (*child)[j];
Packit Service 7770af
            Complex_Selector_Obj base = childSeq->tail();
Packit Service 7770af
Packit Service 7770af
            // Must be a simple sequence
Packit Service 7770af
            if( childSeq->combinator() != Complex_Selector::Combinator::ANCESTOR_OF ) {
Packit Service 7770af
              std::string msg("Can't append \"");
Packit Service 7770af
              msg += childSeq->to_string();
Packit Service 7770af
              msg += "\" to \"";
Packit Service 7770af
              msg += parentSeqClone->to_string();
Packit Service 7770af
              msg += "\" for `selector-append'";
Packit Service 7770af
              error(msg, pstate, backtrace);
Packit Service 7770af
            }
Packit Service 7770af
Packit Service 7770af
            // Cannot be a Universal selector
Packit Service 7770af
            Element_Selector_Obj pType = Cast<Element_Selector>(childSeq->head()->first());
Packit Service 7770af
            if(pType && pType->name() == "*") {
Packit Service 7770af
              std::string msg("Can't append \"");
Packit Service 7770af
              msg += childSeq->to_string();
Packit Service 7770af
              msg += "\" to \"";
Packit Service 7770af
              msg += parentSeqClone->to_string();
Packit Service 7770af
              msg += "\" for `selector-append'";
Packit Service 7770af
              error(msg, pstate, backtrace);
Packit Service 7770af
            }
Packit Service 7770af
Packit Service 7770af
            // TODO: Add check for namespace stuff
Packit Service 7770af
Packit Service 7770af
            // append any selectors in childSeq's head
Packit Service 7770af
            parentSeqClone->innermost()->head()->concat(base->head());
Packit Service 7770af
Packit Service 7770af
            // Set parentSeqClone new tail
Packit Service 7770af
            parentSeqClone->innermost()->tail( base->tail() );
Packit Service 7770af
Packit Service 7770af
            newElements.push_back(parentSeqClone);
Packit Service 7770af
          }
Packit Service 7770af
        }
Packit Service 7770af
Packit Service 7770af
        result->elements(newElements);
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      Listize listize;
Packit Service 7770af
      return result->perform(&listize);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature selector_unify_sig = "selector-unify($selector1, $selector2)";
Packit Service 7770af
    BUILT_IN(selector_unify)
Packit Service 7770af
    {
Packit Service 7770af
      Selector_List_Obj selector1 = ARGSEL("$selector1", Selector_List_Obj, p_contextualize);
Packit Service 7770af
      Selector_List_Obj selector2 = ARGSEL("$selector2", Selector_List_Obj, p_contextualize);
Packit Service 7770af
Packit Service 7770af
      Selector_List_Obj result = selector1->unify_with(selector2);
Packit Service 7770af
      Listize listize;
Packit Service 7770af
      return result->perform(&listize);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature simple_selectors_sig = "simple-selectors($selector)";
Packit Service 7770af
    BUILT_IN(simple_selectors)
Packit Service 7770af
    {
Packit Service 7770af
      Compound_Selector_Obj sel = ARGSEL("$selector", Compound_Selector_Obj, p_contextualize);
Packit Service 7770af
Packit Service 7770af
      List_Ptr l = SASS_MEMORY_NEW(List, sel->pstate(), sel->length(), SASS_COMMA);
Packit Service 7770af
Packit Service 7770af
      for (size_t i = 0, L = sel->length(); i < L; ++i) {
Packit Service 7770af
        Simple_Selector_Obj ss = (*sel)[i];
Packit Service 7770af
        std::string ss_string = ss->to_string() ;
Packit Service 7770af
Packit Service 7770af
        l->append(SASS_MEMORY_NEW(String_Quoted, ss->pstate(), ss_string));
Packit Service 7770af
      }
Packit Service 7770af
Packit Service 7770af
      return l;
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature selector_extend_sig = "selector-extend($selector, $extendee, $extender)";
Packit Service 7770af
    BUILT_IN(selector_extend)
Packit Service 7770af
    {
Packit Service 7770af
      Selector_List_Obj  selector = ARGSEL("$selector", Selector_List_Obj, p_contextualize);
Packit Service 7770af
      Selector_List_Obj  extendee = ARGSEL("$extendee", Selector_List_Obj, p_contextualize);
Packit Service 7770af
      Selector_List_Obj  extender = ARGSEL("$extender", Selector_List_Obj, p_contextualize);
Packit Service 7770af
Packit Service 7770af
      Subset_Map subset_map;
Packit Service 7770af
      extender->populate_extends(extendee, subset_map);
Packit Service 7770af
      Extend extend(subset_map);
Packit Service 7770af
Packit Service 7770af
      Selector_List_Obj result = extend.extendSelectorList(selector, false);
Packit Service 7770af
Packit Service 7770af
      Listize listize;
Packit Service 7770af
      return result->perform(&listize);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature selector_replace_sig = "selector-replace($selector, $original, $replacement)";
Packit Service 7770af
    BUILT_IN(selector_replace)
Packit Service 7770af
    {
Packit Service 7770af
      Selector_List_Obj selector = ARGSEL("$selector", Selector_List_Obj, p_contextualize);
Packit Service 7770af
      Selector_List_Obj original = ARGSEL("$original", Selector_List_Obj, p_contextualize);
Packit Service 7770af
      Selector_List_Obj replacement = ARGSEL("$replacement", Selector_List_Obj, p_contextualize);
Packit Service 7770af
      Subset_Map subset_map;
Packit Service 7770af
      replacement->populate_extends(original, subset_map);
Packit Service 7770af
      Extend extend(subset_map);
Packit Service 7770af
Packit Service 7770af
      Selector_List_Obj result = extend.extendSelectorList(selector, true);
Packit Service 7770af
Packit Service 7770af
      Listize listize;
Packit Service 7770af
      return result->perform(&listize);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature selector_parse_sig = "selector-parse($selector)";
Packit Service 7770af
    BUILT_IN(selector_parse)
Packit Service 7770af
    {
Packit Service 7770af
      Selector_List_Obj sel = ARGSEL("$selector", Selector_List_Obj, p_contextualize);
Packit Service 7770af
Packit Service 7770af
      Listize listize;
Packit Service 7770af
      return sel->perform(&listize);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature is_superselector_sig = "is-superselector($super, $sub)";
Packit Service 7770af
    BUILT_IN(is_superselector)
Packit Service 7770af
    {
Packit Service 7770af
      Selector_List_Obj  sel_sup = ARGSEL("$super", Selector_List_Obj, p_contextualize);
Packit Service 7770af
      Selector_List_Obj  sel_sub = ARGSEL("$sub", Selector_List_Obj, p_contextualize);
Packit Service 7770af
      bool result = sel_sup->is_superselector_of(sel_sub);
Packit Service 7770af
      return SASS_MEMORY_NEW(Boolean, pstate, result);
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
    Signature unique_id_sig = "unique-id()";
Packit Service 7770af
    BUILT_IN(unique_id)
Packit Service 7770af
    {
Packit Service 7770af
      std::stringstream ss;
Packit Service 7770af
      std::uniform_real_distribution<> distributor(0, 4294967296); // 16^8
Packit Service 7770af
      uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand));
Packit Service 7770af
      ss << "u" << std::setfill('0') << std::setw(8) << std::hex << distributed;
Packit Service 7770af
      return SASS_MEMORY_NEW(String_Quoted, pstate, ss.str());
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
}