DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (409f3966645a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "frontend/BinToken.h"

#include "mozilla/Maybe.h"

#include <sys/types.h>

#include "frontend/BinSourceRuntimeSupport.h"
#include "frontend/TokenStream.h"
#include "js/Result.h"
#include "vm/Runtime.h"

namespace js {
namespace frontend {

const BinaryASTSupport::CharSlice BINKIND_DESCRIPTIONS[] = {
#define WITH_VARIANT(_, SPEC_NAME) BinaryASTSupport::CharSlice(SPEC_NAME, sizeof(SPEC_NAME) - 1),
    FOR_EACH_BIN_KIND(WITH_VARIANT)
#undef WITH_VARIANT
};

const BinaryASTSupport::CharSlice BINFIELD_DESCRIPTIONS[] = {
#define WITH_VARIANT(_, SPEC_NAME) BinaryASTSupport::CharSlice(SPEC_NAME, sizeof(SPEC_NAME) - 1),
    FOR_EACH_BIN_FIELD(WITH_VARIANT)
#undef WITH_VARIANT
};

const BinaryASTSupport::CharSlice BINVARIANT_DESCRIPTIONS[] = {
#define WITH_VARIANT(_, SPEC_NAME) BinaryASTSupport::CharSlice(SPEC_NAME, sizeof(SPEC_NAME) - 1),
    FOR_EACH_BIN_VARIANT(WITH_VARIANT)
#undef WITH_VARIANT
};

const BinaryASTSupport::CharSlice&
getBinKind(const BinKind& variant)
{
    return BINKIND_DESCRIPTIONS[static_cast<size_t>(variant)];
}

const BinaryASTSupport::CharSlice&
getBinVariant(const BinVariant& variant)
{
    return BINVARIANT_DESCRIPTIONS[static_cast<size_t>(variant)];
}

const BinaryASTSupport::CharSlice&
getBinField(const BinField& variant)
{
    return BINFIELD_DESCRIPTIONS[static_cast<size_t>(variant)];
}

const char* describeBinKind(const BinKind& variant)
{
    return getBinKind(variant).begin();
}

const char* describeBinField(const BinField& variant)
{
    return getBinField(variant).begin();
}

const char* describeBinVariant(const BinVariant& variant)
{
    return getBinVariant(variant).begin();
}

} // namespace frontend

BinaryASTSupport::BinaryASTSupport()
  : binKindMap_(frontend::BINKIND_LIMIT)
  , binFieldMap_(frontend::BINFIELD_LIMIT)
  , binVariantMap_(frontend::BINVARIANT_LIMIT)
{
}

JS::Result<const js::frontend::BinKind*>
BinaryASTSupport::binKind(JSContext* cx, const CharSlice key)
{
    if (binKindMap_.empty()) {
        for (size_t i = 0; i < frontend::BINKIND_LIMIT; ++i) {
            const BinKind variant = static_cast<BinKind>(i);
            const CharSlice& key = getBinKind(variant);
            auto ptr = binKindMap_.lookupForAdd(key);
            MOZ_ASSERT(!ptr);
            if (!binKindMap_.add(ptr, key, variant))
                return ReportOutOfMemoryResult(cx);
        }
    }

    auto ptr = binKindMap_.lookup(key);
    if (!ptr)
        return nullptr;

    return &ptr->value();
}

JS::Result<const js::frontend::BinVariant*>
BinaryASTSupport::binVariant(JSContext* cx, const CharSlice key)
{
    if (binVariantMap_.empty()) {
        for (size_t i = 0; i < frontend::BINVARIANT_LIMIT; ++i) {
            const BinVariant variant = static_cast<BinVariant>(i);
            const CharSlice& key = getBinVariant(variant);
            auto ptr = binVariantMap_.lookupForAdd(key);
            MOZ_ASSERT(!ptr);
            if (!binVariantMap_.add(ptr, key, variant))
                return ReportOutOfMemoryResult(cx);
        }
    }


    auto ptr = binVariantMap_.lookup(key);
    if (!ptr)
        return nullptr;

    return &ptr->value();
}

} // namespace js