blob: cafb105e996ab3a5b5435257e7f2fc8a44e55cf2 [file] [log] [blame]
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00001//===--- SelectorLocationsKind.cpp - Kind of selector locations -*- C++ -*-===//
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//
10// Describes whether the identifier locations for a selector are "standard"
11// or not.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/SelectorLocationsKind.h"
16#include "clang/AST/Expr.h"
17
18using namespace clang;
19
20static SourceLocation getStandardSelLoc(unsigned Index,
21 Selector Sel,
22 bool WithArgSpace,
23 SourceLocation ArgLoc,
24 SourceLocation EndLoc) {
25 unsigned NumSelArgs = Sel.getNumArgs();
26 if (NumSelArgs == 0) {
27 assert(Index == 0);
28 if (EndLoc.isInvalid())
29 return SourceLocation();
30 IdentifierInfo *II = Sel.getIdentifierInfoForSlot(0);
31 unsigned Len = II ? II->getLength() : 0;
32 return EndLoc.getLocWithOffset(-Len);
33 }
34
35 assert(Index < NumSelArgs);
36 if (ArgLoc.isInvalid())
37 return SourceLocation();
38 IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Index);
39 unsigned Len = /* selector id */ (II ? II->getLength() : 0) + /* ':' */ 1;
40 if (WithArgSpace)
41 ++Len;
42 return ArgLoc.getLocWithOffset(-Len);
43}
44
45namespace {
46
47template <typename T>
48SourceLocation getArgLoc(T* Arg);
49
50template <>
51SourceLocation getArgLoc<Expr>(Expr *Arg) {
52 return Arg->getLocStart();
53}
54
55template <typename T>
56SourceLocation getArgLoc(unsigned Index, ArrayRef<T*> Args) {
57 return Index < Args.size() ? getArgLoc(Args[Index]) : SourceLocation();
58}
59
60template <typename T>
61SelectorLocationsKind hasStandardSelLocs(Selector Sel,
62 ArrayRef<SourceLocation> SelLocs,
63 ArrayRef<T *> Args,
64 SourceLocation EndLoc) {
65 // Are selector locations in standard position with no space between args ?
66 unsigned i;
67 for (i = 0; i != SelLocs.size(); ++i) {
68 if (SelLocs[i] != getStandardSelectorLoc(i, Sel, /*WithArgSpace=*/false,
69 Args, EndLoc))
70 break;
71 }
72 if (i == SelLocs.size())
73 return SelLoc_StandardNoSpace;
74
75 // Are selector locations in standard position with space between args ?
76 for (i = 0; i != SelLocs.size(); ++i) {
77 if (SelLocs[i] != getStandardSelectorLoc(i, Sel, /*WithArgSpace=*/true,
78 Args, EndLoc))
79 return SelLoc_NonStandard;
80 }
81
82 return SelLoc_StandardWithSpace;
83}
84
85} // anonymous namespace
86
87SelectorLocationsKind
88clang::hasStandardSelectorLocs(Selector Sel,
89 ArrayRef<SourceLocation> SelLocs,
90 ArrayRef<Expr *> Args,
91 SourceLocation EndLoc) {
92 return hasStandardSelLocs(Sel, SelLocs, Args, EndLoc);
93}
94
95SourceLocation clang::getStandardSelectorLoc(unsigned Index,
96 Selector Sel,
97 bool WithArgSpace,
98 ArrayRef<Expr *> Args,
99 SourceLocation EndLoc) {
100 return getStandardSelLoc(Index, Sel, WithArgSpace,
101 getArgLoc(Index, Args), EndLoc);
102}