1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef SANDBOXED_API_TOOLS_CLANG_GENERATOR_TYPES_H_
16 #define SANDBOXED_API_TOOLS_CLANG_GENERATOR_TYPES_H_
17
18 #include <string>
19 #include <vector>
20
21 #include "absl/container/flat_hash_set.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/Decl.h"
24 #include "clang/AST/Type.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27
28 namespace sapi {
29
30 using QualTypeSet =
31 llvm::SetVector<clang::QualType, std::vector<clang::QualType>,
32 llvm::SmallPtrSet<clang::QualType, 8>>;
33
34 // Returns whether a type is "simple". Simple types are arithmetic types,
35 // i.e. signed and unsigned integer, character and bool types, as well as
36 // "void".
IsSimple(clang::QualType qual)37 inline bool IsSimple(clang::QualType qual) {
38 return qual->isArithmeticType() || qual->isVoidType();
39 }
40
IsPointerOrReference(clang::QualType qual)41 inline bool IsPointerOrReference(clang::QualType qual) {
42 return qual->isAnyPointerType() || qual->isReferenceType();
43 }
44
45 class TypeCollector {
46 public:
47 // Records the source order of the given type in the current translation unit.
48 // This is different from collecting related types, as the emitter also needs
49 // to know in which order to emit typedefs vs forward decls, etc. and
50 // QualTypes only refer to complete definitions.
51 void RecordOrderedDecl(clang::TypeDecl* type_decl);
52
53 // Computes the transitive closure of all types that a type depends on. Those
54 // are types that need to be declared before a declaration of the type denoted
55 // by the qual parameter is valid. For example, given
56 // struct SubStruct { bool truth_value; };
57 // struct AggregateStruct {
58 // int int_member;
59 // SubStruct struct_member;
60 // };
61 //
62 // calling this function on the type "AggregateStruct" yields these types:
63 // int
64 // bool
65 // SubStruct
66 void CollectRelatedTypes(clang::QualType qual);
67
68 // Returns the declarations for the collected types in source order.
69 std::vector<clang::TypeDecl*> GetTypeDeclarations();
70
71 private:
72 std::vector<clang::TypeDecl*> ordered_decls_;
73 QualTypeSet collected_;
74 QualTypeSet seen_;
75 };
76
77 // Maps a qualified type to a fully qualified SAPI-compatible type name. This
78 // is used for the generated code that invokes the actual function call IPC.
79 // If no mapping can be found, "int" is assumed.
80 std::string MapQualType(const clang::ASTContext& context, clang::QualType qual);
81
82 // Maps a qualified type to a fully qualified C++ type name. Transforms C-only
83 // constructs such as _Bool to bool.
84 std::string MapQualTypeParameterForCxx(const clang::ASTContext& context,
85 clang::QualType qual);
86
87 // Maps a qualified type used as a function parameter to a type name compatible
88 // with the generated Sandboxed API.
89 std::string MapQualTypeParameter(const clang::ASTContext& context,
90 clang::QualType qual);
91
92 // Maps a qualified type used as a function return type to a type name
93 // compatible with the generated Sandboxed API. Uses MapQualTypeParameter() and
94 // wraps the type in an "absl::StatusOr<>" if qual is non-void. Otherwise
95 // returns "absl::Status".
96 std::string MapQualTypeReturn(const clang::ASTContext& context,
97 clang::QualType qual);
98
99 } // namespace sapi
100
101 #endif // SANDBOXED_API_TOOLS_CLANG_GENERATOR_TYPES_H_
102