Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template instantiation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
| 13 | #include "Sema.h" |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/Expr.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 19 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" // for the identifier table |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===/ |
| 27 | // Template Instantiation Support |
| 28 | //===----------------------------------------------------------------------===/ |
| 29 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 30 | Sema::InstantiatingTemplate:: |
| 31 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 32 | ClassTemplateSpecializationDecl *Entity, |
| 33 | SourceRange InstantiationRange) |
| 34 | : SemaRef(SemaRef) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 35 | |
| 36 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 37 | InstantiationRange); |
| 38 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 39 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 40 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 41 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 42 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 43 | Inst.TemplateArgs = 0; |
| 44 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 45 | Inst.InstantiationRange = InstantiationRange; |
| 46 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 47 | Invalid = false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 52 | SourceLocation PointOfInstantiation, |
| 53 | TemplateDecl *Template, |
| 54 | const TemplateArgument *TemplateArgs, |
| 55 | unsigned NumTemplateArgs, |
| 56 | SourceRange InstantiationRange) |
| 57 | : SemaRef(SemaRef) { |
| 58 | |
| 59 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 60 | InstantiationRange); |
| 61 | if (!Invalid) { |
| 62 | ActiveTemplateInstantiation Inst; |
| 63 | Inst.Kind |
| 64 | = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; |
| 65 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 66 | Inst.Entity = reinterpret_cast<uintptr_t>(Template); |
| 67 | Inst.TemplateArgs = TemplateArgs; |
| 68 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 69 | Inst.InstantiationRange = InstantiationRange; |
| 70 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 71 | Invalid = false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | Sema::InstantiatingTemplate::~InstantiatingTemplate() { |
| 76 | if (!Invalid) |
| 77 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
| 78 | } |
| 79 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 80 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 81 | SourceLocation PointOfInstantiation, |
| 82 | SourceRange InstantiationRange) { |
| 83 | if (SemaRef.ActiveTemplateInstantiations.size() |
| 84 | <= SemaRef.getLangOptions().InstantiationDepth) |
| 85 | return false; |
| 86 | |
| 87 | SemaRef.Diag(PointOfInstantiation, |
| 88 | diag::err_template_recursion_depth_exceeded) |
| 89 | << SemaRef.getLangOptions().InstantiationDepth |
| 90 | << InstantiationRange; |
| 91 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| 92 | << SemaRef.getLangOptions().InstantiationDepth; |
| 93 | return true; |
| 94 | } |
| 95 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 96 | /// \brief Post-diagnostic hook for printing the instantiation stack. |
| 97 | void Sema::PrintInstantiationStackHook(unsigned, void *Cookie) { |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 98 | Sema &SemaRef = *static_cast<Sema*>(Cookie); |
| 99 | SemaRef.PrintInstantiationStack(); |
| 100 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 101 | = SemaRef.ActiveTemplateInstantiations.back(); |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /// \brief Prints the current instantiation stack through a series of |
| 105 | /// notes. |
| 106 | void Sema::PrintInstantiationStack() { |
| 107 | for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator |
| 108 | Active = ActiveTemplateInstantiations.rbegin(), |
| 109 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 110 | Active != ActiveEnd; |
| 111 | ++Active) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 112 | switch (Active->Kind) { |
| 113 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
| 114 | ClassTemplateSpecializationDecl *Spec |
| 115 | = cast<ClassTemplateSpecializationDecl>((Decl*)Active->Entity); |
| 116 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 117 | diag::note_template_class_instantiation_here) |
| 118 | << Context.getTypeDeclType(Spec) |
| 119 | << Active->InstantiationRange; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { |
| 124 | TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); |
| 125 | std::string TemplateArgsStr |
| 126 | = ClassTemplateSpecializationType::PrintTemplateArgumentList( |
| 127 | Active->TemplateArgs, |
| 128 | Active->NumTemplateArgs); |
| 129 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 130 | diag::note_default_arg_instantiation_here) |
| 131 | << (Template->getNameAsString() + TemplateArgsStr) |
| 132 | << Active->InstantiationRange; |
| 133 | break; |
| 134 | } |
| 135 | } |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 139 | //===----------------------------------------------------------------------===/ |
| 140 | // Template Instantiation for Types |
| 141 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 142 | namespace { |
| 143 | class VISIBILITY_HIDDEN TemplateTypeInstantiator { |
| 144 | Sema &SemaRef; |
| 145 | const TemplateArgument *TemplateArgs; |
| 146 | unsigned NumTemplateArgs; |
| 147 | SourceLocation Loc; |
| 148 | DeclarationName Entity; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 150 | public: |
| 151 | TemplateTypeInstantiator(Sema &SemaRef, |
| 152 | const TemplateArgument *TemplateArgs, |
| 153 | unsigned NumTemplateArgs, |
| 154 | SourceLocation Loc, |
| 155 | DeclarationName Entity) |
| 156 | : SemaRef(SemaRef), TemplateArgs(TemplateArgs), |
| 157 | NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { } |
| 158 | |
| 159 | QualType operator()(QualType T) const { return Instantiate(T); } |
| 160 | |
| 161 | QualType Instantiate(QualType T) const; |
| 162 | |
| 163 | // Declare instantiate functions for each type. |
| 164 | #define TYPE(Class, Base) \ |
| 165 | QualType Instantiate##Class##Type(const Class##Type *T, \ |
| 166 | unsigned Quals) const; |
| 167 | #define ABSTRACT_TYPE(Class, Base) |
| 168 | #include "clang/AST/TypeNodes.def" |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | QualType |
| 173 | TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T, |
| 174 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 175 | // FIXME: Implement this |
| 176 | assert(false && "Cannot instantiate ExtQualType yet"); |
| 177 | return QualType(); |
| 178 | } |
| 179 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 180 | QualType |
| 181 | TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T, |
| 182 | unsigned Quals) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 183 | assert(false && "Builtin types are not dependent and cannot be instantiated"); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 184 | return QualType(T, Quals); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 187 | QualType |
| 188 | TemplateTypeInstantiator:: |
| 189 | InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 190 | // FIXME: Implement this |
| 191 | assert(false && "Cannot instantiate FixedWidthIntType yet"); |
| 192 | return QualType(); |
| 193 | } |
| 194 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 195 | QualType |
| 196 | TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T, |
| 197 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 198 | // FIXME: Implement this |
| 199 | assert(false && "Cannot instantiate ComplexType yet"); |
| 200 | return QualType(); |
| 201 | } |
| 202 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 203 | QualType |
| 204 | TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T, |
| 205 | unsigned Quals) const { |
| 206 | QualType PointeeType = Instantiate(T->getPointeeType()); |
| 207 | if (PointeeType.isNull()) |
| 208 | return QualType(); |
| 209 | |
| 210 | return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 213 | QualType |
| 214 | TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T, |
| 215 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 216 | // FIXME: Implement this |
| 217 | assert(false && "Cannot instantiate BlockPointerType yet"); |
| 218 | return QualType(); |
| 219 | } |
| 220 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 221 | QualType |
| 222 | TemplateTypeInstantiator::InstantiateReferenceType(const ReferenceType *T, |
| 223 | unsigned Quals) const { |
| 224 | QualType ReferentType = Instantiate(T->getPointeeType()); |
| 225 | if (ReferentType.isNull()) |
| 226 | return QualType(); |
| 227 | |
| 228 | return SemaRef.BuildReferenceType(ReferentType, Quals, Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 231 | QualType |
| 232 | TemplateTypeInstantiator:: |
| 233 | InstantiateMemberPointerType(const MemberPointerType *T, |
| 234 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 235 | // FIXME: Implement this |
| 236 | assert(false && "Cannot instantiate MemberPointerType yet"); |
| 237 | return QualType(); |
| 238 | } |
| 239 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 240 | QualType |
| 241 | TemplateTypeInstantiator:: |
| 242 | InstantiateConstantArrayType(const ConstantArrayType *T, |
| 243 | unsigned Quals) const { |
| 244 | QualType ElementType = Instantiate(T->getElementType()); |
| 245 | if (ElementType.isNull()) |
| 246 | return ElementType; |
| 247 | |
| 248 | // Build a temporary integer literal to specify the size for |
| 249 | // BuildArrayType. Since we have already checked the size as part of |
| 250 | // creating the dependent array type in the first place, we know |
| 251 | // there aren't any errors. |
Douglas Gregor | 8d21721 | 2009-03-09 20:07:22 +0000 | [diff] [blame] | 252 | // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes |
| 253 | // problems that I have yet to investigate. |
| 254 | IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 255 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 256 | &ArraySize, T->getIndexTypeQualifier(), |
| 257 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 260 | QualType |
| 261 | TemplateTypeInstantiator:: |
| 262 | InstantiateIncompleteArrayType(const IncompleteArrayType *T, |
| 263 | unsigned Quals) const { |
| 264 | QualType ElementType = Instantiate(T->getElementType()); |
| 265 | if (ElementType.isNull()) |
| 266 | return ElementType; |
| 267 | |
| 268 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 269 | 0, T->getIndexTypeQualifier(), |
| 270 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 273 | QualType |
| 274 | TemplateTypeInstantiator:: |
| 275 | InstantiateVariableArrayType(const VariableArrayType *T, |
| 276 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 277 | // FIXME: Implement this |
| 278 | assert(false && "Cannot instantiate VariableArrayType yet"); |
| 279 | return QualType(); |
| 280 | } |
| 281 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 282 | QualType |
| 283 | TemplateTypeInstantiator:: |
| 284 | InstantiateDependentSizedArrayType(const DependentSizedArrayType *T, |
| 285 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 286 | // FIXME: Implement this |
| 287 | assert(false && "Cannot instantiate DependentSizedArrayType yet"); |
| 288 | return QualType(); |
| 289 | } |
| 290 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 291 | QualType |
| 292 | TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T, |
| 293 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 294 | // FIXME: Implement this |
| 295 | assert(false && "Cannot instantiate VectorType yet"); |
| 296 | return QualType(); |
| 297 | } |
| 298 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 299 | QualType |
| 300 | TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T, |
| 301 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 302 | // FIXME: Implement this |
| 303 | assert(false && "Cannot instantiate ExtVectorType yet"); |
| 304 | return QualType(); |
| 305 | } |
| 306 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 307 | QualType |
| 308 | TemplateTypeInstantiator:: |
| 309 | InstantiateFunctionProtoType(const FunctionProtoType *T, |
| 310 | unsigned Quals) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 311 | QualType ResultType = Instantiate(T->getResultType()); |
| 312 | if (ResultType.isNull()) |
| 313 | return ResultType; |
| 314 | |
| 315 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 316 | for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(), |
| 317 | ParamEnd = T->arg_type_end(); |
| 318 | Param != ParamEnd; ++Param) { |
| 319 | QualType P = Instantiate(*Param); |
| 320 | if (P.isNull()) |
| 321 | return P; |
| 322 | |
| 323 | ParamTypes.push_back(P); |
| 324 | } |
| 325 | |
| 326 | return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0], |
| 327 | ParamTypes.size(), |
| 328 | T->isVariadic(), T->getTypeQuals(), |
| 329 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 332 | QualType |
| 333 | TemplateTypeInstantiator:: |
| 334 | InstantiateFunctionNoProtoType(const FunctionNoProtoType *T, |
| 335 | unsigned Quals) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 336 | assert(false && "Functions without prototypes cannot be dependent."); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 337 | return QualType(); |
| 338 | } |
| 339 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 340 | QualType |
| 341 | TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T, |
| 342 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 343 | // FIXME: Implement this |
| 344 | assert(false && "Cannot instantiate TypedefType yet"); |
| 345 | return QualType(); |
| 346 | } |
| 347 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 348 | QualType |
| 349 | TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T, |
| 350 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 351 | // FIXME: Implement this |
| 352 | assert(false && "Cannot instantiate TypeOfExprType yet"); |
| 353 | return QualType(); |
| 354 | } |
| 355 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 356 | QualType |
| 357 | TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T, |
| 358 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 359 | // FIXME: Implement this |
| 360 | assert(false && "Cannot instantiate TypeOfType yet"); |
| 361 | return QualType(); |
| 362 | } |
| 363 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 364 | QualType |
| 365 | TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T, |
| 366 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 367 | // FIXME: Implement this |
| 368 | assert(false && "Cannot instantiate RecordType yet"); |
| 369 | return QualType(); |
| 370 | } |
| 371 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 372 | QualType |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 373 | TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T, |
| 374 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 375 | // FIXME: Implement this |
| 376 | assert(false && "Cannot instantiate EnumType yet"); |
| 377 | return QualType(); |
| 378 | } |
| 379 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 380 | QualType |
| 381 | TemplateTypeInstantiator:: |
| 382 | InstantiateTemplateTypeParmType(const TemplateTypeParmType *T, |
| 383 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 384 | if (T->getDepth() == 0) { |
| 385 | // Replace the template type parameter with its corresponding |
| 386 | // template argument. |
| 387 | assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args"); |
| 388 | assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type && |
| 389 | "Template argument kind mismatch"); |
| 390 | QualType Result = TemplateArgs[T->getIndex()].getAsType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 391 | if (Result.isNull() || !Quals) |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 392 | return Result; |
| 393 | |
| 394 | // C++ [dcl.ref]p1: |
| 395 | // [...] Cv-qualified references are ill-formed except when |
| 396 | // the cv-qualifiers are introduced through the use of a |
| 397 | // typedef (7.1.3) or of a template type argument (14.3), in |
| 398 | // which case the cv-qualifiers are ignored. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 399 | if (Quals && Result->isReferenceType()) |
| 400 | Quals = 0; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 402 | return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | // The template type parameter comes from an inner template (e.g., |
| 406 | // the template parameter list of a member template inside the |
| 407 | // template we are instantiating). Create a new template type |
| 408 | // parameter with the template "level" reduced by one. |
| 409 | return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1, |
| 410 | T->getIndex(), |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 411 | T->getName()) |
| 412 | .getQualifiedType(Quals); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 415 | QualType |
| 416 | TemplateTypeInstantiator:: |
| 417 | InstantiateClassTemplateSpecializationType( |
| 418 | const ClassTemplateSpecializationType *T, |
| 419 | unsigned Quals) const { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 420 | llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs; |
| 421 | InstantiatedTemplateArgs.reserve(T->getNumArgs()); |
| 422 | for (ClassTemplateSpecializationType::iterator Arg = T->begin(), |
| 423 | ArgEnd = T->end(); |
| 424 | Arg != ArgEnd; ++Arg) { |
| 425 | switch (Arg->getKind()) { |
| 426 | case TemplateArgument::Type: { |
| 427 | QualType T = SemaRef.InstantiateType(Arg->getAsType(), |
| 428 | TemplateArgs, NumTemplateArgs, |
| 429 | Arg->getLocation(), |
| 430 | DeclarationName()); |
| 431 | if (T.isNull()) |
| 432 | return QualType(); |
| 433 | |
| 434 | InstantiatedTemplateArgs.push_back( |
| 435 | TemplateArgument(Arg->getLocation(), T)); |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | case TemplateArgument::Declaration: |
| 440 | case TemplateArgument::Integral: |
| 441 | InstantiatedTemplateArgs.push_back(*Arg); |
| 442 | break; |
| 443 | |
| 444 | case TemplateArgument::Expression: |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 445 | Sema::OwningExprResult E |
| 446 | = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs, |
| 447 | NumTemplateArgs); |
| 448 | if (E.isInvalid()) |
| 449 | return QualType(); |
| 450 | InstantiatedTemplateArgs.push_back((Expr *)E.release()); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // FIXME: We're missing the locations of the template name, '<', and |
| 456 | // '>'. |
| 457 | return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()), |
| 458 | Loc, |
| 459 | SourceLocation(), |
| 460 | &InstantiatedTemplateArgs[0], |
| 461 | InstantiatedTemplateArgs.size(), |
| 462 | SourceLocation()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 465 | QualType |
| 466 | TemplateTypeInstantiator:: |
| 467 | InstantiateObjCInterfaceType(const ObjCInterfaceType *T, |
| 468 | unsigned Quals) const { |
| 469 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 470 | return QualType(); |
| 471 | } |
| 472 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 473 | QualType |
| 474 | TemplateTypeInstantiator:: |
| 475 | InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T, |
| 476 | unsigned Quals) const { |
| 477 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 478 | return QualType(); |
| 479 | } |
| 480 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 481 | QualType |
| 482 | TemplateTypeInstantiator:: |
| 483 | InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T, |
| 484 | unsigned Quals) const { |
| 485 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 486 | return QualType(); |
| 487 | } |
| 488 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 489 | QualType |
| 490 | TemplateTypeInstantiator:: |
| 491 | InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T, |
| 492 | unsigned Quals) const { |
| 493 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 494 | return QualType(); |
| 495 | } |
| 496 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 497 | /// \brief The actual implementation of Sema::InstantiateType(). |
| 498 | QualType TemplateTypeInstantiator::Instantiate(QualType T) const { |
| 499 | // If T is not a dependent type, there is nothing to do. |
| 500 | if (!T->isDependentType()) |
| 501 | return T; |
| 502 | |
| 503 | switch (T->getTypeClass()) { |
| 504 | #define TYPE(Class, Base) \ |
| 505 | case Type::Class: \ |
| 506 | return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \ |
| 507 | T.getCVRQualifiers()); |
| 508 | #define ABSTRACT_TYPE(Class, Base) |
| 509 | #include "clang/AST/TypeNodes.def" |
| 510 | } |
| 511 | |
| 512 | assert(false && "Not all types have been decoded for instantiation"); |
| 513 | return QualType(); |
| 514 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 515 | |
| 516 | /// \brief Instantiate the type T with a given set of template arguments. |
| 517 | /// |
| 518 | /// This routine substitutes the given template arguments into the |
| 519 | /// type T and produces the instantiated type. |
| 520 | /// |
| 521 | /// \param T the type into which the template arguments will be |
| 522 | /// substituted. If this type is not dependent, it will be returned |
| 523 | /// immediately. |
| 524 | /// |
| 525 | /// \param TemplateArgs the template arguments that will be |
| 526 | /// substituted for the top-level template parameters within T. |
| 527 | /// |
| 528 | /// \param NumTemplateArgs the number of template arguments provided |
| 529 | /// by TemplateArgs. |
| 530 | /// |
| 531 | /// \param Loc the location in the source code where this substitution |
| 532 | /// is being performed. It will typically be the location of the |
| 533 | /// declarator (if we're instantiating the type of some declaration) |
| 534 | /// or the location of the type in the source code (if, e.g., we're |
| 535 | /// instantiating the type of a cast expression). |
| 536 | /// |
| 537 | /// \param Entity the name of the entity associated with a declaration |
| 538 | /// being instantiated (if any). May be empty to indicate that there |
| 539 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 540 | /// a cast expression) or that the entity has no name (e.g., an |
| 541 | /// unnamed function parameter). |
| 542 | /// |
| 543 | /// \returns If the instantiation succeeds, the instantiated |
| 544 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
| 545 | QualType Sema::InstantiateType(QualType T, |
| 546 | const TemplateArgument *TemplateArgs, |
| 547 | unsigned NumTemplateArgs, |
| 548 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 549 | assert(!ActiveTemplateInstantiations.empty() && |
| 550 | "Cannot perform an instantiation without some context on the " |
| 551 | "instantiation stack"); |
| 552 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 553 | // If T is not a dependent type, there is nothing to do. |
| 554 | if (!T->isDependentType()) |
| 555 | return T; |
| 556 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 557 | TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs, |
| 558 | Loc, Entity); |
| 559 | return Instantiator(T); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 560 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 561 | |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 562 | //===----------------------------------------------------------------------===/ |
| 563 | // Template Instantiation for Expressions |
| 564 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 565 | namespace { |
| 566 | class VISIBILITY_HIDDEN TemplateExprInstantiator |
| 567 | : public StmtVisitor<TemplateExprInstantiator, Sema::OwningExprResult> { |
| 568 | Sema &SemaRef; |
| 569 | const TemplateArgument *TemplateArgs; |
| 570 | unsigned NumTemplateArgs; |
| 571 | |
| 572 | public: |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 573 | typedef Sema::OwningExprResult OwningExprResult; |
| 574 | |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 575 | TemplateExprInstantiator(Sema &SemaRef, |
| 576 | const TemplateArgument *TemplateArgs, |
| 577 | unsigned NumTemplateArgs) |
| 578 | : SemaRef(SemaRef), TemplateArgs(TemplateArgs), |
| 579 | NumTemplateArgs(NumTemplateArgs) { } |
| 580 | |
| 581 | // FIXME: Once we get closer to completion, replace these |
| 582 | // manually-written declarations with automatically-generated ones |
| 583 | // from clang/AST/StmtNodes.def. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 584 | OwningExprResult VisitIntegerLiteral(IntegerLiteral *E); |
| 585 | OwningExprResult VisitDeclRefExpr(DeclRefExpr *E); |
| 586 | OwningExprResult VisitParenExpr(ParenExpr *E); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 587 | OwningExprResult VisitUnaryOperator(UnaryOperator *E); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 588 | OwningExprResult VisitBinaryOperator(BinaryOperator *E); |
| 589 | OwningExprResult VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
| 590 | OwningExprResult VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); |
| 591 | OwningExprResult VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 592 | |
| 593 | // Base case. I'm supposed to ignore this. |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame] | 594 | Sema::OwningExprResult VisitStmt(Stmt *) { |
| 595 | assert(false && "Cannot instantiate this kind of expression"); |
| 596 | return SemaRef.ExprError(); |
| 597 | } |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 598 | }; |
| 599 | } |
| 600 | |
| 601 | Sema::OwningExprResult |
| 602 | TemplateExprInstantiator::VisitIntegerLiteral(IntegerLiteral *E) { |
| 603 | // FIXME: Can't we just re-use the expression node? |
| 604 | return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(E->getValue(), |
| 605 | E->getType(), |
| 606 | E->getLocation())); |
| 607 | } |
| 608 | |
| 609 | Sema::OwningExprResult |
| 610 | TemplateExprInstantiator::VisitDeclRefExpr(DeclRefExpr *E) { |
| 611 | Decl *D = E->getDecl(); |
| 612 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
| 613 | assert(NTTP->getDepth() == 0 && "No nested templates yet"); |
Douglas Gregor | c971f86 | 2009-03-12 22:20:26 +0000 | [diff] [blame] | 614 | const TemplateArgument &Arg = TemplateArgs[NTTP->getPosition()]; |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 615 | return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral( |
Douglas Gregor | c971f86 | 2009-03-12 22:20:26 +0000 | [diff] [blame] | 616 | *Arg.getAsIntegral(), |
| 617 | Arg.getIntegralType(), |
| 618 | E->getSourceRange().getBegin())); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 619 | } else |
| 620 | assert(false && "Can't handle arbitrary declaration references"); |
| 621 | |
| 622 | return SemaRef.ExprError(); |
| 623 | } |
| 624 | |
| 625 | Sema::OwningExprResult |
| 626 | TemplateExprInstantiator::VisitParenExpr(ParenExpr *E) { |
| 627 | Sema::OwningExprResult SubExpr |
| 628 | = SemaRef.InstantiateExpr(E->getSubExpr(), TemplateArgs, NumTemplateArgs); |
| 629 | if (SubExpr.isInvalid()) |
| 630 | return SemaRef.ExprError(); |
| 631 | |
| 632 | return SemaRef.Owned(new (SemaRef.Context) ParenExpr( |
| 633 | E->getLParen(), E->getRParen(), |
| 634 | (Expr *)SubExpr.release())); |
| 635 | } |
| 636 | |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 637 | Sema::OwningExprResult |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 638 | TemplateExprInstantiator::VisitUnaryOperator(UnaryOperator *E) { |
| 639 | Sema::OwningExprResult Arg = Visit(E->getSubExpr()); |
| 640 | if (Arg.isInvalid()) |
| 641 | return SemaRef.ExprError(); |
| 642 | |
| 643 | return SemaRef.CreateBuiltinUnaryOp(E->getOperatorLoc(), |
| 644 | E->getOpcode(), |
| 645 | move(Arg)); |
| 646 | } |
| 647 | |
| 648 | Sema::OwningExprResult |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame] | 649 | TemplateExprInstantiator::VisitBinaryOperator(BinaryOperator *E) { |
| 650 | Sema::OwningExprResult LHS = Visit(E->getLHS()); |
| 651 | if (LHS.isInvalid()) |
| 652 | return SemaRef.ExprError(); |
| 653 | |
| 654 | Sema::OwningExprResult RHS = Visit(E->getRHS()); |
| 655 | if (RHS.isInvalid()) |
| 656 | return SemaRef.ExprError(); |
| 657 | |
| 658 | Sema::OwningExprResult Result |
| 659 | = SemaRef.CreateBuiltinBinOp(E->getOperatorLoc(), |
| 660 | E->getOpcode(), |
| 661 | (Expr *)LHS.get(), |
| 662 | (Expr *)RHS.get()); |
| 663 | if (Result.isInvalid()) |
| 664 | return SemaRef.ExprError(); |
| 665 | |
| 666 | LHS.release(); |
| 667 | RHS.release(); |
| 668 | return move(Result); |
| 669 | } |
| 670 | |
| 671 | Sema::OwningExprResult |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 672 | TemplateExprInstantiator::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 673 | Sema::OwningExprResult First = Visit(E->getArg(0)); |
| 674 | if (First.isInvalid()) |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 675 | return SemaRef.ExprError(); |
| 676 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 677 | Expr *Args[2] = { (Expr *)First.get(), 0 }; |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 679 | Sema::OwningExprResult Second(SemaRef); |
| 680 | if (E->getNumArgs() == 2) { |
| 681 | Second = Visit(E->getArg(1)); |
| 682 | |
| 683 | if (Second.isInvalid()) |
| 684 | return SemaRef.ExprError(); |
| 685 | |
| 686 | Args[1] = (Expr *)Second.get(); |
| 687 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 688 | |
| 689 | if (!E->isTypeDependent()) { |
| 690 | // Since our original expression was not type-dependent, we do not |
| 691 | // perform lookup again at instantiation time (C++ [temp.dep]p1). |
| 692 | // Instead, we just build the new overloaded operator call |
| 693 | // expression. |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 694 | First.release(); |
| 695 | Second.release(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 696 | return SemaRef.Owned(new (SemaRef.Context) CXXOperatorCallExpr( |
| 697 | SemaRef.Context, |
| 698 | E->getOperator(), |
| 699 | E->getCallee(), |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 700 | Args, E->getNumArgs(), |
| 701 | E->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 702 | E->getOperatorLoc())); |
| 703 | } |
| 704 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 705 | bool isPostIncDec = E->getNumArgs() == 2 && |
| 706 | (E->getOperator() == OO_PlusPlus || E->getOperator() == OO_MinusMinus); |
| 707 | if (E->getNumArgs() == 1 || isPostIncDec) { |
| 708 | if (!Args[0]->getType()->isOverloadableType()) { |
| 709 | // The argument is not of overloadable type, so try to create a |
| 710 | // built-in unary operation. |
| 711 | UnaryOperator::Opcode Opc |
| 712 | = UnaryOperator::getOverloadedOpcode(E->getOperator(), isPostIncDec); |
| 713 | |
| 714 | return SemaRef.CreateBuiltinUnaryOp(E->getOperatorLoc(), Opc, |
| 715 | move(First)); |
| 716 | } |
| 717 | |
| 718 | // Fall through to perform overload resolution |
| 719 | } else { |
| 720 | assert(E->getNumArgs() == 2 && "Expected binary operation"); |
| 721 | |
| 722 | Sema::OwningExprResult Result(SemaRef); |
| 723 | if (!Args[0]->getType()->isOverloadableType() && |
| 724 | !Args[1]->getType()->isOverloadableType()) { |
| 725 | // Neither of the arguments is an overloadable type, so try to |
| 726 | // create a built-in binary operation. |
| 727 | BinaryOperator::Opcode Opc = |
| 728 | BinaryOperator::getOverloadedOpcode(E->getOperator()); |
| 729 | Result = SemaRef.CreateBuiltinBinOp(E->getOperatorLoc(), Opc, |
| 730 | Args[0], Args[1]); |
| 731 | if (Result.isInvalid()) |
| 732 | return SemaRef.ExprError(); |
| 733 | |
| 734 | First.release(); |
| 735 | Second.release(); |
| 736 | return move(Result); |
| 737 | } |
| 738 | |
| 739 | // Fall through to perform overload resolution. |
| 740 | } |
| 741 | |
| 742 | // Compute the set of functions that were found at template |
| 743 | // definition time. |
| 744 | Sema::FunctionSet Functions; |
| 745 | DeclRefExpr *DRE = cast<DeclRefExpr>(E->getCallee()); |
| 746 | OverloadedFunctionDecl *Overloads |
| 747 | = cast<OverloadedFunctionDecl>(DRE->getDecl()); |
| 748 | |
| 749 | // FIXME: Do we have to check |
| 750 | // IsAcceptableNonMemberOperatorCandidate for each of these? |
| 751 | for (OverloadedFunctionDecl::function_iterator |
| 752 | F = Overloads->function_begin(), |
| 753 | FEnd = Overloads->function_end(); |
| 754 | F != FEnd; ++F) |
| 755 | Functions.insert(*F); |
| 756 | |
| 757 | // Add any functions found via argument-dependent lookup. |
| 758 | DeclarationName OpName |
| 759 | = SemaRef.Context.DeclarationNames.getCXXOperatorName(E->getOperator()); |
| 760 | SemaRef.ArgumentDependentLookup(OpName, Args, E->getNumArgs(), Functions); |
| 761 | |
| 762 | // Create the overloaded operator invocation. |
| 763 | if (E->getNumArgs() == 1 || isPostIncDec) { |
| 764 | UnaryOperator::Opcode Opc |
| 765 | = UnaryOperator::getOverloadedOpcode(E->getOperator(), isPostIncDec); |
| 766 | return SemaRef.CreateOverloadedUnaryOp(E->getOperatorLoc(), Opc, |
| 767 | Functions, move(First)); |
| 768 | } |
| 769 | |
| 770 | // FIXME: This would be far less ugly if CreateOverloadedBinOp took |
| 771 | // in ExprArg arguments! |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 772 | BinaryOperator::Opcode Opc = |
| 773 | BinaryOperator::getOverloadedOpcode(E->getOperator()); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 774 | OwningExprResult Result |
| 775 | = SemaRef.CreateOverloadedBinOp(E->getOperatorLoc(), Opc, |
| 776 | Functions, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 777 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 778 | if (Result.isInvalid()) |
| 779 | return SemaRef.ExprError(); |
| 780 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame^] | 781 | First.release(); |
| 782 | Second.release(); |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 783 | return move(Result); |
| 784 | } |
| 785 | |
| 786 | Sema::OwningExprResult |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 787 | TemplateExprInstantiator::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { |
| 788 | bool isSizeOf = E->isSizeOf(); |
| 789 | |
| 790 | if (E->isArgumentType()) { |
| 791 | QualType T = E->getArgumentType(); |
| 792 | if (T->isDependentType()) { |
| 793 | T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs, |
| 794 | /*FIXME*/E->getOperatorLoc(), |
| 795 | &SemaRef.PP.getIdentifierTable().get("sizeof")); |
| 796 | if (T.isNull()) |
| 797 | return SemaRef.ExprError(); |
| 798 | } |
| 799 | |
| 800 | return SemaRef.CreateSizeOfAlignOfExpr(T, E->getOperatorLoc(), isSizeOf, |
| 801 | E->getSourceRange()); |
| 802 | } |
| 803 | |
| 804 | Sema::OwningExprResult Arg = Visit(E->getArgumentExpr()); |
| 805 | if (Arg.isInvalid()) |
| 806 | return SemaRef.ExprError(); |
| 807 | |
| 808 | Sema::OwningExprResult Result |
| 809 | = SemaRef.CreateSizeOfAlignOfExpr((Expr *)Arg.get(), E->getOperatorLoc(), |
| 810 | isSizeOf, E->getSourceRange()); |
| 811 | if (Result.isInvalid()) |
| 812 | return SemaRef.ExprError(); |
| 813 | |
| 814 | Arg.release(); |
| 815 | return move(Result); |
| 816 | } |
| 817 | |
| 818 | Sema::OwningExprResult |
| 819 | TemplateExprInstantiator::VisitCXXTemporaryObjectExpr( |
| 820 | CXXTemporaryObjectExpr *E) { |
| 821 | QualType T = E->getType(); |
| 822 | if (T->isDependentType()) { |
| 823 | T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs, |
| 824 | E->getTypeBeginLoc(), DeclarationName()); |
| 825 | if (T.isNull()) |
| 826 | return SemaRef.ExprError(); |
| 827 | } |
| 828 | |
| 829 | llvm::SmallVector<Expr *, 16> Args; |
| 830 | Args.reserve(E->getNumArgs()); |
| 831 | bool Invalid = false; |
| 832 | for (CXXTemporaryObjectExpr::arg_iterator Arg = E->arg_begin(), |
| 833 | ArgEnd = E->arg_end(); |
| 834 | Arg != ArgEnd; ++Arg) { |
| 835 | OwningExprResult InstantiatedArg = Visit(*Arg); |
| 836 | if (InstantiatedArg.isInvalid()) { |
| 837 | Invalid = true; |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | Args.push_back((Expr *)InstantiatedArg.release()); |
| 842 | } |
| 843 | |
| 844 | if (!Invalid) { |
| 845 | SourceLocation CommaLoc; |
| 846 | // FIXME: HACK! |
| 847 | if (Args.size() > 1) |
| 848 | CommaLoc |
| 849 | = SemaRef.PP.getLocForEndOfToken(Args[0]->getSourceRange().getEnd()); |
| 850 | Sema::ExprResult Result |
| 851 | = SemaRef.ActOnCXXTypeConstructExpr(SourceRange(E->getTypeBeginLoc() |
| 852 | /*, FIXME*/), |
| 853 | T.getAsOpaquePtr(), |
| 854 | /*FIXME*/E->getTypeBeginLoc(), |
| 855 | (void**)&Args[0], Args.size(), |
| 856 | /*HACK*/&CommaLoc, |
| 857 | E->getSourceRange().getEnd()); |
| 858 | if (!Result.isInvalid()) |
| 859 | return SemaRef.Owned(Result); |
| 860 | } |
| 861 | |
| 862 | // Clean up the instantiated arguments. |
| 863 | // FIXME: Would rather do this with RAII. |
| 864 | for (unsigned Idx = 0; Idx < Args.size(); ++Idx) |
| 865 | SemaRef.DeleteExpr(Args[Idx]); |
| 866 | |
| 867 | return SemaRef.ExprError(); |
| 868 | } |
| 869 | |
| 870 | Sema::OwningExprResult |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 871 | Sema::InstantiateExpr(Expr *E, const TemplateArgument *TemplateArgs, |
| 872 | unsigned NumTemplateArgs) { |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 873 | TemplateExprInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs); |
| 874 | return Instantiator.Visit(E); |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 877 | /// \brief Instantiate the base class specifiers of the given class |
| 878 | /// template specialization. |
| 879 | /// |
| 880 | /// Produces a diagnostic and returns true on error, returns false and |
| 881 | /// attaches the instantiated base classes to the class template |
| 882 | /// specialization if successful. |
| 883 | bool |
| 884 | Sema::InstantiateBaseSpecifiers( |
| 885 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 886 | ClassTemplateDecl *ClassTemplate) { |
| 887 | bool Invalid = false; |
| 888 | llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases; |
| 889 | for (ClassTemplateSpecializationDecl::base_class_iterator |
| 890 | Base = ClassTemplate->getTemplatedDecl()->bases_begin(), |
| 891 | BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end(); |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 892 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 893 | if (!Base->getType()->isDependentType()) { |
| 894 | // FIXME: Allocate via ASTContext |
| 895 | InstantiatedBases.push_back(new CXXBaseSpecifier(*Base)); |
| 896 | continue; |
| 897 | } |
| 898 | |
| 899 | QualType BaseType = InstantiateType(Base->getType(), |
| 900 | ClassTemplateSpec->getTemplateArgs(), |
| 901 | ClassTemplateSpec->getNumTemplateArgs(), |
| 902 | Base->getSourceRange().getBegin(), |
| 903 | DeclarationName()); |
| 904 | if (BaseType.isNull()) { |
| 905 | Invalid = true; |
| 906 | continue; |
| 907 | } |
| 908 | |
| 909 | if (CXXBaseSpecifier *InstantiatedBase |
| 910 | = CheckBaseSpecifier(ClassTemplateSpec, |
| 911 | Base->getSourceRange(), |
| 912 | Base->isVirtual(), |
| 913 | Base->getAccessSpecifierAsWritten(), |
| 914 | BaseType, |
| 915 | /*FIXME: Not totally accurate */ |
| 916 | Base->getSourceRange().getBegin())) |
| 917 | InstantiatedBases.push_back(InstantiatedBase); |
| 918 | else |
| 919 | Invalid = true; |
| 920 | } |
| 921 | |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 922 | if (!Invalid && |
| 923 | AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0], |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 924 | InstantiatedBases.size())) |
| 925 | Invalid = true; |
| 926 | |
| 927 | return Invalid; |
| 928 | } |
| 929 | |
| 930 | bool |
| 931 | Sema::InstantiateClassTemplateSpecialization( |
| 932 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 933 | bool ExplicitInstantiation) { |
| 934 | // Perform the actual instantiation on the canonical declaration. |
| 935 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
| 936 | Context.getCanonicalDecl(ClassTemplateSpec)); |
| 937 | |
| 938 | // We can only instantiate something that hasn't already been |
| 939 | // instantiated or specialized. Fail without any diagnostics: our |
| 940 | // caller will provide an error message. |
| 941 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) |
| 942 | return true; |
| 943 | |
| 944 | // FIXME: Push this class template instantiation onto the |
| 945 | // instantiation stack, checking for recursion that exceeds a |
| 946 | // certain depth. |
| 947 | |
| 948 | // FIXME: Perform class template partial specialization to select |
| 949 | // the best template. |
| 950 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
| 951 | |
| 952 | if (!Template->getTemplatedDecl()->getDefinition(Context)) { |
| 953 | Diag(ClassTemplateSpec->getLocation(), |
| 954 | diag::err_template_implicit_instantiate_undefined) |
| 955 | << Context.getTypeDeclType(ClassTemplateSpec); |
| 956 | Diag(Template->getTemplatedDecl()->getLocation(), |
| 957 | diag::note_template_decl_here); |
| 958 | return true; |
| 959 | } |
| 960 | |
| 961 | // Note that this is an instantiation. |
| 962 | ClassTemplateSpec->setSpecializationKind( |
| 963 | ExplicitInstantiation? TSK_ExplicitInstantiation |
| 964 | : TSK_ImplicitInstantiation); |
| 965 | |
| 966 | |
| 967 | bool Invalid = false; |
| 968 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 969 | InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(), |
| 970 | ClassTemplateSpec); |
| 971 | if (Inst) |
| 972 | return true; |
| 973 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 974 | // Enter the scope of this instantiation. We don't use |
| 975 | // PushDeclContext because we don't have a scope. |
| 976 | DeclContext *PreviousContext = CurContext; |
| 977 | CurContext = ClassTemplateSpec; |
| 978 | |
| 979 | // Start the definition of this instantiation. |
| 980 | ClassTemplateSpec->startDefinition(); |
| 981 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 982 | |
| 983 | // Instantiate the base class specifiers. |
| 984 | if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template)) |
| 985 | Invalid = true; |
| 986 | |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 987 | // FIXME: Create the injected-class-name for the |
| 988 | // instantiation. Should this be a typedef or something like it? |
| 989 | |
| 990 | RecordDecl *Pattern = Template->getTemplatedDecl(); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 991 | llvm::SmallVector<DeclTy *, 32> Fields; |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 992 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
| 993 | MemberEnd = Pattern->decls_end(); |
| 994 | Member != MemberEnd; ++Member) { |
| 995 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(*Member)) { |
| 996 | // FIXME: Simplified instantiation of typedefs needs to be made |
| 997 | // "real". |
| 998 | QualType T = Typedef->getUnderlyingType(); |
| 999 | if (T->isDependentType()) { |
| 1000 | T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(), |
| 1001 | ClassTemplateSpec->getNumTemplateArgs(), |
| 1002 | Typedef->getLocation(), |
| 1003 | Typedef->getDeclName()); |
| 1004 | if (T.isNull()) { |
| 1005 | Invalid = true; |
| 1006 | T = Context.IntTy; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | // Create the new typedef |
| 1011 | TypedefDecl *New |
| 1012 | = TypedefDecl::Create(Context, ClassTemplateSpec, |
| 1013 | Typedef->getLocation(), |
| 1014 | Typedef->getIdentifier(), |
| 1015 | T); |
| 1016 | ClassTemplateSpec->addDecl(New); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1017 | } |
| 1018 | else if (FieldDecl *Field = dyn_cast<FieldDecl>(*Member)) { |
| 1019 | // FIXME: Simplified instantiation of fields needs to be made |
| 1020 | // "real". |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 1021 | bool InvalidDecl = false; |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1022 | QualType T = Field->getType(); |
| 1023 | if (T->isDependentType()) { |
| 1024 | T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(), |
| 1025 | ClassTemplateSpec->getNumTemplateArgs(), |
| 1026 | Field->getLocation(), |
| 1027 | Field->getDeclName()); |
| 1028 | if (!T.isNull() && T->isFunctionType()) { |
| 1029 | // C++ [temp.arg.type]p3: |
| 1030 | // If a declaration acquires a function type through a type |
| 1031 | // dependent on a template-parameter and this causes a |
| 1032 | // declaration that does not use the syntactic form of a |
| 1033 | // function declarator to have function type, the program is |
| 1034 | // ill-formed. |
| 1035 | Diag(Field->getLocation(), diag::err_field_instantiates_to_function) |
| 1036 | << T; |
| 1037 | T = QualType(); |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 1038 | InvalidDecl = true; |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 1042 | Expr *BitWidth = Field->getBitWidth(); |
| 1043 | if (InvalidDecl) |
| 1044 | BitWidth = 0; |
| 1045 | if (BitWidth && |
| 1046 | (BitWidth->isTypeDependent() || BitWidth->isValueDependent())) { |
| 1047 | OwningExprResult InstantiatedBitWidth |
| 1048 | = InstantiateExpr(BitWidth, |
| 1049 | ClassTemplateSpec->getTemplateArgs(), |
| 1050 | ClassTemplateSpec->getNumTemplateArgs()); |
| 1051 | if (InstantiatedBitWidth.isInvalid()) { |
| 1052 | Invalid = InvalidDecl = true; |
| 1053 | BitWidth = 0; |
| 1054 | } else |
| 1055 | BitWidth = (Expr *)InstantiatedBitWidth.release(); |
| 1056 | } |
| 1057 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1058 | FieldDecl *New = CheckFieldDecl(Field->getDeclName(), T, |
| 1059 | ClassTemplateSpec, |
| 1060 | Field->getLocation(), |
| 1061 | Field->isMutable(), |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 1062 | BitWidth, |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1063 | Field->getAccess(), |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1064 | 0); |
| 1065 | if (New) { |
| 1066 | ClassTemplateSpec->addDecl(New); |
| 1067 | Fields.push_back(New); |
| 1068 | |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 1069 | if (InvalidDecl) |
| 1070 | New->setInvalidDecl(); |
| 1071 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1072 | if (New->isInvalidDecl()) |
| 1073 | Invalid = true; |
| 1074 | } |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1078 | // Finish checking fields. |
| 1079 | ActOnFields(0, ClassTemplateSpec->getLocation(), ClassTemplateSpec, |
| 1080 | &Fields[0], Fields.size(), SourceLocation(), SourceLocation(), |
| 1081 | 0); |
| 1082 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1083 | // Add any implicitly-declared members that we might need. |
| 1084 | AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec); |
| 1085 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1086 | // Exit the scope of this instantiation. |
| 1087 | CurContext = PreviousContext; |
| 1088 | |
| 1089 | return Invalid; |
| 1090 | } |