
#line 307 "../../../qtremoteobjects/src/repparser/parser.g"

#include "repparser.h"

#include <QCryptographicHash>
#include <QDebug>
#include <QTextStream>

// for normalizeTypeInternal
#include <private/qmetaobject_p.h>
#include <private/qmetaobject_moc_p.h>

// Code copied from moc.cpp
// We cannot depend on QMetaObject::normalizedSignature,
// since repc is linked against Qt5Bootstrap (which doesn't offer QMetaObject) when cross-compiling
// Thus, just use internal API which is exported in private headers, as moc does
static QByteArray normalizeType(const QByteArray &ba)
{
    const char *s = ba.constData();
    int len = ba.size();
    char stackbuf[64];
    char *buf = (len >= 64 ? new char[len + 1] : stackbuf);
    char *d = buf;
    char last = 0;
    while (*s && is_space(*s))
        s++;
    while (*s) {
        while (*s && !is_space(*s))
            last = *d++ = *s++;
        while (*s && is_space(*s))
            s++;
        if (*s && ((is_ident_char(*s) && is_ident_char(last))
                   || ((*s == ':') && (last == '<')))) {
            last = *d++ = ' ';
        }
    }
    *d = '\0';
    QByteArray result = normalizeTypeInternal(buf, d);
    if (buf != stackbuf)
        delete [] buf;
    return result;
}

SignedType::SignedType(const QString &name) : name(name)
{
}

void SignedType::generateSignature(AST &ast)
{
    QCryptographicHash checksum(QCryptographicHash::Sha1);
    signature_impl(ast, checksum);
    ast.typeSignatures[typeName()] = checksum.result().toHex();
}

QString SignedType::typeName() const
{
    return name;
}

ASTProperty::ASTProperty()
    : modifier(ReadPush), persisted(false), isPointer(false)
{
}

ASTProperty::ASTProperty(const QString &type, const QString &name, const QString &defaultValue, Modifier modifier, bool persisted, bool isPointer)
    : type(type), name(name), defaultValue(defaultValue), modifier(modifier), persisted(persisted), isPointer(isPointer)
{
}

QString ASTDeclaration::asString(bool withName) const
{
    QString str;
    if (variableType & ASTDeclaration::Constant)
        str += QLatin1String("const ");
    str += type;
    if (variableType & ASTDeclaration::Reference)
        str += QLatin1String(" &");
    if (withName)
        str += QString::fromLatin1(" %1").arg(name);
    return str;
}

ASTFunction::ASTFunction(const QString &name, const QString &returnType)
    : returnType(returnType), name(name)
{
}

QString ASTFunction::paramsAsString(ParamsAsStringFormat format) const
{
    QString str;
    for (const ASTDeclaration &param : params) {
        QString paramStr = param.asString(format != Normalized);
        if (format == Normalized) {
            paramStr = QString::fromLatin1(::normalizeType(paramStr.toLatin1().constData()));
            str += paramStr + QLatin1Char(',');
        } else {
            str += paramStr + QLatin1String(", ");
        }
    }

    str.chop((format == Normalized ? 1 : 2)); // chop trailing ',' or ', '

    return str;
}

QStringList ASTFunction::paramNames() const
{
    QStringList names;
    names.reserve(params.size());
    for (const ASTDeclaration &param : params)
        names << param.name;
    return names;
}

ASTEnum::ASTEnum(const QString &name)
    : SignedType(name), isSigned(false), isScoped(false), max(0)
{
}

void ASTEnum::signature_impl(const AST &ast, QCryptographicHash &checksum)
{
    Q_UNUSED(ast)
    checksum.addData(name.toLatin1());
    if (isScoped)
        checksum.addData("class");
    if (!type.isEmpty())
        checksum.addData(type.toLatin1());
    for (const ASTEnumParam &param : params) {
        checksum.addData(param.name.toLatin1());
        checksum.addData(QByteArray::number(param.value));
    }
}

QString ASTEnum::typeName() const
{
    if (scope.isEmpty())
        return name;

    return QLatin1String("%1::%2").arg(scope, name);
}

ASTFlag::ASTFlag(const QString &name, const QString &_enum)
    : SignedType(name), _enum(_enum)
{
}

void ASTFlag::signature_impl(const AST &ast, QCryptographicHash &checksum)
{
    checksum.addData(name.toLatin1());
    checksum.addData(ast.typeData(_enum, scope));
}

QString ASTFlag::typeName() const
{
    if (scope.isEmpty())
        return name;

    return QLatin1String("%1::%2").arg(scope, name);
}

bool ASTFlag::isValid() const
{
    return !name.isEmpty();
}

void ASTModel::signature_impl(const AST &ast, QCryptographicHash &checksum)
{
    Q_UNUSED(ast)
    QByteArrayList _roles;
    for (const auto &role : roles)
        _roles << role.name.toLatin1();
    std::sort(_roles.begin(), _roles.end());
    checksum.addData(_roles.join('_'));
}

QString ASTModel::typeName() const
{
    return QLatin1String("%1::%2").arg(scope, name);
}

ASTClass::ASTClass(const QString &name)
    : SignedType(name), hasPersisted(false)
{
}

void ASTClass::signature_impl(const AST &ast, QCryptographicHash &checksum)
{
    checksum.addData(name.toLatin1());

    // Checksum properties
    QSet<int> classIndices{ subClassPropertyIndices.begin(),
                            subClassPropertyIndices.end() };
    int propertyIndex = -1;
    int modelIndex = 0;
    for (const ASTProperty &p : properties) {
        propertyIndex++;
        checksum.addData(p.name.toLatin1());
        if (p.type == QLatin1String("QAbstractItemModel"))
            checksum.addData(ast.typeSignatures[modelMetadata[modelIndex++].typeName()]);
        else if (classIndices.contains(propertyIndex))
            checksum.addData(ast.typeSignatures[p.type]);
        else
            checksum.addData(ast.typeData(p.type, name));
        ASTProperty::Modifier m = p.modifier;
        // Treat ReadOnly and SourceOnlySetter the same (interface-wise they are)
        if (m == ASTProperty::SourceOnlySetter)
            m = ASTProperty::ReadOnly;
        checksum.addData({reinterpret_cast<const char *>(&m), sizeof(m)});
    }

    // Checksum signals
    checksum.addData(ast.functionsData(signalsList, name));

    // Checksum slots
    checksum.addData(ast.functionsData(slotsList, name));
}

void POD::signature_impl(const AST &ast, QCryptographicHash &checksum)
{
    checksum.addData(name.toLatin1());
    for (const PODAttribute &attr : attributes) {
        checksum.addData(attr.name.toLatin1());
        checksum.addData(ast.typeData(attr.type, name));
    }
}

bool ASTClass::isValid() const
{
    return !name.isEmpty();
}

bool ASTClass::hasPointerObjects() const
{
    int count = modelMetadata.size() + subClassPropertyIndices.size();
    return count > 0;
}

QByteArray AST::typeData(const QString &type, const QString &className) const
{
    static const QRegularExpression re = QRegularExpression(QLatin1String("([^<>,\\s]+)"));
    if (type.contains(QLatin1Char('<'))) { // templated type
        QByteArray result;
        for (const QRegularExpressionMatch &match : re.globalMatch(type))
            result += typeData(match.captured(1), className);
        return result;
    }
    // Try enum/flags within the class first
    if (!className.isEmpty()) {
        auto classType = QLatin1String("%1::%2").arg(className, type);
        auto it = typeSignatures.find(classType);
        if (it != typeSignatures.end())
            return it.value();
    }
    auto it = typeSignatures.find(type);
    if (it != typeSignatures.end())
        return it.value();
    const auto pos = type.lastIndexOf(QLatin1String("::"));
    if (pos > 0)
        return typeData(type.mid(pos + 2), className);
    return type.toLatin1();
}

QByteArray AST::functionsData(const QList<ASTFunction> &functions, const QString &className) const
{
    QByteArray ret;
    for (const ASTFunction &func : functions) {
        ret += func.name.toLatin1();
        for (const ASTDeclaration &param : func.params) {
            ret += param.name.toLatin1();
            ret += typeData(param.type, className);
            ret += QByteArray(reinterpret_cast<const char *>(&param.variableType),
                              sizeof(param.variableType));
        }
        ret += typeData(func.returnType, className);
    }
    return ret;
}

RepParser::RepParser(QIODevice &outputDevice)
    : QRegexParser(), m_astEnumValue(-1)
{
    setBufferFromDevice(&outputDevice);
}

void RepParser::reset()
{
    m_ast = AST();
    m_astClass = ASTClass();
    m_astPod = POD();
    m_argString.clear();
    m_astEnum = ASTEnum();
    //setDebug();
}

bool RepParser::parseModifierFlag(const QString &flag, ASTProperty::Modifier &modifier, bool &persisted)
{
    QRegularExpression regex(QStringLiteral("\\s*,\\s*"));
    QStringList flags = flag.split(regex);
    persisted = flags.removeAll(QStringLiteral("PERSISTED")) > 0;
    if (flags.length() == 0)
        return true;
    if (flags.length() > 1) {
        // Only valid combination is "READONLY" and "CONSTANT"
        if (flags.length() == 2 && flags.contains(QStringLiteral("READONLY")) &&
            flags.contains(QStringLiteral("CONSTANT"))) {
            // If we have READONLY and CONSTANT that means CONSTANT
            modifier = ASTProperty::Constant;
            return true;
        } else {
            setErrorString(QLatin1String("Invalid property declaration: combination not allowed (%1)").arg(flag));
            return false;
        }
    }
    const QString &f = flags.at(0);
    if (f == QLatin1String("READONLY"))
        modifier = ASTProperty::ReadOnly;
    else if (f == QLatin1String("CONSTANT"))
        modifier = ASTProperty::Constant;
    else if (f == QLatin1String("READPUSH"))
        modifier = ASTProperty::ReadPush;
    else if (f == QLatin1String("READWRITE"))
        modifier = ASTProperty::ReadWrite;
    else if (f == QLatin1String("SOURCEONLYSETTER"))
        modifier = ASTProperty::SourceOnlySetter;
    else {
        setErrorString(QLatin1String("Invalid property declaration: flag %1 is unknown").arg(flag));
        return false;
    }

    return true;
}

QString stripArgs(const QString &arguments)
{
    // This repc parser searches for the longest possible matches, which can be multiline.
    // This method "cleans" the string input, removing comments and converting to a single
    // line for subsequent parsing.
    QStringList lines = arguments.split(QRegularExpression(QStringLiteral("\r?\n")));
    for (auto & line : lines)
        line.replace(QRegularExpression(QStringLiteral("//.*")),QString());
    return lines.join(QString());
}

bool RepParser::parseProperty(ASTClass &astClass, const QString &propertyDeclaration)
{
    QString input = stripArgs(propertyDeclaration).trimmed();
    const QRegularExpression whitespace(QStringLiteral("\\s"));

    QString propertyType;
    QString propertyName;
    QString propertyDefaultValue;
    ASTProperty::Modifier propertyModifier = ASTProperty::ReadPush;
    bool persisted = false;

    // parse type declaration which could be a nested template as well
    bool inTemplate = false;
    int templateDepth = 0;
    int nameIndex = -1;

    for (int i = 0; i < input.size(); ++i) {
        const QChar inputChar(input.at(i));
        if (inputChar == QLatin1Char('<')) {
            propertyType += inputChar;
            inTemplate = true;
            ++templateDepth;
        } else if (inputChar == QLatin1Char('>')) {
            propertyType += inputChar;
            --templateDepth;
            if (templateDepth == 0)
                inTemplate = false;
        } else if (inputChar.isSpace()) {
            if (!inTemplate) {
                nameIndex = i;
                break;
            } else {
                propertyType += inputChar;
            }
        } else {
            propertyType += inputChar;
        }
    }

    if (nameIndex == -1) {
        setErrorString(QLatin1String("PROP: Invalid property declaration: %1").arg(propertyDeclaration));
        return false;
    }

    // parse the name of the property
    input = input.mid(nameIndex).trimmed();

    const int equalSignIndex = input.indexOf(QLatin1Char('='));
    if (equalSignIndex != -1) { // we have a default value
        propertyName = input.left(equalSignIndex).trimmed();

        input = input.mid(equalSignIndex + 1).trimmed();
        const int lastQuoteIndex = input.lastIndexOf(QLatin1Char('"'));
        if (lastQuoteIndex != -1) {
            propertyDefaultValue = input.left(lastQuoteIndex + 1);
            input = input.mid(lastQuoteIndex + 1);
        }
        const int whitespaceIndex = input.indexOf(whitespace);
        if (whitespaceIndex == -1) { // no flag given
            if (propertyDefaultValue.isEmpty())
                propertyDefaultValue = input;
            propertyModifier = ASTProperty::ReadPush;
        } else { // flag given
            if (propertyDefaultValue.isEmpty())
                propertyDefaultValue = input.left(whitespaceIndex).trimmed();

            const QString flag = input.mid(whitespaceIndex + 1).trimmed();
            if (!parseModifierFlag(flag, propertyModifier, persisted))
                return false;
        }
    } else { // there is no default value
        const int whitespaceIndex = input.indexOf(whitespace);
        if (whitespaceIndex == -1) { // no flag given
            propertyName = input;
            propertyModifier = ASTProperty::ReadPush;
        } else { // flag given
            propertyName = input.left(whitespaceIndex).trimmed();

            const QString flag = input.mid(whitespaceIndex + 1).trimmed();
            if (!parseModifierFlag(flag, propertyModifier, persisted))
                return false;
        }
    }

    astClass.properties << ASTProperty(propertyType, propertyName, propertyDefaultValue, propertyModifier, persisted);
    if (persisted)
        astClass.hasPersisted = true;
    return true;
}

bool RepParser::parseRoles(ASTModel &astModel, const QString &modelRoles)
{
    const QString input = modelRoles.trimmed();

    if (input.isEmpty())
        return true;

    const QStringList roleStrings = input.split(QChar(QLatin1Char(',')));
    for (auto role : roleStrings)
        astModel.roles << ASTModelRole(role.trimmed());
    return true;
}

AST RepParser::ast() const
{
    return m_ast;
}

void RepParser::TypeParser::parseArguments(const QString &arguments)
{
    const QString strippedArgs = stripArgs(arguments);
    int templateDepth = 0;
    bool inTemplate = false;
    bool inVariable = false;
    QString propertyType;
    QString variableName;
    ASTDeclaration::VariableTypes variableType = ASTDeclaration::None;
    int variableNameIndex = 0;
    for (int i = 0; i < strippedArgs.size(); ++i) {
        const QChar inputChar(strippedArgs.at(i));
        if (inputChar == QLatin1Char('<')) {
            propertyType += inputChar;
            inTemplate = true;
            ++templateDepth;
        } else if (inputChar == QLatin1Char('>')) {
            propertyType += inputChar;
            --templateDepth;
            if (templateDepth == 0)
                inTemplate = false;
        } else if (inputChar.isSpace()) {
            if (inTemplate)
                propertyType += inputChar;
            else if (!propertyType.isEmpty()) {
                if (propertyType == QLatin1String("const")) {
                    propertyType.clear();
                    variableType |= ASTDeclaration::Constant;
                } else {
                    inVariable = true;
                }
            }
        } else if (inputChar == QLatin1Char('&')) {
            variableType |= ASTDeclaration::Reference;
        } else if (inputChar == QLatin1Char(',')) {
            if (!inTemplate) {
                RepParser::TypeParser::generateFunctionParameter(variableName, propertyType, variableNameIndex, variableType);
                propertyType.clear();
                variableName.clear();
                variableType = ASTDeclaration::None;
                inVariable = false;
            } else {
                propertyType += inputChar;
            }
        } else {
            if (inVariable)
                variableName += inputChar;
            else
                propertyType += inputChar;
        }
    }
    if (!propertyType.isEmpty()) {
        RepParser::TypeParser::generateFunctionParameter(variableName, propertyType, variableNameIndex, variableType);
    }
}

void RepParser::TypeParser::generateFunctionParameter(QString variableName, const QString &propertyType, int &variableNameIndex, ASTDeclaration::VariableTypes variableType)
{
    if (!variableName.isEmpty())
        variableName = variableName.trimmed();
    else
        variableName = QString::fromLatin1("__repc_variable_%1").arg(++variableNameIndex);
    arguments.append(ASTDeclaration(propertyType, variableName, variableType));
}

void RepParser::TypeParser::appendParams(ASTFunction &slot)
{
    for (const ASTDeclaration &arg : std::as_const(arguments))
        slot.params << arg;
}

void RepParser::TypeParser::appendPods(POD &pods)
{
    for (const ASTDeclaration &arg : std::as_const(arguments)) {
        PODAttribute attr;
        attr.type = arg.type;
        attr.name = arg.name;
        pods.attributes.append(qMove(attr));
    }
}

bool RepParser::consumeRule(int ruleno)
{
    if (isDebug()) {
        qDebug() << "consumeRule:" << ruleno << spell[rule_info[rule_index[ruleno]]];
    }
    switch (ruleno) {

#line 860 "../../../qtremoteobjects/src/repparser/parser.g"

    case 22:
    {
        m_astEnum.generateSignature(m_ast);
        m_ast.enums.append(m_astEnum);
    }
    break;

#line 875 "../../../qtremoteobjects/src/repparser/parser.g"

    case 28:
    {
        const QString line = captured().value(QStringLiteral("preprocessor_directive"));
        if (line.startsWith(QStringLiteral("#HEADER")))
            m_ast.headerLines.append(line.sliced(8));
        else if (line.startsWith(QStringLiteral("#FOOTER")))
            m_ast.footerLines.append(line.sliced(8));
        else
            m_ast.preprocessorDirectives.append(captured().value(QStringLiteral("preprocessor_directive")));
    }
    break;

#line 890 "../../../qtremoteobjects/src/repparser/parser.g"

    case 29:
    {
        POD pod;
        pod.name = captured().value(QStringLiteral("name")).trimmed();
        pod.compilerAttribute = captured().value(QStringLiteral("attribute")).trimmed();

        const QString argString = captured().value(QLatin1String("types")).trimmed();
        if (argString.isEmpty()) {
            qWarning() << "[repc] - Ignoring POD with no data members.  POD name: " << qPrintable(pod.name);
            return true;
        }
        for (const QStringView &token : argString.tokenize(u' ')) {
            if (token.startsWith(QLatin1String("ENUM"))) {
                if (token.size() == 4 || !(token[4].isLetterOrNumber() || token[4] == u'_')) {
                    setErrorString(QLatin1String("ENUMs are only available in PODs using bracket syntax ('{'), not parentheses"));
                    return false;
                }
            }
        }

        RepParser::TypeParser parseType;
        parseType.parseArguments(argString);
        parseType.appendPods(pod);
        pod.generateSignature(m_ast);
        m_ast.pods.append(pod);
    }
    break;

#line 921 "../../../qtremoteobjects/src/repparser/parser.g"

    case 30:

#line 925 "../../../qtremoteobjects/src/repparser/parser.g"

    case 31:
    {
        m_astClass.generateSignature(m_ast);
        m_ast.classes.append(m_astClass);
    }
    break;

#line 942 "../../../qtremoteobjects/src/repparser/parser.g"

    case 49:
    {
        m_astEnum.scope = m_astClass.name;
        m_astEnum.generateSignature(m_ast);
        m_astClass.enums.append(m_astEnum);
    }
    break;

#line 953 "../../../qtremoteobjects/src/repparser/parser.g"

    case 50:

#line 957 "../../../qtremoteobjects/src/repparser/parser.g"

    case 51:
    {
        RepParser::TypeParser parseType;
        parseType.parseArguments(m_argString);
        parseType.appendPods(m_astPod);
        m_astPod.generateSignature(m_ast);
        m_ast.pods.append(m_astPod);
    }
    break;

#line 980 "../../../qtremoteobjects/src/repparser/parser.g"

    case 65:
    {
        m_astEnum.generateSignature(m_ast);
        m_astPod.enums.append(m_astEnum);
    }
    break;

#line 999 "../../../qtremoteobjects/src/repparser/parser.g"

    case 76:
    {
        m_argString += m_symbol;
    }
    break;

#line 1008 "../../../qtremoteobjects/src/repparser/parser.g"

    case 77:
    {
        m_argString += QLatin1Char('<');
    }
    break;

#line 1017 "../../../qtremoteobjects/src/repparser/parser.g"

    case 78:
    {
        m_argString += QLatin1Char('>');
    }
    break;

#line 1026 "../../../qtremoteobjects/src/repparser/parser.g"

    case 79:
    {
        m_argString += captured().value(QLatin1String("value")).trimmed() + QLatin1Char(' ');
    }
    break;

#line 1035 "../../../qtremoteobjects/src/repparser/parser.g"

    case 80:
    {
        m_argString += QLatin1Char(' ') + captured().value(QLatin1String("value")).trimmed();
    }
    break;

#line 1046 "../../../qtremoteobjects/src/repparser/parser.g"

    case 83:

#line 1050 "../../../qtremoteobjects/src/repparser/parser.g"

    case 84:
    {
        m_argString += QLatin1Char(',');
    }
    break;

#line 1059 "../../../qtremoteobjects/src/repparser/parser.g"

    case 85:
    {
        m_argString += m_symbol;
    }
    break;

#line 1068 "../../../qtremoteobjects/src/repparser/parser.g"

    case 86:
    {
        m_argString += QLatin1Char(' ') + m_symbol;
    }
    break;

#line 1093 "../../../qtremoteobjects/src/repparser/parser.g"

    case 130:
    {
        const QString name = captured().value(QLatin1String("name"));
        const QString type = captured().value(QLatin1String("type"));
        const QString _class = captured().value(QLatin1String("class"));

        // new Class declaration
        m_astEnum = ASTEnum(name);
        if (!_class.isEmpty())
            m_astEnum.isScoped = true;
        if (!type.isEmpty())
            m_astEnum.type = type;
        m_astEnumValue = -1;
    }
    break;

#line 1114 "../../../qtremoteobjects/src/repparser/parser.g"

    case 133:
    {
        ASTEnumParam param;
        param.name = m_symbol;
        param.value = ++m_astEnumValue;
        if (m_astEnum.max < param.value)
            m_astEnum.max = param.value;
        m_astEnum.params << param;
    }
    break;

#line 1128 "../../../qtremoteobjects/src/repparser/parser.g"

    case 134:
    {
        ASTEnumParam param;
        param.name = m_symbol;
        param.value = m_astEnumValue;
        if (param.value < 0) {
            m_astEnum.isSigned = true;
            if (m_astEnum.max < -param.value)
                m_astEnum.max = -param.value;
        } else if (m_astEnum.max < param.value)
            m_astEnum.max = param.value;
        m_astEnum.params << param;
    }
    break;

#line 1146 "../../../qtremoteobjects/src/repparser/parser.g"

    case 135:
    {
        m_symbol = captured().value(QStringLiteral("symbol")).trimmed();
    }
    break;

#line 1155 "../../../qtremoteobjects/src/repparser/parser.g"

    case 136:
    {
        QString value = captured().value(QStringLiteral("value")).trimmed();
        if (value.startsWith(QLatin1String("0x"), Qt::CaseInsensitive))
            m_astEnumValue = value.toInt(0,16);
        else
            m_astEnumValue = value.toInt();
    }
    break;

#line 1168 "../../../qtremoteobjects/src/repparser/parser.g"

    case 137:
    {
        const QString name = captured().value(QLatin1String("name"));
        const QString _enum = captured().value(QLatin1String("enum"));
        int enumIndex = 0;
        for (auto &en : m_astClass.enums) {
            if (en.name == _enum) {
                en.flagIndex = m_astClass.flags.count();
                break;
            }
            enumIndex++;
        }
        if (enumIndex == m_astClass.enums.count()) {
            setErrorString(QLatin1String("FLAG: Unknown (class) enum: %1").arg(_enum));
            return false;
        }
        auto flag = ASTFlag(name, _enum);
        flag.scope = m_astClass.name;
        flag.generateSignature(m_ast);
        m_astClass.flags.append(flag);
    }
    break;

#line 1194 "../../../qtremoteobjects/src/repparser/parser.g"

    case 138:
    {
        const QString name = captured().value(QLatin1String("name"));
        const QString _enum = captured().value(QLatin1String("enum"));
        int enumIndex = 0;
        for (auto &en : m_astPod.enums) {
            if (en.name == _enum) {
                en.flagIndex = m_astPod.flags.count();
                break;
            }
            enumIndex++;
        }
        if (enumIndex == m_astPod.enums.count()) {
            setErrorString(QLatin1String("FLAG: Unknown (pod) enum: %1").arg(_enum));
            return false;
        }
        auto flag = ASTFlag(name, _enum);
        flag.scope = m_astPod.name;
        flag.generateSignature(m_ast);
        m_astPod.flags.append(flag);
    }
    break;

#line 1220 "../../../qtremoteobjects/src/repparser/parser.g"

    case 139:
    {
        const QString args = captured().value(QLatin1String("args"));
        if (!parseProperty(m_astClass, args))
            return false;
    }
    break;

#line 1231 "../../../qtremoteobjects/src/repparser/parser.g"

    case 140:
    {
        ASTFunction signal;
        signal.name = captured().value(QLatin1String("name")).trimmed();

        const QString argString = captured().value(QLatin1String("args")).trimmed();
        RepParser::TypeParser parseType;
        parseType.parseArguments(argString);
        parseType.appendParams(signal);
        m_astClass.signalsList << signal;
    }
    break;

#line 1247 "../../../qtremoteobjects/src/repparser/parser.g"

    case 141:
    {
        QString returnTypeAndName = captured().value(QLatin1String("type")).trimmed();
        const QString argString = captured().value(QLatin1String("args")).trimmed();

        // compat code with old SLOT declaration: "SLOT(func(...))"
        const bool hasWhitespace = returnTypeAndName.indexOf(u' ') != -1;
        if (!hasWhitespace) {
            qWarning() << "[repc] - Adding 'void' for unspecified return type on" << qPrintable(returnTypeAndName);
            returnTypeAndName.prepend(QLatin1String("void "));
        }

        const int startOfFunctionName = returnTypeAndName.lastIndexOf(u' ') + 1;

        ASTFunction slot;
        slot.returnType = returnTypeAndName.mid(0, startOfFunctionName-1);
        slot.name = returnTypeAndName.mid(startOfFunctionName);

        RepParser::TypeParser parseType;
        parseType.parseArguments(argString);
        parseType.appendParams(slot);
        m_astClass.slotsList << slot;
    }
    break;

#line 1275 "../../../qtremoteobjects/src/repparser/parser.g"

    case 142:
    {
        const QString name = captured().value(QLatin1String("name")).trimmed();
        const QString argString = captured().value(QLatin1String("args")).trimmed();

        ASTModel model(name, m_astClass.name, m_astClass.properties.size());
        if (!parseRoles(model, argString))
            return false;

        model.generateSignature(m_ast);
        m_astClass.modelMetadata << model;
        m_astClass.properties << ASTProperty(QStringLiteral("QAbstractItemModel"), name, QStringLiteral("nullptr"), ASTProperty::SourceOnlySetter, false, true);
    }
    break;

#line 1293 "../../../qtremoteobjects/src/repparser/parser.g"

case 143:
{
    const QString name = captured().value(QLatin1String("name")).trimmed();
    const QString type = captured().value(QLatin1String("type")).trimmed();

    m_astClass.subClassPropertyIndices << m_astClass.properties.size();
    m_astClass.properties << ASTProperty(type, name, QStringLiteral("nullptr"), ASTProperty::SourceOnlySetter, false, true);
}
break;

#line 1306 "../../../qtremoteobjects/src/repparser/parser.g"

    case 144:

#line 1310 "../../../qtremoteobjects/src/repparser/parser.g"

    case 145:
    {
        const QString attribute = captured().value(QLatin1String("attribute"));
        const QString name = captured().value(QLatin1String("name"));

        // new Class declaration
        m_astClass = ASTClass(name);
        m_astClass.compilerAttribute = attribute;
    }
    break;

#line 1324 "../../../qtremoteobjects/src/repparser/parser.g"

    case 146:

#line 1328 "../../../qtremoteobjects/src/repparser/parser.g"

    case 147:
    {
        // new POD declaration
        m_astPod = POD();
        m_astPod.name = captured().value(QLatin1String("name")).trimmed();
        m_astPod.compilerAttribute = captured().value(QLatin1String("attribute"));
        m_argString.clear();
    }
    break;

#line 1341 "../../../qtremoteobjects/src/repparser/parser.g"

    case 148:
    {
        const QString name = captured().value(QLatin1String("name"));

        m_ast.enumUses.append(name);
    }
    break;

#line 1352 "../../../qtremoteobjects/src/repparser/parser.g"

    case 149:
    {
        const QString name = captured().value(QLatin1String("name"));
        const QString _enum = captured().value(QLatin1String("enum"));
        int enumIndex = 0;
        for (auto &en : m_ast.enums) {
            if (en.name == _enum) {
                en.flagIndex = m_ast.flags.count();
                break;
            }
            if (en.name == _enum)
                break;
            enumIndex++;
        }
        if (enumIndex == m_ast.enums.count()) {
            setErrorString(QLatin1String("FLAG: Unknown (global) enum: %1").arg(_enum));
            return false;
        }
        auto flag = ASTFlag(name, _enum);
        flag.generateSignature(m_ast);
        m_ast.flags.append(flag);
    }
    break;

#line 1380 "../../../qtremoteobjects/src/repparser/parser.g"

    case 150:
    {
        setErrorString(QStringLiteral("class: Cannot be nested"));
        return false;
    }
    break;

#line 1389 "../../../qtremoteobjects/src/repparser/parser.g"

    case 151:
    {
        setErrorString(QStringLiteral("POD: Can only be used in global scope"));
        return false;
    }
    break;

#line 1398 "../../../qtremoteobjects/src/repparser/parser.g"

    case 152:
    {
        setErrorString(QStringLiteral("USE_ENUM: Can only be used in global scope"));
        return false;
    }
    break;

#line 1407 "../../../qtremoteobjects/src/repparser/parser.g"

    case 153:
    {
        setErrorString(QStringLiteral("SIGNAL: Can only be used in class scope"));
        return false;
    }
    break;

#line 1416 "../../../qtremoteobjects/src/repparser/parser.g"

    case 154:
    {
        setErrorString(QStringLiteral("SLOT: Can only be used in class scope"));
        return false;
    }
    break;

#line 1425 "../../../qtremoteobjects/src/repparser/parser.g"

    case 155:
    {
        setErrorString(QStringLiteral("PROP: Can only be used in class scope"));
        return false;
    }
    break;

#line 1434 "../../../qtremoteobjects/src/repparser/parser.g"

    case 156:
    {
        setErrorString(QStringLiteral("MODEL: Can only be used in class scope"));
        return false;
    }
    break;

#line 1444 "../../../qtremoteobjects/src/repparser/parser.g"

    case 157:
    {
        setErrorString(QStringLiteral("CLASS: Can only be used in class scope"));
        return false;
    }
    break;

#line 1453 "../../../qtremoteobjects/src/repparser/parser.g"

    } // switch
    return true;
}
