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>
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program.h"
|
2020-12-10 18:40:01 +00:00
|
|
|
|
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
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param program the program this namer works with
|
|
|
|
explicit Namer(Program* program);
|
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
|
|
|
|
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);
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// The program storing the symbol table
|
|
|
|
Program* program_ = 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
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param program the program to retrieve names from
|
|
|
|
explicit MangleNamer(Program* program);
|
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
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param program the program to retrieve names from
|
|
|
|
explicit UnsafeNamer(Program* program);
|
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_
|