blob: 8211cb0088907452b523bdb8c80612824a371f8f [file] [log] [blame]
Ted Kremenek16c440a2010-01-15 20:35:54 +00001//===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
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//
Douglas Gregor2e331b92010-01-16 14:00:32 +000010// This file defines routines for manipulating CXCursors. It should be the
11// only file that has internal knowledge of the encoding of the data in
12// CXCursor.
Ted Kremenek16c440a2010-01-15 20:35:54 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "CXCursor.h"
17#include "clang/AST/Decl.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Ted Kremenekedc8aa62010-01-16 00:36:30 +000020#include "llvm/Support/ErrorHandling.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000021
22using namespace clang;
23
24CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D) {
Douglas Gregor283cae32010-01-15 21:56:13 +000025 CXCursor C = { K, { D, 0, 0 } };
Ted Kremenek16c440a2010-01-15 20:35:54 +000026 return C;
27}
28
29CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D, Stmt *S) {
30 assert(clang_isReference(K));
Douglas Gregor283cae32010-01-15 21:56:13 +000031 CXCursor C = { K, { D, S, 0 } };
Ted Kremenek16c440a2010-01-15 20:35:54 +000032 return C;
33}
34
Ted Kremenekedc8aa62010-01-16 00:36:30 +000035static CXCursorKind GetCursorKind(Decl *D) {
36 switch (D->getKind()) {
Ted Kremenek70ee5422010-01-16 01:44:12 +000037 case Decl::Enum: return CXCursor_EnumDecl;
38 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
39 case Decl::Field: return CXCursor_FieldDecl;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000040 case Decl::Function:
41 return cast<FunctionDecl>(D)->isThisDeclarationADefinition()
42 ? CXCursor_FunctionDefn : CXCursor_FunctionDecl;
43 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
44 case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryDefn;
Ted Kremenek70ee5422010-01-16 01:44:12 +000045 case Decl::ObjCClass:
46 // FIXME
47 return CXCursor_NotImplemented;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000048 case Decl::ObjCImplementation: return CXCursor_ObjCClassDefn;
49 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Ted Kremenek70ee5422010-01-16 01:44:12 +000050 case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl;
51 case Decl::ObjCMethod:
52 return cast<ObjCMethodDecl>(D)->isInstanceMethod()
53 ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl;
Ted Kremenek10fa3cc2010-01-16 02:08:29 +000054 case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000055 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Ted Kremenek70ee5422010-01-16 01:44:12 +000056 case Decl::ParmVar: return CXCursor_ParmDecl;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000057 case Decl::Typedef: return CXCursor_TypedefDecl;
58 case Decl::Var: return CXCursor_VarDecl;
59 default:
60 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
61 switch (TD->getTagKind()) {
62 case TagDecl::TK_struct: return CXCursor_StructDecl;
63 case TagDecl::TK_class: return CXCursor_ClassDecl;
64 case TagDecl::TK_union: return CXCursor_UnionDecl;
65 case TagDecl::TK_enum: return CXCursor_EnumDecl;
66 }
67 }
68 }
69
70 llvm_unreachable("Invalid Decl");
71 return CXCursor_NotImplemented;
72}
73
74CXCursor cxcursor::MakeCXCursor(Decl *D) {
75 return MakeCXCursor(GetCursorKind(D), D);
76}
77
Douglas Gregor2e331b92010-01-16 14:00:32 +000078CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
79 SourceLocation Loc) {
80 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
81 CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, 0 } };
82 return C;
83}
84
85std::pair<ObjCInterfaceDecl *, SourceLocation>
86cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
87 assert(C.kind == CXCursor_ObjCSuperClassRef);
88 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
89 SourceLocation::getFromRawEncoding(
90 reinterpret_cast<uintptr_t>(C.data[1])));
91}
92
Douglas Gregor78db0cd2010-01-16 15:44:18 +000093CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
94 SourceLocation Loc) {
95 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
96 CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, 0 } };
97 return C;
98}
99
100std::pair<ObjCProtocolDecl *, SourceLocation>
101cxcursor::getCursorObjCProtocolRef(CXCursor C) {
102 assert(C.kind == CXCursor_ObjCProtocolRef);
103 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
104 SourceLocation::getFromRawEncoding(
105 reinterpret_cast<uintptr_t>(C.data[1])));
106}
107
Douglas Gregor1adb0822010-01-16 17:14:40 +0000108CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
109 SourceLocation Loc) {
110 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
111 CXCursor C = { CXCursor_ObjCClassRef, { Class, RawLoc, 0 } };
112 return C;
113}
114
115std::pair<ObjCInterfaceDecl *, SourceLocation>
116cxcursor::getCursorObjCClassRef(CXCursor C) {
117 assert(C.kind == CXCursor_ObjCClassRef);
118 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
119 SourceLocation::getFromRawEncoding(
120 reinterpret_cast<uintptr_t>(C.data[1])));
121}
122
Douglas Gregor283cae32010-01-15 21:56:13 +0000123Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
124 return (Decl *)Cursor.data[0];
125}
126
127Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
128 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
129}
130
131Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000132 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000133 Cursor.kind == CXCursor_ObjCProtocolRef ||
134 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000135 return 0;
136
Douglas Gregor283cae32010-01-15 21:56:13 +0000137 return (Stmt *)Cursor.data[1];
138}
139
140Decl *cxcursor::getCursorReferringDecl(CXCursor Cursor) {
141 return (Decl *)Cursor.data[2];
142}
143
Douglas Gregor283cae32010-01-15 21:56:13 +0000144bool cxcursor::operator==(CXCursor X, CXCursor Y) {
145 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
146 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000147}