|
| 1 | +private import cpp |
| 2 | +private import qtil.Qtil |
| 3 | +private import codingstandards.cpp.types.Specifiers |
| 4 | +private import codeql.util.Boolean |
| 5 | + |
| 6 | +Type typeResolvesToTypeStep(Type type) { |
| 7 | + result = type.(Decltype).getBaseType() |
| 8 | + or |
| 9 | + result = type.(TypedefType).getBaseType() |
| 10 | +} |
| 11 | + |
| 12 | +module PointerTo<Qtil::Signature<Type>::Type PointeeType> { |
| 13 | + /** |
| 14 | + * A pointer type that points to a type that resolves to the module's `PointeeType` type parameter. |
| 15 | + */ |
| 16 | + class Type extends Qtil::Final<PointerType>::Type { |
| 17 | + Type() { getBaseType() instanceof PointeeType } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +module ReferenceOf<Qtil::Signature<Type>::Type ReferencedType> { |
| 22 | + /** |
| 23 | + * A reference type that refers to a type that resolves to the module's `ReferencedType` type |
| 24 | + * parameter. |
| 25 | + */ |
| 26 | + class Type extends Qtil::Final<ReferenceType>::Type { |
| 27 | + Type() { getBaseType() instanceof ReferencedType } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * A module for handling complex type resolution in c++, such as a decltype of a const typedef |
| 33 | + * of a struct type, which resolves to a const struct type. |
| 34 | + * |
| 35 | + * Basic usage: |
| 36 | + * - `ResolvesTo<ClassType>::Exactly` is the set of class types, and typedefs/decltypes that resolve |
| 37 | + * to class types exactly (without specifiers). |
| 38 | + * - `resolvedType.resolve()` gets the fully resolved class type for the above. |
| 39 | + * - `ResolvesTo<ClassType>::Specified` is the set of types that resolve to a specified class type. |
| 40 | + * - `ResolvesTo<ClassType>::Ref` is the set of types that resolve to a reference to a class type. |
| 41 | + * - `ResolvesTo<ClassType>::Const` is the set of types that resolve to a const class type. |
| 42 | + * - `ResolvesTo<ClassType>::IgnoringSpecifiers` is the set of types that resolve to a class type |
| 43 | + * ignoring specifiers. |
| 44 | + * - `ResolvesTo<ClassType>::ExactlyOrRef` is the set of types that resolve to a class type and |
| 45 | + * unwraps references. |
| 46 | + * |
| 47 | + * These module classes are preferred to the member predicates on `Type` such as `resolveTypedefs`, |
| 48 | + * `stripSpecifiers`, `getUnderlyingType()`, etc, for the following reasons: |
| 49 | + * - Hopefully the API is clearer and easier to use correctly. |
| 50 | + * - Unlike `resolveTypedefs`, these classes can find types that resolve to other typedefs (via |
| 51 | + * `ResolvesType<SomeTypedefType>::Exactly` etc.), instead of always resolving all typedefs. |
| 52 | + * - The member predicates on `Type` have cases with no result. For instance, if there's a const |
| 53 | + * typedef `const T = F`, but `const F` is never explicitly written in the code, then there is no |
| 54 | + * matching extracted `Type` for `resolveTypedefs` to return, and it will have no result. |
| 55 | + */ |
| 56 | +module ResolvesTo<Qtil::Signature<Type>::Type ResolvedType> { |
| 57 | + private import cpp as cpp |
| 58 | + |
| 59 | + final class CppType = cpp::Type; |
| 60 | + |
| 61 | + /** |
| 62 | + * A type that resolve exactly to the module's `ResolvedType` type parameter. |
| 63 | + * |
| 64 | + * For example, `ResolvesTo<FooType>::Type` is the set of all `FooType`s and types that resolve |
| 65 | + * (through typedefs * and/or decltypes) to `FooType`s. This does _not_ include types that resolve |
| 66 | + * to a const `FooType` (though `FooType` itself may be const). To perform type resolution and |
| 67 | + * check or strip specifiers, see module classes `IgnoringSpecifiers`, `Specified`, `Const`, etc. |
| 68 | + * |
| 69 | + * ``` |
| 70 | + * // Example `ResolvesTo<FooType>::Exactly` types: |
| 71 | + * FooType f; // matches (a FooType) |
| 72 | + * decltype(f); // matches (a decltype of FooType) |
| 73 | + * typedef FooType FT; // matches (a typedef of FooType) |
| 74 | + * FT f2; // matches (a typedef of FooType) |
| 75 | + * decltype(f2); // matches (a decltype of typedef of FooType) |
| 76 | + * typedef FT FT2; // matches (a typedef of typedef of FooType) |
| 77 | + * |
| 78 | + * // Examples types that are not `ResolvesTo<FooType>::Exactly` types: |
| 79 | + * const FooType cf; // does not match (specified FooTypes) |
| 80 | + * FooType& rf = f; // does not match (references to FooTypes) |
| 81 | + * NotFooType nf; // does not match (non FooTypes) |
| 82 | + * ``` |
| 83 | + */ |
| 84 | + class Exactly extends CppType { |
| 85 | + ResolvedType resolvedType; |
| 86 | + |
| 87 | + Exactly() { resolvedType = typeResolvesToTypeStep*(this) } |
| 88 | + |
| 89 | + ResolvedType resolve() { result = resolvedType } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * A type that resolves to a const type that in turn resolves to the module's `ResolvedType` type. |
| 94 | + * |
| 95 | + * For example, `ResolvesTo<FooType>::CvConst` is the set of all const `FooType`s and types that |
| 96 | + * resolve (through typedefs and/or decltypes) to const `FooType`s, including cases involving |
| 97 | + * `const` typedefs, etc. |
| 98 | + * |
| 99 | + * Volatile specifiers are ignored, since const volatile types are still const. |
| 100 | + * |
| 101 | + * For matching both const and non-const types that resolve to `FooType`, see |
| 102 | + * `IgnoringSpecifiers`. |
| 103 | + * |
| 104 | + * ``` |
| 105 | + * // Note that this does NOT match `FooType`s that are not const. |
| 106 | + * FooType f; // does not match (non-const) |
| 107 | + * decltype(f) df; // does not match (non-const) |
| 108 | + * typedef TF = FooType; // does not match (non-const) |
| 109 | + * |
| 110 | + * // Example `ResolvesTo<FooType>::Const` types: |
| 111 | + * const FooType cf; // matches (a const FooType) |
| 112 | + * const volatile FooType cf; // matches (a const FooType, volatile is allowed and ignored) |
| 113 | + * decltype(cf); // matches (a decltype of a const FooType) |
| 114 | + * const decltype(f); // matches (a const decltype of FooType) |
| 115 | + * const decltype(cf); // matches (a const decltype of a const FooType) |
| 116 | + * typedef const FooType CFT; // matches (a typedef of const FooType) |
| 117 | + * const TF ctf; // matches (a const typedef of FooType) |
| 118 | + * |
| 119 | + * // Additional examples types that are not `ResolvesTo<FooType>::Const` types: |
| 120 | + * const FooType &f; // does not match (reference to const FooType) |
| 121 | + * ``` |
| 122 | + */ |
| 123 | + class CvConst extends Specified { |
| 124 | + CvConst() { getASpecifier() instanceof ConstSpecifier } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * A type that resolves to a cv-qualified (or otherwise specified) type that in turn resolves to |
| 129 | + * the module's `ResolvedType` type parameter. |
| 130 | + * |
| 131 | + * For example, `ResolvesTo<FooType>::Specified` is the set of all specified `FooType`s and types |
| 132 | + * that resolve (through typedefs and/or decltypes) to specified `FooType`s, including cases |
| 133 | + * involving `const` and `volatile` typedefs, etc. |
| 134 | + * |
| 135 | + * ``` |
| 136 | + * // Note that this does NOT match `FooType`s that are not specified. |
| 137 | + * FooType f; // does not match (not specified) |
| 138 | + * decltype(f) df; // does not match (not specified) |
| 139 | + * typedef TF = FooType; // does not match (not specified) |
| 140 | + * |
| 141 | + * // Example `ResolvesTo<FooType>::Specified` types: |
| 142 | + * const FooType cf; // matches (a const FooType) |
| 143 | + * volatile FooType vf; // matches (a volatile FooType) |
| 144 | + * const volatile FooType cvf; // matches (a const volatile FooType) |
| 145 | + * decltype(cf); // matches (a decltype of a const FooType) |
| 146 | + * volatile decltype(f); // matches (a volatile decltype of FooType) |
| 147 | + * const decltype(vf); // matches (a const decltype of volatile FooType) |
| 148 | + * const decltype(cf); // matches (a const decltype of const FooType) |
| 149 | + * typedef const FooType CFT; // matches (a typedef of const FooType) |
| 150 | + * const TF ctf; // matches (a const typedef of FooType) |
| 151 | + * volatile TF ctf; // matches (a volatile typedef of FooType) |
| 152 | + * |
| 153 | + * // Additional examples types that are not `ResolvesTo<FooType>::Specified` types: |
| 154 | + * const FooType &f; // does not match (reference to const FooType) |
| 155 | + * ``` |
| 156 | + */ |
| 157 | + class Specified extends CppType { |
| 158 | + ResolvedType resolved; |
| 159 | + |
| 160 | + Specified() { |
| 161 | + resolved = typeResolvesToTypeStep*(this).(SpecifiedType).getBaseType().(Exactly).resolve() |
| 162 | + } |
| 163 | + |
| 164 | + ResolvedType resolve() { result = resolved } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * A class that resolves to the module's `ResolvedType` type parameter, ignoring specifiers. |
| 169 | + */ |
| 170 | + class IgnoringSpecifiers extends CppType { |
| 171 | + ResolvedType resolved; |
| 172 | + |
| 173 | + IgnoringSpecifiers() { |
| 174 | + resolved = this.(Specified).resolve() |
| 175 | + or |
| 176 | + resolved = this.(Exactly).resolve() |
| 177 | + } |
| 178 | + |
| 179 | + ResolvedType resolve() { result = resolved } |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * A type that resolves to a reference to that resolves to the module's `ResolvedType` type |
| 184 | + * parameter. |
| 185 | + * |
| 186 | + * For example, `ResolvesTo<FooType>::Ref` is the set of all references to `FooType`s and types |
| 187 | + * that resolve (through typedefs and/or decltypes) to references to `FooType`s. |
| 188 | + * |
| 189 | + * ``` |
| 190 | + * // Example `ResolvesTo<FooType>::Ref` types: |
| 191 | + * FooType &f; // matches (a reference to FooType) |
| 192 | + * decltype(f); // matches (a decltype of reference to FooType) |
| 193 | + * typedef FooType &FT; // matches (a typedef of ref to FooType) |
| 194 | + * FT f2; // matches (a typedef of ref to FooType) |
| 195 | + * decltype(f2); // matches (a decltype of typedef of ref to FooType) |
| 196 | + * typedef FT FT2; // matches (a typedef of typedef of ref to FooType) |
| 197 | + * |
| 198 | + * // Examples types that are not `ResolvesTo<FooType>::Ref` types: |
| 199 | + * const FooType &cf; // does not match (specified references to FooTypes) |
| 200 | + * FooType rf = f; // does not match (non-rerefence FooTypes) |
| 201 | + * NotFooType &nf; // does not match (non FooTypes) |
| 202 | + * ``` |
| 203 | + */ |
| 204 | + class Ref extends CppType { |
| 205 | + ResolvedType resolved; |
| 206 | + |
| 207 | + Ref() { |
| 208 | + // Note that the extractor appears to perform reference collapsing, so cases like |
| 209 | + // `const T& &` are treated as `const T&`. |
| 210 | + resolved = typeResolvesToTypeStep*(this).(ReferenceType).getBaseType().(Exactly).resolve() |
| 211 | + } |
| 212 | + |
| 213 | + ResolvedType resolve() { result = resolved } |
| 214 | + } |
| 215 | + |
| 216 | + //class Ref = |
| 217 | + //Impl::ResolveRefType; |
| 218 | + /** |
| 219 | + * A type that resolves to a const reference of (or reference to const of) the module's |
| 220 | + * `ResolvedType` type parameter. |
| 221 | + * |
| 222 | + * For example, `ResolvesTo<FooType>::CvConstRef` is the set of all const references to `FooType`s |
| 223 | + * and types that resolve (through typedefs and/or decltypes) to const references to `FooType`s. |
| 224 | + * |
| 225 | + * Volatile specifiers are ignored, since const volatile types are still const. |
| 226 | + * |
| 227 | + * ``` |
| 228 | + * FooType &f; // does not match (not const) |
| 229 | + * const FooType f; // does not match (not a reference) |
| 230 | + * |
| 231 | + * // Example `ResolvesTo<FooType>::CvConstRef` types: |
| 232 | + * const FooType &cf; // matches (a const reference to FooType) |
| 233 | + * const volatile FooType &cf; // matches (a const reference to FooType, volatile is ignored) |
| 234 | + * const decltype(f) cdf; // matches (a const decltype of reference to FooType) |
| 235 | + * decltype(f)& dcf; // matches (a decltype of const reference to FooType) |
| 236 | + * typedef const FooType &CFT; // matches (a typedef of const ref to FooType) |
| 237 | + * const CFT ctf; // matches due to const collapse |
| 238 | + * CFT &ctf; // matches due to reference collapse |
| 239 | + * ``` |
| 240 | + */ |
| 241 | + class CvConstRef extends CppType { |
| 242 | + ResolvedType resolved; |
| 243 | + |
| 244 | + CvConstRef() { |
| 245 | + exists(ReferenceType refType | |
| 246 | + // A type can be a reference to const, but a const type cannot contain a reference. |
| 247 | + // Therefore, we only need to find reference types that resolve to const types. |
| 248 | + // Note that the extractor appears to perform reference collapsing, so cases like |
| 249 | + // `const T& &` are treated as `const T&`. |
| 250 | + refType = typeResolvesToTypeStep*(this) and |
| 251 | + refType.getBaseType() instanceof CvConst |
| 252 | + ) |
| 253 | + } |
| 254 | + |
| 255 | + ResolvedType resolve() { result = resolved } |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * A type that resolves to either a reference to that resolves to the module's `ResolvedType` type |
| 260 | + * parameter, or exactly to the `ResolvedType`. |
| 261 | + */ |
| 262 | + class ExactlyOrRef extends CppType { |
| 263 | + ResolvedType resolved; |
| 264 | + |
| 265 | + ExactlyOrRef() { |
| 266 | + resolved = this.(Ref).resolve() |
| 267 | + or |
| 268 | + resolved = this.(Exactly).resolve() |
| 269 | + } |
| 270 | + |
| 271 | + ResolvedType resolve() { result = resolved } |
| 272 | + } |
| 273 | +} |
0 commit comments