2023-12-19 19:57:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "oi/type_graph/TypeGraph.h"
|
|
|
|
|
|
|
|
namespace clang {
|
2024-04-23 18:11:58 +01:00
|
|
|
class AttributedType;
|
2023-12-19 19:57:44 +00:00
|
|
|
class ASTContext;
|
|
|
|
class BuiltinType;
|
|
|
|
class ConstantArrayType;
|
|
|
|
class DecltypeType;
|
|
|
|
class ElaboratedType;
|
|
|
|
class EnumType;
|
|
|
|
class LValueReferenceType;
|
2024-04-16 12:53:36 +01:00
|
|
|
class MemberPointerType;
|
2023-12-19 19:57:44 +00:00
|
|
|
class PointerType;
|
|
|
|
class RecordType;
|
|
|
|
class Sema;
|
|
|
|
class SubstTemplateTypeParmType;
|
|
|
|
class TemplateArgument;
|
|
|
|
class TemplateName;
|
|
|
|
class TemplateSpecializationType;
|
|
|
|
class Type;
|
|
|
|
class TypedefType;
|
|
|
|
class UnaryTransformType;
|
|
|
|
class UsingType;
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
struct ContainerInfo;
|
|
|
|
|
|
|
|
namespace oi::detail::type_graph {
|
|
|
|
|
|
|
|
class Array;
|
|
|
|
class Class;
|
|
|
|
class Enum;
|
|
|
|
class Member;
|
2024-02-15 20:22:38 +00:00
|
|
|
struct Parent;
|
2023-12-19 19:57:44 +00:00
|
|
|
class Primitive;
|
|
|
|
class Reference;
|
|
|
|
class Type;
|
|
|
|
class TypeGraph;
|
|
|
|
class Typedef;
|
|
|
|
struct TemplateParam;
|
|
|
|
|
|
|
|
struct ClangTypeParserOptions {
|
|
|
|
bool chaseRawPointers = false;
|
|
|
|
bool readEnumValues = false;
|
2024-02-14 15:23:35 +00:00
|
|
|
std::set<std::string_view> typesToStub;
|
2024-02-15 19:01:46 +00:00
|
|
|
std::set<std::string_view> mustProcessTemplateParams;
|
2023-12-19 19:57:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ClangTypeParser
|
|
|
|
*
|
|
|
|
* Reads source information from a source file to build a type graph.
|
|
|
|
* Returns a reference to the Type node corresponding to the given clang::Type.
|
|
|
|
*/
|
|
|
|
class ClangTypeParser {
|
|
|
|
public:
|
|
|
|
ClangTypeParser(TypeGraph& typeGraph,
|
|
|
|
const std::vector<std::unique_ptr<ContainerInfo>>& containers,
|
|
|
|
ClangTypeParserOptions options)
|
|
|
|
: typeGraph_{typeGraph}, containers_{containers}, options_{options} {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse from a clang type.
|
|
|
|
Type& parse(clang::ASTContext&, clang::Sema&, const clang::Type&);
|
|
|
|
|
|
|
|
private:
|
|
|
|
TypeGraph& typeGraph_;
|
|
|
|
const std::vector<std::unique_ptr<ContainerInfo>>& containers_;
|
|
|
|
ClangTypeParserOptions options_;
|
|
|
|
clang::ASTContext* ast;
|
|
|
|
clang::Sema* sema;
|
|
|
|
|
|
|
|
uint_fast32_t depth_;
|
|
|
|
std::unordered_map<const clang::Type*, std::reference_wrapper<Type>>
|
|
|
|
clang_types_;
|
|
|
|
|
|
|
|
Type& enumerateType(const clang::Type&);
|
|
|
|
Type& enumerateClass(const clang::RecordType&);
|
|
|
|
Type& enumerateReference(const clang::LValueReferenceType&);
|
|
|
|
Type& enumeratePointer(const clang::PointerType&);
|
2024-04-16 12:53:36 +01:00
|
|
|
Type& enumerateMemberPointer(const clang::MemberPointerType&);
|
2023-12-19 19:57:44 +00:00
|
|
|
Type& enumerateSubstTemplateTypeParm(const clang::SubstTemplateTypeParmType&);
|
|
|
|
Primitive& enumeratePrimitive(const clang::BuiltinType&);
|
|
|
|
Type& enumerateElaboratedType(const clang::ElaboratedType&);
|
|
|
|
Type& enumerateTemplateSpecialization(
|
|
|
|
const clang::TemplateSpecializationType&);
|
|
|
|
Typedef& enumerateTypedef(const clang::TypedefType&);
|
|
|
|
Typedef& enumerateUsing(const clang::UsingType&);
|
|
|
|
Type& enumerateUnaryTransformType(const clang::UnaryTransformType&);
|
|
|
|
Type& enumerateDecltypeType(const clang::DecltypeType&);
|
2024-04-23 18:11:58 +01:00
|
|
|
Type& enumerateAttributed(const clang::AttributedType&);
|
2023-12-19 19:57:44 +00:00
|
|
|
|
|
|
|
Array& enumerateArray(const clang::ConstantArrayType&);
|
|
|
|
Enum& enumerateEnum(const clang::EnumType&);
|
|
|
|
|
|
|
|
void enumerateClassTemplateParams(const clang::RecordType&,
|
|
|
|
std::vector<TemplateParam>&);
|
|
|
|
std::optional<TemplateParam> enumerateTemplateParam(
|
|
|
|
const clang::TemplateArgument&);
|
|
|
|
std::optional<TemplateParam> enumerateTemplateTemplateParam(
|
|
|
|
const clang::TemplateName&);
|
|
|
|
|
2024-02-15 20:22:38 +00:00
|
|
|
void enumerateClassParents(const clang::RecordType&, std::vector<Parent>&);
|
2023-12-19 19:57:44 +00:00
|
|
|
void enumerateClassMembers(const clang::RecordType&, std::vector<Member>&);
|
|
|
|
|
|
|
|
ContainerInfo* getContainerInfo(const std::string& fqName) const;
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
T& makeType(const clang::Type& clangType, Args&&... args) {
|
|
|
|
auto& newType = typeGraph_.makeType<T>(std::forward<Args>(args)...);
|
|
|
|
clang_types_.insert({&clangType, newType});
|
|
|
|
return newType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool chasePointer() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace oi::detail::type_graph
|