2019-06-25 20:48:56 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-06-25 20:48:56 +08:00
|
|
|
//
|
|
|
|
// 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 SANDBOXED_API_TOOLS_CLANG_GENERATOR_TYPES_H_
|
|
|
|
#define SANDBOXED_API_TOOLS_CLANG_GENERATOR_TYPES_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-11-15 00:18:42 +08:00
|
|
|
#include "absl/container/flat_hash_set.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2022-11-15 00:18:42 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
|
|
|
|
using QualTypeSet =
|
|
|
|
llvm::SetVector<clang::QualType, std::vector<clang::QualType>,
|
|
|
|
llvm::SmallPtrSet<clang::QualType, 8>>;
|
|
|
|
|
2022-11-02 15:42:51 +08:00
|
|
|
// Returns whether a type is "simple". Simple types are arithmetic types,
|
2019-06-25 20:48:56 +08:00
|
|
|
// i.e. signed and unsigned integer, character and bool types, as well as
|
|
|
|
// "void".
|
|
|
|
inline bool IsSimple(clang::QualType qual) {
|
|
|
|
return qual->isArithmeticType() || qual->isVoidType();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsPointerOrReference(clang::QualType qual) {
|
2022-11-07 16:37:13 +08:00
|
|
|
return qual->isAnyPointerType() || qual->isReferenceType();
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
|
2020-10-06 16:04:11 +08:00
|
|
|
class TypeCollector {
|
|
|
|
public:
|
2022-11-15 00:18:42 +08:00
|
|
|
// Records the source order of the given type in the current translation unit.
|
|
|
|
// This is different from collecting related types, as the emitter also needs
|
|
|
|
// to know in which order to emit typedefs vs forward decls, etc. and
|
|
|
|
// QualTypes only refer to complete definitions.
|
|
|
|
void RecordOrderedDecl(clang::TypeDecl* type_decl);
|
|
|
|
|
2020-10-06 16:04:11 +08:00
|
|
|
// Computes the transitive closure of all types that a type depends on. Those
|
|
|
|
// are types that need to be declared before a declaration of the type denoted
|
|
|
|
// by the qual parameter is valid. For example, given
|
|
|
|
// struct SubStruct { bool truth_value; };
|
|
|
|
// struct AggregateStruct {
|
|
|
|
// int int_member;
|
|
|
|
// SubStruct struct_member;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// calling this function on the type "AggregateStruct" yields these types:
|
|
|
|
// int
|
|
|
|
// bool
|
2022-11-15 00:18:42 +08:00
|
|
|
// SubStruct
|
2020-10-06 16:04:11 +08:00
|
|
|
void CollectRelatedTypes(clang::QualType qual);
|
|
|
|
|
2022-11-15 00:18:42 +08:00
|
|
|
// Returns the declarations for the collected types in source order.
|
|
|
|
std::vector<clang::TypeDecl*> GetTypeDeclarations();
|
2020-10-06 16:04:11 +08:00
|
|
|
|
|
|
|
private:
|
2022-11-15 00:18:42 +08:00
|
|
|
std::vector<clang::TypeDecl*> ordered_decls_;
|
2020-10-06 16:04:11 +08:00
|
|
|
QualTypeSet collected_;
|
|
|
|
QualTypeSet seen_;
|
|
|
|
};
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
// Maps a qualified type to a fully qualified SAPI-compatible type name. This
|
2022-11-02 15:42:51 +08:00
|
|
|
// is used for the generated code that invokes the actual function call IPC.
|
2019-06-25 20:48:56 +08:00
|
|
|
// If no mapping can be found, "int" is assumed.
|
2020-05-15 21:44:44 +08:00
|
|
|
std::string MapQualType(const clang::ASTContext& context, clang::QualType qual);
|
2019-06-25 20:48:56 +08:00
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
// Maps a qualified type to a fully qualified C++ type name. Transforms C-only
|
|
|
|
// constructs such as _Bool to bool.
|
|
|
|
std::string MapQualTypeParameterForCxx(const clang::ASTContext& context,
|
|
|
|
clang::QualType qual);
|
|
|
|
|
2019-06-25 20:48:56 +08:00
|
|
|
// Maps a qualified type used as a function parameter to a type name compatible
|
|
|
|
// with the generated Sandboxed API.
|
2020-05-15 21:44:44 +08:00
|
|
|
std::string MapQualTypeParameter(const clang::ASTContext& context,
|
|
|
|
clang::QualType qual);
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
// Maps a qualified type used as a function return type to a type name
|
2020-05-15 21:44:44 +08:00
|
|
|
// compatible with the generated Sandboxed API. Uses MapQualTypeParameter() and
|
2022-11-02 15:42:51 +08:00
|
|
|
// wraps the type in an "absl::StatusOr<>" if qual is non-void. Otherwise
|
|
|
|
// returns "absl::Status".
|
2020-05-15 21:44:44 +08:00
|
|
|
std::string MapQualTypeReturn(const clang::ASTContext& context,
|
|
|
|
clang::QualType qual);
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
} // namespace sapi
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_TOOLS_CLANG_GENERATOR_TYPES_H_
|