aboutsummaryrefslogtreecommitdiff
path: root/lib/AST/StmtIterator.cpp
blob: 14083e30a992c72a9cb090766dc67fd0077aba18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//===--- StmtIterator.cpp - Iterators for Statements ------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines internal methods for StmtIterator.
//
//===----------------------------------------------------------------------===//

#include "clang/AST/StmtIterator.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Decl.h"

using namespace clang;

static inline VariableArrayType* FindVA(Type* t) {
  while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
    if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
      if (vat->getSizeExpr())
        return vat;
    
    t = vt->getElementType().getTypePtr();
  }
  
  return NULL;
}

void StmtIteratorBase::NextVA() {
  assert (getVAPtr());

  VariableArrayType* p = getVAPtr();
  p = FindVA(p->getElementType().getTypePtr());
  setVAPtr(p);

  if (!p && decl) {
    if (VarDecl* VD = dyn_cast<VarDecl>(decl)) 
      if (VD->Init)
        return;
      
    NextDecl();
  }
  else {
    RawVAPtr = 0;
  }    
}

void StmtIteratorBase::NextDecl(bool ImmediateAdvance) {
  assert (inDecl());
  assert (getVAPtr() == NULL);
  assert (decl);
  
  if (ImmediateAdvance) {
    decl = decl->getNextDeclarator();

    if (!decl) {
      RawVAPtr = 0;
      return;
    }
  }    
  
  for ( ; decl ; decl = decl->getNextDeclarator()) {
    if (VarDecl* VD = dyn_cast<VarDecl>(decl)) {        
      if (VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) {
        setVAPtr(VAPtr);
        return;
      }
      
      if (VD->getInit())
        return;    
    }
    else if (TypedefDecl* TD = dyn_cast<TypedefDecl>(decl)) {
      if (VariableArrayType* VAPtr = 
           FindVA(TD->getUnderlyingType().getTypePtr())) {
        setVAPtr(VAPtr);
        return;
      }
    }
    else if (EnumConstantDecl* ECD = dyn_cast<EnumConstantDecl>(decl))
      if (ECD->getInitExpr())
        return;  
  }
  
  if (!decl) {
    RawVAPtr = 0;
    return;
  }
}

StmtIteratorBase::StmtIteratorBase(ScopedDecl* d)
  : decl(d), RawVAPtr(DeclMode) {
  assert (decl);
  NextDecl(false);
}

StmtIteratorBase::StmtIteratorBase(VariableArrayType* t)
: decl(NULL), RawVAPtr(SizeOfTypeVAMode) {
  RawVAPtr |= reinterpret_cast<uintptr_t>(t);
}


Stmt*& StmtIteratorBase::GetDeclExpr() const {
  if (VariableArrayType* VAPtr = getVAPtr()) {
    assert (VAPtr->SizeExpr);
    return reinterpret_cast<Stmt*&>(VAPtr->SizeExpr);
  }

  if (VarDecl* VD = dyn_cast<VarDecl>(decl)) {
    assert (VD->Init);
    return reinterpret_cast<Stmt*&>(VD->Init);
  }

  EnumConstantDecl* ECD = cast<EnumConstantDecl>(decl);
  return reinterpret_cast<Stmt*&>(ECD->Init);
}