summaryrefslogtreecommitdiff
path: root/lib/AST/DeclarationName.cpp
blob: 7fbde8e108b9c31e80256456af4e02a51cd77b1d (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
//===-- DeclarationName.cpp - Declaration names implementation ------------===//
//
//                     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 DeclarationName and DeclarationNameTable classes.
//
//===----------------------------------------------------------------------===//

#include "flang/AST/ASTContext.h"
#include "flang/AST/Decl.h"
#include "flang/AST/DeclarationName.h"
#include "flang/Basic/IdentifierTable.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"

namespace flang {

//===----------------------------------------------------------------------===//
// DeclarationName Implementation
//===----------------------------------------------------------------------===//

DeclarationName::NameKind DeclarationName::getNameKind() const {
  switch (getStoredNameKind()) {
  default: break;
  case StoredIdentifier:
    return Identifier;
  }

  // Can't actually get here.
  assert(false && "This should be unreachable!");
  return Identifier;
}

std::string DeclarationName::getAsString() const {
  std::string Result;
  llvm::raw_string_ostream OS(Result);
  printName(OS);
  return OS.str();
}

void DeclarationName::printName(llvm::raw_ostream &OS) const {
  if (getNameKind() == Identifier) {
    if (const IdentifierInfo *II = getAsIdentifierInfo())
      OS << II->getName();
    return;
  }

  assert(false && "Unexpected declaration name kind");
}

void *DeclarationName::getFETokenInfoAsVoid() const {
  switch (getNameKind()) {
  case Identifier:
    return getAsIdentifierInfo()->getFETokenInfo<void>();
  }

  assert(false && "Declaration name has no FETokenInfo");
  return 0;
}

void DeclarationName::setFETokenInfo(void *T) {
  if (getNameKind() == Identifier) {
    getAsIdentifierInfo()->setFETokenInfo(T);
    return;
  }

  assert(false && "Declaration name has no FETokenInfo");
}

int DeclarationName::compare(DeclarationName LHS, DeclarationName RHS) {
  if (LHS.getNameKind() != RHS.getNameKind())
    return (LHS.getNameKind() < RHS.getNameKind() ? -1 : 1);

  switch (LHS.getNameKind()) {
  case DeclarationName::Identifier: {
    IdentifierInfo *LII = LHS.getAsIdentifierInfo();
    IdentifierInfo *RII = RHS.getAsIdentifierInfo();
    if (!LII) return RII ? -1 : 0;
    if (!RII) return 1;

    return LII->getName().compare(RII->getName());
  }
  }

  return 0;
}

void DeclarationName::dump() const {
  printName(llvm::errs());
  llvm::errs() << '\n';
}

//===----------------------------------------------------------------------===//
// DeclarationNameTable Implementation
//===----------------------------------------------------------------------===//

DeclarationNameTable::~DeclarationNameTable() {}

//===----------------------------------------------------------------------===//
// DeclarationNameInfo Implementation
//===----------------------------------------------------------------------===//

std::string DeclarationNameInfo::getAsString() const {
  std::string Result;
  llvm::raw_string_ostream OS(Result);
  printName(OS);
  return OS.str();
}

void DeclarationNameInfo::printName(llvm::raw_ostream &OS) const {
  switch (Name.getNameKind()) {
  case DeclarationName::Identifier:
    Name.printName(OS);
    return;
  }

  assert(false && "Unexpected declaration name kind");
}

SourceLocation DeclarationNameInfo::getEndLoc() const {
  switch (Name.getNameKind()) {
  case DeclarationName::Identifier:
    return NameLoc;
  }

  assert(false && "Unexpected declaration name kind");
  return SourceLocation();
}

} //namespace flang

//===----------------------------------------------------------------------===//
// DenseMapInfo Implementation
//===----------------------------------------------------------------------===//

unsigned
llvm::DenseMapInfo<flang::DeclarationName>::
getHashValue(flang::DeclarationName N) {
  return DenseMapInfo<void*>::getHashValue(N.getAsOpaquePtr());
}