MinimalAction.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //===--- MinimalAction.cpp - Implement the MinimalAction class ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Chris Lattner and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the MinimalAction interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Parse/Parser.h"
  14. #include "clang/Parse/DeclSpec.h"
  15. #include "clang/Parse/Scope.h"
  16. using namespace clang;
  17. /// TypeNameInfo - A link exists here for each scope that an identifier is
  18. /// defined.
  19. struct TypeNameInfo {
  20. TypeNameInfo *Prev;
  21. bool isTypeName;
  22. TypeNameInfo(bool istypename, TypeNameInfo *prev) {
  23. isTypeName = istypename;
  24. Prev = prev;
  25. }
  26. };
  27. /// isTypeName - This looks at the IdentifierInfo::FETokenInfo field to
  28. /// determine whether the name is a type name (objc class name or typedef) or
  29. /// not in this scope.
  30. Action::DeclTy *
  31. MinimalAction::isTypeName(const IdentifierInfo &II, Scope *S) const {
  32. if (TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>())
  33. if (TI->isTypeName)
  34. return TI;
  35. return 0;
  36. }
  37. /// ParseDeclarator - If this is a typedef declarator, we modify the
  38. /// IdentifierInfo::FETokenInfo field to keep track of this fact, until S is
  39. /// popped.
  40. Action::DeclTy *
  41. MinimalAction::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
  42. DeclTy *LastInGroup) {
  43. IdentifierInfo *II = D.getIdentifier();
  44. // If there is no identifier associated with this declarator, bail out.
  45. if (II == 0) return 0;
  46. TypeNameInfo *weCurrentlyHaveTypeInfo = II->getFETokenInfo<TypeNameInfo>();
  47. bool isTypeName =
  48. D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef;
  49. // this check avoids creating TypeNameInfo objects for the common case.
  50. // It does need to handle the uncommon case of shadowing a typedef name with a
  51. // non-typedef name. e.g. { typedef int a; a xx; { int a; } }
  52. if (weCurrentlyHaveTypeInfo || isTypeName) {
  53. TypeNameInfo *TI = new TypeNameInfo(isTypeName, weCurrentlyHaveTypeInfo);
  54. II->setFETokenInfo(TI);
  55. // Remember that this needs to be removed when the scope is popped.
  56. S->AddDecl(II);
  57. }
  58. return 0;
  59. }
  60. /// ParsedObjcClassDeclaration -
  61. /// Scope will always be top level file scope.
  62. Action::DeclTy *
  63. MinimalAction::ParsedObjcClassDeclaration(Scope *S,
  64. IdentifierInfo **IdentList,
  65. unsigned NumElts) {
  66. for (unsigned i = 0; i != NumElts; ++i) {
  67. TypeNameInfo *TI =
  68. new TypeNameInfo(1, IdentList[i]->getFETokenInfo<TypeNameInfo>());
  69. IdentList[i]->setFETokenInfo(TI);
  70. // Remember that this needs to be removed when the scope is popped.
  71. S->AddDecl(IdentList[i]);
  72. }
  73. return 0;
  74. }
  75. /// PopScope - When a scope is popped, if any typedefs are now out-of-scope,
  76. /// they are removed from the IdentifierInfo::FETokenInfo field.
  77. void MinimalAction::PopScope(SourceLocation Loc, Scope *S) {
  78. for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
  79. I != E; ++I) {
  80. IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
  81. TypeNameInfo *TI = II.getFETokenInfo<TypeNameInfo>();
  82. assert(TI && "This decl didn't get pushed??");
  83. if (TI) {
  84. TypeNameInfo *Next = TI->Prev;
  85. delete TI;
  86. II.setFETokenInfo(Next);
  87. }
  88. }
  89. }