summaryrefslogtreecommitdiff
path: root/lib/Sema/Scope.cpp
blob: 276dce02903b281be7eb9a5531c24fdd414c941e (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//===- Scope.cpp - Lexical scope information ------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Scope class, which is used for recording information
// about a lexical scope.
//
//===----------------------------------------------------------------------===//

#include "flang/Sema/Scope.h"
#include "flang/AST/Expr.h"
#include "flang/AST/StorageSet.h"
#include <limits>

namespace flang {

static StmtLabelInteger GetStmtLabelValue(const Expr *E) {
  if(const IntegerConstantExpr *IExpr =
     dyn_cast<IntegerConstantExpr>(E)) {
    return StmtLabelInteger(
          IExpr->getValue().getLimitedValue(
            std::numeric_limits<StmtLabelInteger>::max()));
  } else {
    llvm_unreachable("Invalid stmt label expression");
    return 0;
  }
}

void StmtLabelScope::setParent(StmtLabelScope *P) {
  assert(!Parent);
  Parent = P;
}

/// \brief Declares a new statement label.
void StmtLabelScope::Declare(Expr *StmtLabel, Stmt *Statement) {
  auto Key = GetStmtLabelValue(StmtLabel);
  StmtLabelDeclsInScope.insert(std::make_pair(Key,Statement));
}

/// \brief Tries to resolve a statement label reference.
Stmt *StmtLabelScope::Resolve(Expr *StmtLabel) const {
  auto Key = GetStmtLabelValue(StmtLabel);
  auto Result = StmtLabelDeclsInScope.find(Key);
  if(Result == StmtLabelDeclsInScope.end()) return nullptr;
  Result->second->setStmtLabelUsed();
  return Result->second;
}

/// \brief Declares a forward reference of some statement label.
void StmtLabelScope::DeclareForwardReference(ForwardDecl Reference) {
  ForwardStmtLabelDeclsInScope.append(1,Reference);
}

/// \brief Removes a forward reference of some statement label.
void StmtLabelScope::RemoveForwardReference(const Stmt *User) {
  for(size_t I = 0; I < ForwardStmtLabelDeclsInScope.size(); ++I) {
    if(ForwardStmtLabelDeclsInScope[I].Statement == User) {
      ForwardStmtLabelDeclsInScope.erase(ForwardStmtLabelDeclsInScope.begin() + I);
      return;
    }
  }
}

/// \brief Returns true is the two statement labels are identical.
bool StmtLabelScope::IsSame(const Expr *StmtLabelA,
                            const Expr *StmtLabelB) const {
  return GetStmtLabelValue(StmtLabelA) == GetStmtLabelValue(StmtLabelB);
}

void ConstructNameScope::setParent(ConstructNameScope *P) {
  Parent = P;
}

void ConstructNameScope::Declare(const IdentifierInfo *Name, NamedConstructStmt *Construct) {
  Constructs.insert(std::make_pair(Name, Construct));
}

NamedConstructStmt *ConstructNameScope::Resolve(const IdentifierInfo *ConstructName) const {
  auto Result = Constructs.find(ConstructName);
  if(Result == Constructs.end()) return nullptr;
  return Result->second;
}

ImplicitTypingScope::ImplicitTypingScope(ImplicitTypingScope *Prev)
  : Parent(Prev), None(false) {
}

void ImplicitTypingScope::setParent(ImplicitTypingScope *P) {
  assert(!Parent);
  Parent = P;
}

bool ImplicitTypingScope::Apply(const ImplicitStmt::LetterSpecTy &Spec, QualType T) {
  if(None) return false;
  char Low = toupper((Spec.first->getNameStart())[0]);
  if(Spec.second) {
    char High = toupper((Spec.second->getNameStart())[0]);
    for(; Low <= High; ++Low) {
      llvm::StringRef Key(&Low, 1);
      if(Rules.find(Key) != Rules.end())
        return false;
      Rules[Key] = T;
    }
  } else {
    llvm::StringRef Key(&Low, 1);
    if(Rules.find(Key) != Rules.end())
      return false;
    Rules[Key] = T;
  }
  return true;
}

bool ImplicitTypingScope::ApplyNone() {
  if(Rules.size()) return false;
  None = true;
  return true;
}

std::pair<ImplicitTypingScope::RuleType, QualType>
ImplicitTypingScope::Resolve(const IdentifierInfo *IdInfo) {
  if(None)
    return std::make_pair(NoneRule, QualType());
  char C = toupper(IdInfo->getNameStart()[0]);
  auto Result = Rules.find(llvm::StringRef(&C, 1));
  if(Result != Rules.end())
    return std::make_pair(TypeRule, Result->getValue());
  else if(Parent)
    return Parent->Resolve(IdInfo);
  else return std::make_pair(DefaultRule, QualType());
}

InnerScope::InnerScope(InnerScope *Prev)
  : Parent(Prev) {
}

void InnerScope::Declare(const IdentifierInfo *IDInfo, Decl *Declaration) {
  Declarations[IDInfo->getName()] = Declaration;
}

Decl *InnerScope::Lookup(const IdentifierInfo *IDInfo) const {
  auto Result = Declarations.find(IDInfo->getName());
  if(Result != Declarations.end())
    return Result->getValue();
  return nullptr;
}

Decl *InnerScope::Resolve(const IdentifierInfo *IDInfo) const {
  auto Result = Lookup(IDInfo);
  return Result? Result : (Parent? Parent->Resolve(IDInfo) : nullptr);
}

CommonBlockScope::CommonBlockScope()
  : UnnamedBlock(nullptr) {}

CommonBlockDecl *CommonBlockScope::find(const IdentifierInfo *IDInfo) {
  assert(IDInfo);
  auto Result = Blocks.find(IDInfo);
  if(Result != Blocks.end())
    return Result->second;
  return nullptr;
}

CommonBlockDecl *CommonBlockScope::findOrInsert(ASTContext &C, DeclContext *DC,
                                                SourceLocation IDLoc,
                                                const IdentifierInfo *IDInfo) {
  if(!IDInfo) {
    if(!UnnamedBlock) {
      UnnamedBlock = CommonBlockDecl::Create(C, DC, IDLoc, IDInfo);
      auto Set = CommonBlockSet::Create(C, UnnamedBlock);
      UnnamedBlock->setStorageSet(Set);
    }
    return UnnamedBlock;
  }
  auto Result = Blocks.find(IDInfo);
  if(Result != Blocks.end())
    return Result->second;
  auto Block = CommonBlockDecl::Create(C, DC, IDLoc, IDInfo);
  auto Set = CommonBlockSet::Create(C, Block);
  Block->setStorageSet(Set);
  Blocks.insert(std::make_pair(IDInfo, Block));
  return Block;
}

} // end namespace flang