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.

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
/* -*- 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/. */

#ifndef frontend_BinSourceSupport_h
#define frontend_BinSourceSupport_h

#include "mozilla/HashFunctions.h"

#include "frontend/BinToken.h"

#include "js/AllocPolicy.h"
#include "js/HashTable.h"
#include "js/Result.h"

namespace js {

// Support for parsing JS Binary ASTs.
struct BinaryASTSupport {
    using BinVariant = js::frontend::BinVariant;
    using BinField = js::frontend::BinField;
    using BinKind = js::frontend::BinKind;

    // A structure designed to perform fast char* + length lookup
    // without copies.
    struct CharSlice {
        const char* start_;
        uint32_t byteLen_;
        CharSlice(const CharSlice& other)
            : start_(other.start_)
            , byteLen_(other.byteLen_)
        {  }
        CharSlice(const char* start, const uint32_t byteLen)
            : start_(start)
            , byteLen_(byteLen)
        { }
        const char* begin() const {
            return start_;
        }
        const char* end() const {
            return start_ + byteLen_;
        }
#ifdef DEBUG
        void dump() const {
            for (auto c: *this)
                fprintf(stderr, "%c", c);

            fprintf(stderr, " (%d)", byteLen_);
        }
#endif // DEBUG

        typedef const CharSlice Lookup;
        static js::HashNumber hash(Lookup l) {
            return mozilla::HashString(l.start_, l.byteLen_);
        }
        static bool match(const Lookup key, Lookup lookup) {
            if (key.byteLen_ != lookup.byteLen_)
                return false;
            return strncmp(key.start_, lookup.start_, key.byteLen_) == 0;
        }
    };

    BinaryASTSupport();

    JS::Result<const BinVariant*>  binVariant(JSContext*, const CharSlice);
    JS::Result<const BinField*> binField(JSContext*, const CharSlice);
    JS::Result<const BinKind*> binKind(JSContext*,  const CharSlice);

  private:
    // A HashMap that can be queried without copies from a CharSlice key.
    // Initialized on first call. Keys are CharSlices into static strings.
    using BinKindMap = js::HashMap<const CharSlice, BinKind, CharSlice, js::SystemAllocPolicy>;
    BinKindMap binKindMap_;

    using BinFieldMap = js::HashMap<const CharSlice, BinField, CharSlice, js::SystemAllocPolicy>;
    BinFieldMap binFieldMap_;

    using BinVariantMap = js::HashMap<const CharSlice, BinVariant, CharSlice, js::SystemAllocPolicy>;
    BinVariantMap binVariantMap_;

};

} // namespace js

#endif // frontend_BinSourceSupport_h