2020-11-10 21:49:56 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SRC_NAMER_H_
|
|
|
|
#define SRC_NAMER_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
2020-12-10 18:40:01 +00:00
|
|
|
#include "src/ast/module.h"
|
|
|
|
|
2020-11-10 21:49:56 +00:00
|
|
|
namespace tint {
|
|
|
|
|
2020-11-13 18:13:24 +00:00
|
|
|
/// Base class for the namers.
|
2020-11-10 21:49:56 +00:00
|
|
|
class Namer {
|
|
|
|
public:
|
|
|
|
/// Constructor
|
2020-12-10 18:40:01 +00:00
|
|
|
/// @param mod the module this namer works with
|
2021-01-11 20:01:12 +00:00
|
|
|
explicit Namer(const ast::Module* mod);
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Destructor
|
2020-11-13 18:13:24 +00:00
|
|
|
virtual ~Namer();
|
2020-11-10 21:49:56 +00:00
|
|
|
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Returns the name for `sym`
|
|
|
|
/// @param sym the symbol to retrieve the name for
|
|
|
|
/// @returns the sanitized version of `name` or "" if not found
|
|
|
|
virtual std::string NameFor(const Symbol& sym) = 0;
|
2020-11-10 21:49:56 +00:00
|
|
|
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Generates a unique name for `prefix`
|
|
|
|
/// @param prefix the prefix name
|
|
|
|
/// @returns the unique name string
|
|
|
|
std::string GenerateName(const std::string& prefix);
|
2020-11-10 21:49:56 +00:00
|
|
|
|
2021-01-11 16:24:32 +00:00
|
|
|
/// Resets the namer, removing all known symbols.
|
|
|
|
void Reset();
|
|
|
|
|
2020-11-13 18:13:24 +00:00
|
|
|
protected:
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Checks if `name` has been used
|
|
|
|
/// @param name the name to check
|
|
|
|
/// @returns true if `name` has already been used
|
|
|
|
bool IsUsed(const std::string& name);
|
|
|
|
|
|
|
|
/// The module storing the symbol table
|
2021-01-11 20:01:12 +00:00
|
|
|
const ast::Module* module_ = nullptr;
|
2020-12-10 18:40:01 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// The list of names taken by the remapper
|
|
|
|
std::unordered_set<std::string> used_;
|
2020-11-10 21:49:56 +00:00
|
|
|
};
|
|
|
|
|
2020-12-10 18:40:01 +00:00
|
|
|
/// A namer class which mangles the name
|
|
|
|
class MangleNamer : public Namer {
|
2020-11-13 18:13:24 +00:00
|
|
|
public:
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Constructor
|
|
|
|
/// @param mod the module to retrieve names from
|
2021-01-11 20:01:12 +00:00
|
|
|
explicit MangleNamer(const ast::Module* mod);
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Destructor
|
|
|
|
~MangleNamer() override;
|
2020-11-13 18:13:24 +00:00
|
|
|
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Returns a mangled name for `sym`
|
|
|
|
/// @param sym the symbol to name
|
|
|
|
/// @returns the name for `sym` or "" if not found
|
|
|
|
std::string NameFor(const Symbol& sym) override;
|
2020-11-13 18:13:24 +00:00
|
|
|
};
|
|
|
|
|
2020-12-10 18:40:01 +00:00
|
|
|
/// A namer which returns the user provided name. This is unsafe in general as
|
|
|
|
/// it passes user provided data through to the backend compiler. It is useful
|
|
|
|
/// for development and debugging.
|
|
|
|
class UnsafeNamer : public Namer {
|
2020-11-13 18:13:24 +00:00
|
|
|
public:
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Constructor
|
|
|
|
/// @param mod the module to retrieve names from
|
2021-01-11 20:01:12 +00:00
|
|
|
explicit UnsafeNamer(const ast::Module* mod);
|
2020-12-10 18:40:01 +00:00
|
|
|
/// Destructor
|
|
|
|
~UnsafeNamer() override;
|
2020-11-13 18:13:24 +00:00
|
|
|
|
2020-12-02 15:31:08 +00:00
|
|
|
/// Returns `name`
|
2020-12-10 18:40:01 +00:00
|
|
|
/// @param sym the symbol
|
|
|
|
/// @returns `name` or "" if not found
|
|
|
|
std::string NameFor(const Symbol& sym) override;
|
2020-11-13 18:13:24 +00:00
|
|
|
};
|
|
|
|
|
2020-11-10 21:49:56 +00:00
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_NAMER_H_
|