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 (dcc6d7a0dc00)

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 125 126 127 128 129 130 131 132 133 134 135 136 137
/* -*- 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/ParseMaps-inl.h"

#include "jscntxt.h"

#include "frontend/FullParseHandler.h"
#include "frontend/SyntaxParseHandler.h"

using namespace js;
using namespace js::frontend;

void
ParseMapPool::checkInvariants()
{
    /*
     * Having all values be of the same size permits us to easily reuse the
     * allocated space for each of the map types.
     */
    JS_STATIC_ASSERT(sizeof(Definition*) == sizeof(jsatomid));
    JS_STATIC_ASSERT(sizeof(Definition*) == sizeof(DefinitionList));
    JS_STATIC_ASSERT(sizeof(AtomDefnMap::Entry) == sizeof(AtomIndexMap::Entry));
    JS_STATIC_ASSERT(sizeof(AtomDefnMap::Entry) == sizeof(AtomDefnListMap::Entry));
    JS_STATIC_ASSERT(sizeof(AtomMapT::Entry) == sizeof(AtomDefnListMap::Entry));
    /* Ensure that the HasTable::clear goes quickly via memset. */
    JS_STATIC_ASSERT(mozilla::IsPod<AtomIndexMap::WordMap::Entry>::value);
    JS_STATIC_ASSERT(mozilla::IsPod<AtomDefnListMap::WordMap::Entry>::value);
    JS_STATIC_ASSERT(mozilla::IsPod<AtomDefnMap::WordMap::Entry>::value);
}

void
ParseMapPool::purgeAll()
{
    void** end = all.end();
    for (void** it = all.begin(); it != end; ++it)
        js_delete<AtomMapT>(asAtomMap(*it));

    all.clearAndFree();
    recyclable.clearAndFree();
}

void*
ParseMapPool::allocateFresh()
{
    size_t newAllLength = all.length() + 1;
    if (!all.reserve(newAllLength) || !recyclable.reserve(newAllLength))
        return nullptr;

    AtomMapT* map = js_new<AtomMapT>();
    if (!map)
        return nullptr;

    all.infallibleAppend(map);
    return (void*) map;
}

DefinitionList::Node*
DefinitionList::allocNode(ExclusiveContext* cx, LifoAlloc& alloc, uintptr_t head, Node* tail)
{
    Node* result = alloc.new_<Node>(head, tail);
    if (!result)
        ReportOutOfMemory(cx);
    return result;
}

#ifdef DEBUG
template <typename ParseHandler>
void
AtomDecls<ParseHandler>::dump()
{
    for (AtomDefnListRange r = map->all(); !r.empty(); r.popFront()) {
        fprintf(stderr, "atom: ");
        DumpAtom(r.front().key());
        const DefinitionList& dlist = r.front().value();
        for (DefinitionList::Range dr = dlist.all(); !dr.empty(); dr.popFront()) {
            fprintf(stderr, "    defn: %p\n", (void*) dr.front<ParseHandler>());
        }
    }
}

void
DumpAtomDefnMap(const AtomDefnMapPtr& map)
{
    if (map->empty()) {
        fprintf(stderr, "empty\n");
        return;
    }

    for (AtomDefnRange r = map->all(); !r.empty(); r.popFront()) {
        fprintf(stderr, "atom: ");
        DumpAtom(r.front().key());
        fprintf(stderr, "defn: %p\n", (void*) r.front().value().get<FullParseHandler>());
    }
}
#endif

template <typename ParseHandler>
bool
AtomDecls<ParseHandler>::addShadow(JSAtom* atom, typename ParseHandler::DefinitionNode defn)
{
    AtomDefnListAddPtr p = map->lookupForAdd(atom);
    if (!p)
        return map->add(p, atom, DefinitionList(ParseHandler::definitionToBits(defn)));

    return p.value().pushFront<ParseHandler>(cx, alloc, defn);
}

void
frontend::InitAtomMap(frontend::AtomIndexMap* indices, HeapPtrAtom* atoms)
{
    if (indices->isMap()) {
        typedef AtomIndexMap::WordMap WordMap;
        const WordMap& wm = indices->asMap();
        for (WordMap::Range r = wm.all(); !r.empty(); r.popFront()) {
            JSAtom* atom = r.front().key();
            jsatomid index = r.front().value();
            MOZ_ASSERT(index < indices->count());
            atoms[index].init(atom);
        }
    } else {
        const AtomIndexMap::InlineElem* end = indices->inlineEnd();
        for (const AtomIndexMap::InlineElem* it = indices->asInline(); it != end; ++it) {
            JSAtom* atom = it->key;
            if (!atom)
                continue;
            MOZ_ASSERT(it->value < indices->count());
            atoms[it->value].init(atom);
        }
    }
}

template class js::frontend::AtomDecls<FullParseHandler>;
template class js::frontend::AtomDecls<SyntaxParseHandler>;