/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_Constant.cpp Constant class definition */
/*
    Qore Programming Language

    Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

    Note that the Qore library is released under a choice of three open-source
    licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
    information.
*/

#include "qore_reflection.h"
#include "QC_Constant.h"
#include "QC_Namespace.h"

QoreReflectionNamespaceConstant::QoreReflectionNamespaceConstant(const char* name, ExceptionSink* xsink) {
    ce = pgm->findNamespaceConstant(name, ns);
    if (!ce) {
        xsink->raiseException("UNKNOWN-CONSTANT", "cannot resolve namespace constant '%s'", name);
    }
}

QoreReflectionNamespaceConstant::QoreReflectionNamespaceConstant(QoreProgram* pgm, const char* name, ExceptionSink* xsink) {
    ce = pgm->findNamespaceConstant(name, ns);
    if (!ce) {
        xsink->raiseException("UNKNOWN-CONSTANT", "cannot resolve namespace constant '%s'", name);
    }
}

static QoreValue find_all_constants(const QoreString& pattern, int re_opts, QoreProgram* pgm, ExceptionSink* xsink) {
    const_vec_t const_vec = pgm->findAllNamespaceConstantsRegex(pattern, re_opts, xsink);
    if (*xsink) {
        return QoreValue();
    }

    ReferenceHolder<QoreListNode> l(new QoreListNode(QC_CONSTANT->getTypeInfo()), xsink);
    for (auto& i : const_vec) {
        l->push(new QoreObject(QC_CONSTANT, pgm, new QoreReflectionNamespaceConstant(pgm, i.first, i.second)), xsink);
    }

    return l.release();
}

//! This class provides information about %Qore constants defined in %Qore @ref Qore::Reflection::Namespace "namespaces"
/** @see @ref Qore::Reflection::ClassConstant "ClassConstant"

    @since %Qore 0.9.0
 */
qclass Constant [ns=Qore::Reflection; arg=QoreReflectionNamespaceConstant* c; vparent=AbstractConstant; dom=REFLECTION];

//! Creates the object from the constant name or namespace-justified path as a string
/** @par Example:
    @code{.py}
Constant con("::ARGV");
    @endcode

    @param path the constant name or namespace-justified path as a string

    @throw UNKNOWN-CONSTANT cannot find a constant with the given name or path
    @throw ENCODING-CONVERSION-ERROR this exception could be thrown if the string argument is not in the @ref default_encoding "default character encoding" and an error occurs during encoding conversion

    @note searches namespace constants only; does not search class constants

    @see forName()
 */
Constant::constructor(string path) {
    TempEncodingHelper npath(path, QCS_DEFAULT, xsink);
    if (*xsink) {
        return;
    }

    ReferenceHolder<QoreReflectionNamespaceConstant> con(new QoreReflectionNamespaceConstant(npath->c_str(), xsink), xsink);
    if (*xsink) {
        return;
    }
    self->setPrivate(CID_CONSTANT, con.release());
}

//! Returns the namespace for the constant
/** @par Example:
    @code{.py}
Namespace ns = con.getNamespace();
    @endcode

    @return the namespace for the constant
 */
Namespace Constant::getNamespace() [flags=CONSTANT] {
    return new QoreObject(QC_NAMESPACE, c->pgm, new QoreReflectionNamespace(c->pgm, c->ns));
}

//! Returns a %Constant object from the constant name or namespace-justified path as a string
/** @par Example:
    @code{.py}
Constant con = Constant::forName("Option::HAVE_FORK");
    @endcode

    @param path the constant name or namespace-justified path as a string

    @return a constant object corresponding to the name or namespace-justified path provided as an argument

    @throw UNKNOWN-CONSTANT cannot find a constant with the given name or path
    @throw ENCODING-CONVERSION-ERROR this exception could be thrown if the string argument is not in the @ref default_encoding "default character encoding" and an error occurs during encoding conversion

    @note searches namespace constants only; does not search class constants
 */
static Constant Constant::forName(string path) [flags=RET_VALUE_ONLY] {
    TempEncodingHelper npath(path, QCS_DEFAULT, xsink);
    if (*xsink) {
        return QoreValue();
    }

    ReferenceHolder<QoreReflectionNamespaceConstant> cls(new QoreReflectionNamespaceConstant(npath->c_str(), xsink), xsink);
    if (*xsink) {
        return QoreValue();
    }

    return new QoreObject(QC_CONSTANT, getProgram(), cls.release());
}

//! Returns a %Constant object from the constant name or namespace-justified path as a string
/** @par Example:
    @code{.py}
Constant con = Constant::forName(pgm, "Option::HAVE_FORK");
    @endcode

    @param pgm the Program object to search
    @param path the constant name or namespace-justified path as a string

    @return a constant object corresponding to the name or namespace-justified path provided as an argument

    @throw UNKNOWN-CONSTANT cannot find a constant with the given name or path
    @throw ENCODING-CONVERSION-ERROR this exception could be thrown if the string argument is not in the @ref default_encoding "default character encoding" and an error occurs during encoding conversion

    @note searches namespace constants only; does not search class constants
 */
static Constant Constant::forName(Program[QoreProgram] pgm, string path) [flags=RET_VALUE_ONLY] {
    ReferenceHolder<QoreProgram> pgm_holder(pgm, xsink);
    TempEncodingHelper npath(path, QCS_DEFAULT, xsink);
    if (*xsink) {
        return QoreValue();
    }

    ReferenceHolder<QoreReflectionNamespaceConstant> cls(new QoreReflectionNamespaceConstant(pgm, npath->c_str(), xsink), xsink);
    if (*xsink) {
        return QoreValue();
    }

    return new QoreObject(QC_CONSTANT, pgm, cls.release());
}

//! Returns a list of constants matching the regular expression pattern argument or an empty list if no namespace constants match
/** @par Example:
    @code{.py}
# return all accessible constants that start with the letter "T"
list<Constant> l = Constant::findAllRegex("^T");
    @endcode

    @param pattern the regular expression pattern to match
    @param re_opts see @ref regex_constants for possible values

    @return a list of constants matching the regular expression pattern argument or an empty list if no constants match

    @note searches namespace constants only; does not search class constants
*/
static list<Constant> Constant::findAllRegex(string pattern, *int re_opts) [flags=RET_VALUE_ONLY] {
    return find_all_constants(*pattern, re_opts, getProgram(), xsink);
}

//! Returns a list of constants matching the regular expression pattern argument or an empty list if no namespace constants match
/** @par Example:
    @code{.py}
# return all accessible constants that start with the letter "T"
list<Constant> l = Constant::findAllRegex(pgm, "^T");
    @endcode

    @param pgm the Program object to search
    @param pattern the regular expression pattern to match
    @param re_opts see @ref regex_constants for possible values

    @return a list of constants matching the regular expression pattern argument or an empty list if no constants match

    @note searches namespace constants only; does not search class constants
*/
static list<Constant> Constant::findAllRegex(Program[QoreProgram] pgm, string pattern, *int re_opts) [flags=RET_VALUE_ONLY] {
    ReferenceHolder<QoreProgram> pgm_holder(pgm, xsink);
    return find_all_constants(*pattern, re_opts, pgm, xsink);
}
