Daniel Dunbar | 4efd632 | 2010-01-25 00:43:08 +0000 | [diff] [blame] | 1 | #===- cindex.py - Python Indexing Library Bindings -----------*- python -*--===# |
| 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 | r""" |
| 11 | Clang Indexing Library Bindings |
| 12 | =============================== |
| 13 | |
| 14 | This module provides an interface to the Clang indexing library. It is a |
| 15 | low-level interface to the indexing library which attempts to match the Clang |
| 16 | API directly while also being "pythonic". Notable differences from the C API |
| 17 | are: |
| 18 | |
| 19 | * string results are returned as Python strings, not CXString objects. |
| 20 | |
| 21 | * null cursors are translated to None. |
| 22 | |
| 23 | * access to child cursors is done via iteration, not visitation. |
| 24 | |
| 25 | The major indexing objects are: |
| 26 | |
| 27 | Index |
| 28 | |
| 29 | The top-level object which manages some global library state. |
| 30 | |
| 31 | TranslationUnit |
| 32 | |
| 33 | High-level object encapsulating the AST for a single translation unit. These |
| 34 | can be loaded from .ast files or parsed on the fly. |
| 35 | |
| 36 | Cursor |
| 37 | |
| 38 | Generic object for representing a node in the AST. |
| 39 | |
| 40 | SourceRange, SourceLocation, and File |
| 41 | |
| 42 | Objects representing information about the input source. |
| 43 | |
| 44 | Most object information is exposed using properties, when the underlying API |
| 45 | call is efficient. |
| 46 | """ |
| 47 | |
| 48 | # TODO |
| 49 | # ==== |
| 50 | # |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 51 | # o API support for invalid translation units. Currently we can't even get the |
| 52 | # diagnostics on failure because they refer to locations in an object that |
| 53 | # will have been invalidated. |
| 54 | # |
Daniel Dunbar | 4efd632 | 2010-01-25 00:43:08 +0000 | [diff] [blame] | 55 | # o fix memory management issues (currently client must hold on to index and |
| 56 | # translation unit, or risk crashes). |
| 57 | # |
| 58 | # o expose code completion APIs. |
| 59 | # |
| 60 | # o cleanup ctypes wrapping, would be nice to separate the ctypes details more |
| 61 | # clearly, and hide from the external interface (i.e., help(cindex)). |
| 62 | # |
| 63 | # o implement additional SourceLocation, SourceRange, and File methods. |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 64 | |
| 65 | from ctypes import * |
| 66 | |
| 67 | def get_cindex_library(): |
| 68 | # FIXME: It's probably not the case that the library is actually found in |
| 69 | # this location. We need a better system of identifying and loading the |
| 70 | # CIndex library. It could be on path or elsewhere, or versioned, etc. |
| 71 | import platform |
| 72 | name = platform.system() |
| 73 | if name == 'Darwin': |
Daniel Dunbar | f51f20f | 2010-04-30 21:51:10 +0000 | [diff] [blame] | 74 | return cdll.LoadLibrary('libclang.dylib') |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 75 | elif name == 'Windows': |
Daniel Dunbar | f51f20f | 2010-04-30 21:51:10 +0000 | [diff] [blame] | 76 | return cdll.LoadLibrary('libclang.dll') |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 77 | else: |
Daniel Dunbar | f51f20f | 2010-04-30 21:51:10 +0000 | [diff] [blame] | 78 | return cdll.LoadLibrary('libclang.so') |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 79 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 80 | # ctypes doesn't implicitly convert c_void_p to the appropriate wrapper |
| 81 | # object. This is a problem, because it means that from_parameter will see an |
| 82 | # integer and pass the wrong value on platforms where int != void*. Work around |
| 83 | # this by marshalling object arguments as void**. |
| 84 | c_object_p = POINTER(c_void_p) |
| 85 | |
| 86 | lib = get_cindex_library() |
| 87 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 88 | ### Structures and Utility Classes ### |
| 89 | |
Daniel Dunbar | a33dca4 | 2010-01-24 21:19:57 +0000 | [diff] [blame] | 90 | class _CXString(Structure): |
| 91 | """Helper for transforming CXString results.""" |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 92 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 93 | _fields_ = [("spelling", c_char_p), ("free", c_int)] |
| 94 | |
| 95 | def __del__(self): |
Daniel Dunbar | a33dca4 | 2010-01-24 21:19:57 +0000 | [diff] [blame] | 96 | _CXString_dispose(self) |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 97 | |
Daniel Dunbar | a33dca4 | 2010-01-24 21:19:57 +0000 | [diff] [blame] | 98 | @staticmethod |
| 99 | def from_result(res, fn, args): |
| 100 | assert isinstance(res, _CXString) |
| 101 | return _CXString_getCString(res) |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 102 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 103 | class SourceLocation(Structure): |
| 104 | """ |
Daniel Dunbar | 149f38a | 2010-01-24 04:09:58 +0000 | [diff] [blame] | 105 | A SourceLocation represents a particular location within a source file. |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 106 | """ |
Daniel Dunbar | e32af42 | 2010-01-30 23:58:50 +0000 | [diff] [blame] | 107 | _fields_ = [("ptr_data", c_void_p * 2), ("int_data", c_uint)] |
Daniel Dunbar | f869083 | 2010-01-24 21:20:21 +0000 | [diff] [blame] | 108 | _data = None |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 109 | |
Daniel Dunbar | f869083 | 2010-01-24 21:20:21 +0000 | [diff] [blame] | 110 | def _get_instantiation(self): |
| 111 | if self._data is None: |
Daniel Dunbar | 3239a67 | 2010-01-29 17:02:32 +0000 | [diff] [blame] | 112 | f, l, c, o = c_object_p(), c_uint(), c_uint(), c_uint() |
| 113 | SourceLocation_loc(self, byref(f), byref(l), byref(c), byref(o)) |
Tobias Grosser | 8198288 | 2011-10-31 00:49:07 +0000 | [diff] [blame] | 114 | if f: |
| 115 | f = File(f) |
| 116 | else: |
| 117 | f = None |
Argyrios Kyrtzidis | 6b04623 | 2011-08-17 17:20:24 +0000 | [diff] [blame] | 118 | self._data = (f, int(l.value), int(c.value), int(o.value)) |
Daniel Dunbar | f869083 | 2010-01-24 21:20:21 +0000 | [diff] [blame] | 119 | return self._data |
| 120 | |
Tobias Grosser | 58ba8c9 | 2011-10-31 00:31:32 +0000 | [diff] [blame] | 121 | @staticmethod |
| 122 | def from_position(tu, file, line, column): |
| 123 | """ |
| 124 | Retrieve the source location associated with a given file/line/column in |
| 125 | a particular translation unit. |
| 126 | """ |
| 127 | return SourceLocation_getLocation(tu, file, line, column) |
| 128 | |
Daniel Dunbar | f869083 | 2010-01-24 21:20:21 +0000 | [diff] [blame] | 129 | @property |
| 130 | def file(self): |
| 131 | """Get the file represented by this source location.""" |
| 132 | return self._get_instantiation()[0] |
| 133 | |
| 134 | @property |
| 135 | def line(self): |
| 136 | """Get the line represented by this source location.""" |
| 137 | return self._get_instantiation()[1] |
| 138 | |
| 139 | @property |
| 140 | def column(self): |
| 141 | """Get the column represented by this source location.""" |
| 142 | return self._get_instantiation()[2] |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 143 | |
Daniel Dunbar | 3239a67 | 2010-01-29 17:02:32 +0000 | [diff] [blame] | 144 | @property |
| 145 | def offset(self): |
| 146 | """Get the file offset represented by this source location.""" |
| 147 | return self._get_instantiation()[3] |
| 148 | |
Tobias Grosser | 7485833 | 2012-02-05 11:40:59 +0000 | [diff] [blame] | 149 | def __eq__(self, other): |
| 150 | return SourceLocation_equalLocations(self, other) |
| 151 | |
| 152 | def __ne__(self, other): |
| 153 | return not self.__eq__(other) |
| 154 | |
Daniel Dunbar | 7b48b35 | 2010-01-24 04:09:34 +0000 | [diff] [blame] | 155 | def __repr__(self): |
Tobias Grosser | 8198288 | 2011-10-31 00:49:07 +0000 | [diff] [blame] | 156 | if self.file: |
| 157 | filename = self.file.name |
| 158 | else: |
| 159 | filename = None |
Daniel Dunbar | 7b48b35 | 2010-01-24 04:09:34 +0000 | [diff] [blame] | 160 | return "<SourceLocation file %r, line %r, column %r>" % ( |
Tobias Grosser | 8198288 | 2011-10-31 00:49:07 +0000 | [diff] [blame] | 161 | filename, self.line, self.column) |
Daniel Dunbar | 7b48b35 | 2010-01-24 04:09:34 +0000 | [diff] [blame] | 162 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 163 | class SourceRange(Structure): |
| 164 | """ |
| 165 | A SourceRange describes a range of source locations within the source |
| 166 | code. |
| 167 | """ |
| 168 | _fields_ = [ |
Daniel Dunbar | e32af42 | 2010-01-30 23:58:50 +0000 | [diff] [blame] | 169 | ("ptr_data", c_void_p * 2), |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 170 | ("begin_int_data", c_uint), |
| 171 | ("end_int_data", c_uint)] |
| 172 | |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 173 | # FIXME: Eliminate this and make normal constructor? Requires hiding ctypes |
| 174 | # object. |
| 175 | @staticmethod |
| 176 | def from_locations(start, end): |
| 177 | return SourceRange_getRange(start, end) |
| 178 | |
Daniel Dunbar | 7b48b35 | 2010-01-24 04:09:34 +0000 | [diff] [blame] | 179 | @property |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 180 | def start(self): |
| 181 | """ |
| 182 | Return a SourceLocation representing the first character within a |
| 183 | source range. |
| 184 | """ |
Daniel Dunbar | f869083 | 2010-01-24 21:20:21 +0000 | [diff] [blame] | 185 | return SourceRange_start(self) |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 186 | |
Daniel Dunbar | 7b48b35 | 2010-01-24 04:09:34 +0000 | [diff] [blame] | 187 | @property |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 188 | def end(self): |
| 189 | """ |
| 190 | Return a SourceLocation representing the last character within a |
| 191 | source range. |
| 192 | """ |
Daniel Dunbar | f869083 | 2010-01-24 21:20:21 +0000 | [diff] [blame] | 193 | return SourceRange_end(self) |
| 194 | |
Tobias Grosser | 7485833 | 2012-02-05 11:40:59 +0000 | [diff] [blame] | 195 | def __eq__(self, other): |
| 196 | return SourceRange_equalRanges(self, other) |
| 197 | |
| 198 | def __ne__(self, other): |
| 199 | return not self.__eq__(other) |
| 200 | |
Daniel Dunbar | f869083 | 2010-01-24 21:20:21 +0000 | [diff] [blame] | 201 | def __repr__(self): |
| 202 | return "<SourceRange start %r, end %r>" % (self.start, self.end) |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 204 | class Diagnostic(object): |
| 205 | """ |
| 206 | A Diagnostic is a single instance of a Clang diagnostic. It includes the |
| 207 | diagnostic severity, the message, the location the diagnostic occurred, as |
| 208 | well as additional source ranges and associated fix-it hints. |
| 209 | """ |
| 210 | |
| 211 | Ignored = 0 |
| 212 | Note = 1 |
| 213 | Warning = 2 |
| 214 | Error = 3 |
| 215 | Fatal = 4 |
| 216 | |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 217 | def __init__(self, ptr): |
| 218 | self.ptr = ptr |
| 219 | |
| 220 | def __del__(self): |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 221 | _clang_disposeDiagnostic(self) |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 222 | |
| 223 | @property |
| 224 | def severity(self): |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 225 | return _clang_getDiagnosticSeverity(self) |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 226 | |
| 227 | @property |
| 228 | def location(self): |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 229 | return _clang_getDiagnosticLocation(self) |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 230 | |
| 231 | @property |
| 232 | def spelling(self): |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 233 | return _clang_getDiagnosticSpelling(self) |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 234 | |
| 235 | @property |
| 236 | def ranges(self): |
Benjamin Kramer | 1d02ccd | 2010-03-06 15:38:03 +0000 | [diff] [blame] | 237 | class RangeIterator: |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 238 | def __init__(self, diag): |
| 239 | self.diag = diag |
| 240 | |
| 241 | def __len__(self): |
| 242 | return int(_clang_getDiagnosticNumRanges(self.diag)) |
| 243 | |
| 244 | def __getitem__(self, key): |
Tobias Grosser | ba5d10b | 2011-10-31 02:06:50 +0000 | [diff] [blame] | 245 | if (key >= len(self)): |
| 246 | raise IndexError |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 247 | return _clang_getDiagnosticRange(self.diag, key) |
| 248 | |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 249 | return RangeIterator(self) |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 250 | |
| 251 | @property |
| 252 | def fixits(self): |
Benjamin Kramer | 1d02ccd | 2010-03-06 15:38:03 +0000 | [diff] [blame] | 253 | class FixItIterator: |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 254 | def __init__(self, diag): |
| 255 | self.diag = diag |
| 256 | |
| 257 | def __len__(self): |
| 258 | return int(_clang_getDiagnosticNumFixIts(self.diag)) |
| 259 | |
| 260 | def __getitem__(self, key): |
| 261 | range = SourceRange() |
| 262 | value = _clang_getDiagnosticFixIt(self.diag, key, byref(range)) |
Benjamin Kramer | 1d02ccd | 2010-03-06 15:38:03 +0000 | [diff] [blame] | 263 | if len(value) == 0: |
| 264 | raise IndexError |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 265 | |
| 266 | return FixIt(range, value) |
| 267 | |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 268 | return FixItIterator(self) |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 269 | |
Tobias Grosser | ea40382 | 2012-02-05 11:41:58 +0000 | [diff] [blame] | 270 | @property |
| 271 | def category_number(self): |
| 272 | """The category number for this diagnostic.""" |
| 273 | return _clang_getDiagnosticCategory(self) |
| 274 | |
| 275 | @property |
| 276 | def category_name(self): |
| 277 | """The string name of the category for this diagnostic.""" |
| 278 | return _clang_getDiagnosticCategoryName(self.category_number) |
| 279 | |
| 280 | @property |
| 281 | def option(self): |
| 282 | """The command-line option that enables this diagnostic.""" |
| 283 | return _clang_getDiagnosticOption(self, None) |
| 284 | |
| 285 | @property |
| 286 | def disable_option(self): |
| 287 | """The command-line option that disables this diagnostic.""" |
| 288 | disable = _CXString() |
| 289 | _clang_getDiagnosticOption(self, byref(disable)) |
| 290 | |
| 291 | return _CXString_getCString(disable) |
| 292 | |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 293 | def __repr__(self): |
| 294 | return "<Diagnostic severity %r, location %r, spelling %r>" % ( |
| 295 | self.severity, self.location, self.spelling) |
| 296 | |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 297 | def from_param(self): |
| 298 | return self.ptr |
| 299 | |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 300 | class FixIt(object): |
| 301 | """ |
| 302 | A FixIt represents a transformation to be applied to the source to |
| 303 | "fix-it". The fix-it shouldbe applied by replacing the given source range |
| 304 | with the given value. |
| 305 | """ |
| 306 | |
| 307 | def __init__(self, range, value): |
| 308 | self.range = range |
| 309 | self.value = value |
| 310 | |
| 311 | def __repr__(self): |
| 312 | return "<FixIt range %r, value %r>" % (self.range, self.value) |
| 313 | |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 314 | ### Cursor Kinds ### |
| 315 | |
| 316 | class CursorKind(object): |
| 317 | """ |
| 318 | A CursorKind describes the kind of entity that a cursor points to. |
| 319 | """ |
| 320 | |
| 321 | # The unique kind objects, indexed by id. |
| 322 | _kinds = [] |
| 323 | _name_map = None |
| 324 | |
| 325 | def __init__(self, value): |
| 326 | if value >= len(CursorKind._kinds): |
| 327 | CursorKind._kinds += [None] * (value - len(CursorKind._kinds) + 1) |
| 328 | if CursorKind._kinds[value] is not None: |
| 329 | raise ValueError,'CursorKind already loaded' |
| 330 | self.value = value |
| 331 | CursorKind._kinds[value] = self |
| 332 | CursorKind._name_map = None |
| 333 | |
| 334 | def from_param(self): |
| 335 | return self.value |
| 336 | |
| 337 | @property |
| 338 | def name(self): |
| 339 | """Get the enumeration name of this cursor kind.""" |
| 340 | if self._name_map is None: |
| 341 | self._name_map = {} |
| 342 | for key,value in CursorKind.__dict__.items(): |
| 343 | if isinstance(value,CursorKind): |
| 344 | self._name_map[value] = key |
| 345 | return self._name_map[self] |
| 346 | |
| 347 | @staticmethod |
| 348 | def from_id(id): |
| 349 | if id >= len(CursorKind._kinds) or CursorKind._kinds[id] is None: |
| 350 | raise ValueError,'Unknown cursor kind' |
| 351 | return CursorKind._kinds[id] |
| 352 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 353 | @staticmethod |
| 354 | def get_all_kinds(): |
Daniel Dunbar | 4efd632 | 2010-01-25 00:43:08 +0000 | [diff] [blame] | 355 | """Return all CursorKind enumeration instances.""" |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 356 | return filter(None, CursorKind._kinds) |
| 357 | |
| 358 | def is_declaration(self): |
| 359 | """Test if this is a declaration kind.""" |
| 360 | return CursorKind_is_decl(self) |
| 361 | |
| 362 | def is_reference(self): |
| 363 | """Test if this is a reference kind.""" |
| 364 | return CursorKind_is_ref(self) |
| 365 | |
| 366 | def is_expression(self): |
| 367 | """Test if this is an expression kind.""" |
| 368 | return CursorKind_is_expr(self) |
| 369 | |
| 370 | def is_statement(self): |
| 371 | """Test if this is a statement kind.""" |
| 372 | return CursorKind_is_stmt(self) |
| 373 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 374 | def is_attribute(self): |
| 375 | """Test if this is an attribute kind.""" |
| 376 | return CursorKind_is_attribute(self) |
| 377 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 378 | def is_invalid(self): |
| 379 | """Test if this is an invalid kind.""" |
| 380 | return CursorKind_is_inv(self) |
| 381 | |
Tobias Grosser | eb13634 | 2012-02-05 11:42:09 +0000 | [diff] [blame] | 382 | def is_translation_unit(self): |
| 383 | """Test if this is a translation unit kind.""" |
| 384 | return CursorKind_is_translation_unit(self) |
| 385 | |
| 386 | def is_preprocessing(self): |
| 387 | """Test if this is a preprocessing kind.""" |
| 388 | return CursorKind_is_preprocessing(self) |
| 389 | |
| 390 | def is_unexposed(self): |
| 391 | """Test if this is an unexposed kind.""" |
| 392 | return CursorKind_is_unexposed(self) |
| 393 | |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 394 | def __repr__(self): |
| 395 | return 'CursorKind.%s' % (self.name,) |
| 396 | |
| 397 | # FIXME: Is there a nicer way to expose this enumeration? We could potentially |
| 398 | # represent the nested structure, or even build a class hierarchy. The main |
| 399 | # things we want for sure are (a) simple external access to kinds, (b) a place |
| 400 | # to hang a description and name, (c) easy to keep in sync with Index.h. |
| 401 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 402 | ### |
| 403 | # Declaration Kinds |
| 404 | |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 405 | # A declaration whose specific kind is not exposed via this interface. |
| 406 | # |
| 407 | # Unexposed declarations have the same operations as any other kind of |
| 408 | # declaration; one can extract their location information, spelling, find their |
| 409 | # definitions, etc. However, the specific kind of the declaration is not |
| 410 | # reported. |
| 411 | CursorKind.UNEXPOSED_DECL = CursorKind(1) |
| 412 | |
| 413 | # A C or C++ struct. |
| 414 | CursorKind.STRUCT_DECL = CursorKind(2) |
| 415 | |
| 416 | # A C or C++ union. |
| 417 | CursorKind.UNION_DECL = CursorKind(3) |
| 418 | |
| 419 | # A C++ class. |
| 420 | CursorKind.CLASS_DECL = CursorKind(4) |
| 421 | |
| 422 | # An enumeration. |
| 423 | CursorKind.ENUM_DECL = CursorKind(5) |
| 424 | |
| 425 | # A field (in C) or non-static data member (in C++) in a struct, union, or C++ |
| 426 | # class. |
| 427 | CursorKind.FIELD_DECL = CursorKind(6) |
| 428 | |
| 429 | # An enumerator constant. |
| 430 | CursorKind.ENUM_CONSTANT_DECL = CursorKind(7) |
| 431 | |
| 432 | # A function. |
| 433 | CursorKind.FUNCTION_DECL = CursorKind(8) |
| 434 | |
| 435 | # A variable. |
| 436 | CursorKind.VAR_DECL = CursorKind(9) |
| 437 | |
| 438 | # A function or method parameter. |
| 439 | CursorKind.PARM_DECL = CursorKind(10) |
| 440 | |
| 441 | # An Objective-C @interface. |
| 442 | CursorKind.OBJC_INTERFACE_DECL = CursorKind(11) |
| 443 | |
| 444 | # An Objective-C @interface for a category. |
| 445 | CursorKind.OBJC_CATEGORY_DECL = CursorKind(12) |
| 446 | |
| 447 | # An Objective-C @protocol declaration. |
| 448 | CursorKind.OBJC_PROTOCOL_DECL = CursorKind(13) |
| 449 | |
| 450 | # An Objective-C @property declaration. |
| 451 | CursorKind.OBJC_PROPERTY_DECL = CursorKind(14) |
| 452 | |
| 453 | # An Objective-C instance variable. |
| 454 | CursorKind.OBJC_IVAR_DECL = CursorKind(15) |
| 455 | |
| 456 | # An Objective-C instance method. |
| 457 | CursorKind.OBJC_INSTANCE_METHOD_DECL = CursorKind(16) |
| 458 | |
| 459 | # An Objective-C class method. |
| 460 | CursorKind.OBJC_CLASS_METHOD_DECL = CursorKind(17) |
| 461 | |
| 462 | # An Objective-C @implementation. |
| 463 | CursorKind.OBJC_IMPLEMENTATION_DECL = CursorKind(18) |
| 464 | |
| 465 | # An Objective-C @implementation for a category. |
| 466 | CursorKind.OBJC_CATEGORY_IMPL_DECL = CursorKind(19) |
| 467 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 468 | # A typedef. |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 469 | CursorKind.TYPEDEF_DECL = CursorKind(20) |
| 470 | |
Tobias Grosser | 4ed73ce | 2011-02-05 17:53:47 +0000 | [diff] [blame] | 471 | # A C++ class method. |
| 472 | CursorKind.CXX_METHOD = CursorKind(21) |
| 473 | |
| 474 | # A C++ namespace. |
| 475 | CursorKind.NAMESPACE = CursorKind(22) |
| 476 | |
| 477 | # A linkage specification, e.g. 'extern "C"'. |
| 478 | CursorKind.LINKAGE_SPEC = CursorKind(23) |
| 479 | |
| 480 | # A C++ constructor. |
| 481 | CursorKind.CONSTRUCTOR = CursorKind(24) |
| 482 | |
| 483 | # A C++ destructor. |
| 484 | CursorKind.DESTRUCTOR = CursorKind(25) |
| 485 | |
| 486 | # A C++ conversion function. |
| 487 | CursorKind.CONVERSION_FUNCTION = CursorKind(26) |
| 488 | |
| 489 | # A C++ template type parameter |
| 490 | CursorKind.TEMPLATE_TYPE_PARAMETER = CursorKind(27) |
| 491 | |
| 492 | # A C++ non-type template paramater. |
| 493 | CursorKind.TEMPLATE_NON_TYPE_PARAMETER = CursorKind(28) |
| 494 | |
| 495 | # A C++ template template parameter. |
| 496 | CursorKind.TEMPLATE_TEMPLATE_PARAMTER = CursorKind(29) |
| 497 | |
| 498 | # A C++ function template. |
| 499 | CursorKind.FUNCTION_TEMPLATE = CursorKind(30) |
| 500 | |
| 501 | # A C++ class template. |
| 502 | CursorKind.CLASS_TEMPLATE = CursorKind(31) |
| 503 | |
| 504 | # A C++ class template partial specialization. |
| 505 | CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION = CursorKind(32) |
| 506 | |
| 507 | # A C++ namespace alias declaration. |
| 508 | CursorKind.NAMESPACE_ALIAS = CursorKind(33) |
| 509 | |
| 510 | # A C++ using directive |
| 511 | CursorKind.USING_DIRECTIVE = CursorKind(34) |
| 512 | |
| 513 | # A C++ using declaration |
| 514 | CursorKind.USING_DECLARATION = CursorKind(35) |
| 515 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 516 | # A Type alias decl. |
| 517 | CursorKind.TYPE_ALIAS_DECL = CursorKind(36) |
| 518 | |
| 519 | # A Objective-C synthesize decl |
| 520 | CursorKind.OBJC_SYNTHESIZE_DECL = CursorKind(37) |
| 521 | |
| 522 | # A Objective-C dynamic decl |
| 523 | CursorKind.OBJC_DYNAMIC_DECL = CursorKind(38) |
| 524 | |
| 525 | # A C++ access specifier decl. |
| 526 | CursorKind.CXX_ACCESS_SPEC_DECL = CursorKind(39) |
| 527 | |
| 528 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 529 | ### |
| 530 | # Reference Kinds |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 531 | |
| 532 | CursorKind.OBJC_SUPER_CLASS_REF = CursorKind(40) |
| 533 | CursorKind.OBJC_PROTOCOL_REF = CursorKind(41) |
| 534 | CursorKind.OBJC_CLASS_REF = CursorKind(42) |
| 535 | |
| 536 | # A reference to a type declaration. |
| 537 | # |
| 538 | # A type reference occurs anywhere where a type is named but not |
| 539 | # declared. For example, given: |
| 540 | # typedef unsigned size_type; |
| 541 | # size_type size; |
| 542 | # |
| 543 | # The typedef is a declaration of size_type (CXCursor_TypedefDecl), |
| 544 | # while the type of the variable "size" is referenced. The cursor |
| 545 | # referenced by the type of size is the typedef for size_type. |
| 546 | CursorKind.TYPE_REF = CursorKind(43) |
Tobias Grosser | 4ed73ce | 2011-02-05 17:53:47 +0000 | [diff] [blame] | 547 | CursorKind.CXX_BASE_SPECIFIER = CursorKind(44) |
| 548 | |
| 549 | # A reference to a class template, function template, template |
| 550 | # template parameter, or class template partial specialization. |
| 551 | CursorKind.TEMPLATE_REF = CursorKind(45) |
| 552 | |
| 553 | # A reference to a namespace or namepsace alias. |
| 554 | CursorKind.NAMESPACE_REF = CursorKind(46) |
| 555 | |
| 556 | # A reference to a member of a struct, union, or class that occurs in |
| 557 | # some non-expression context, e.g., a designated initializer. |
| 558 | CursorKind.MEMBER_REF = CursorKind(47) |
| 559 | |
| 560 | # A reference to a labeled statement. |
| 561 | CursorKind.LABEL_REF = CursorKind(48) |
| 562 | |
| 563 | # A reference toa a set of overloaded functions or function templates |
| 564 | # that has not yet been resolved to a specific function or function template. |
| 565 | CursorKind.OVERLOADED_DECL_REF = CursorKind(49) |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 566 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 567 | ### |
| 568 | # Invalid/Error Kinds |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 569 | |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 570 | CursorKind.INVALID_FILE = CursorKind(70) |
| 571 | CursorKind.NO_DECL_FOUND = CursorKind(71) |
| 572 | CursorKind.NOT_IMPLEMENTED = CursorKind(72) |
Tobias Grosser | 4ed73ce | 2011-02-05 17:53:47 +0000 | [diff] [blame] | 573 | CursorKind.INVALID_CODE = CursorKind(73) |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 574 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 575 | ### |
| 576 | # Expression Kinds |
| 577 | |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 578 | # An expression whose specific kind is not exposed via this interface. |
| 579 | # |
| 580 | # Unexposed expressions have the same operations as any other kind of |
| 581 | # expression; one can extract their location information, spelling, children, |
| 582 | # etc. However, the specific kind of the expression is not reported. |
| 583 | CursorKind.UNEXPOSED_EXPR = CursorKind(100) |
| 584 | |
| 585 | # An expression that refers to some value declaration, such as a function, |
| 586 | # varible, or enumerator. |
| 587 | CursorKind.DECL_REF_EXPR = CursorKind(101) |
| 588 | |
| 589 | # An expression that refers to a member of a struct, union, class, Objective-C |
| 590 | # class, etc. |
| 591 | CursorKind.MEMBER_REF_EXPR = CursorKind(102) |
| 592 | |
| 593 | # An expression that calls a function. |
| 594 | CursorKind.CALL_EXPR = CursorKind(103) |
| 595 | |
| 596 | # An expression that sends a message to an Objective-C object or class. |
| 597 | CursorKind.OBJC_MESSAGE_EXPR = CursorKind(104) |
| 598 | |
Tobias Grosser | 4ed73ce | 2011-02-05 17:53:47 +0000 | [diff] [blame] | 599 | # An expression that represents a block literal. |
| 600 | CursorKind.BLOCK_EXPR = CursorKind(105) |
| 601 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 602 | # An integer literal. |
| 603 | CursorKind.INTEGER_LITERAL = CursorKind(106) |
| 604 | |
| 605 | # A floating point number literal. |
| 606 | CursorKind.FLOATING_LITERAL = CursorKind(107) |
| 607 | |
| 608 | # An imaginary number literal. |
| 609 | CursorKind.IMAGINARY_LITERAL = CursorKind(108) |
| 610 | |
| 611 | # A string literal. |
| 612 | CursorKind.STRING_LITERAL = CursorKind(109) |
| 613 | |
| 614 | # A character literal. |
| 615 | CursorKind.CHARACTER_LITERAL = CursorKind(110) |
| 616 | |
| 617 | # A parenthesized expression, e.g. "(1)". |
| 618 | # |
| 619 | # This AST node is only formed if full location information is requested. |
| 620 | CursorKind.PAREN_EXPR = CursorKind(111) |
| 621 | |
| 622 | # This represents the unary-expression's (except sizeof and |
| 623 | # alignof). |
| 624 | CursorKind.UNARY_OPERATOR = CursorKind(112) |
| 625 | |
| 626 | # [C99 6.5.2.1] Array Subscripting. |
| 627 | CursorKind.ARRAY_SUBSCRIPT_EXPR = CursorKind(113) |
| 628 | |
| 629 | # A builtin binary operation expression such as "x + y" or |
| 630 | # "x <= y". |
| 631 | CursorKind.BINARY_OPERATOR = CursorKind(114) |
| 632 | |
| 633 | # Compound assignment such as "+=". |
| 634 | CursorKind.COMPOUND_ASSIGNMENT_OPERATOR = CursorKind(115) |
| 635 | |
| 636 | # The ?: ternary operator. |
| 637 | CursorKind.CONDITONAL_OPERATOR = CursorKind(116) |
| 638 | |
| 639 | # An explicit cast in C (C99 6.5.4) or a C-style cast in C++ |
| 640 | # (C++ [expr.cast]), which uses the syntax (Type)expr. |
| 641 | # |
| 642 | # For example: (int)f. |
| 643 | CursorKind.CSTYLE_CAST_EXPR = CursorKind(117) |
| 644 | |
| 645 | # [C99 6.5.2.5] |
| 646 | CursorKind.COMPOUND_LITERAL_EXPR = CursorKind(118) |
| 647 | |
| 648 | # Describes an C or C++ initializer list. |
| 649 | CursorKind.INIT_LIST_EXPR = CursorKind(119) |
| 650 | |
| 651 | # The GNU address of label extension, representing &&label. |
| 652 | CursorKind.ADDR_LABEL_EXPR = CursorKind(120) |
| 653 | |
| 654 | # This is the GNU Statement Expression extension: ({int X=4; X;}) |
| 655 | CursorKind.StmtExpr = CursorKind(121) |
| 656 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 657 | # Represents a C11 generic selection. |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 658 | CursorKind.GENERIC_SELECTION_EXPR = CursorKind(122) |
| 659 | |
| 660 | # Implements the GNU __null extension, which is a name for a null |
| 661 | # pointer constant that has integral type (e.g., int or long) and is the same |
| 662 | # size and alignment as a pointer. |
| 663 | # |
| 664 | # The __null extension is typically only used by system headers, which define |
| 665 | # NULL as __null in C++ rather than using 0 (which is an integer that may not |
| 666 | # match the size of a pointer). |
| 667 | CursorKind.GNU_NULL_EXPR = CursorKind(123) |
| 668 | |
| 669 | # C++'s static_cast<> expression. |
| 670 | CursorKind.CXX_STATIC_CAST_EXPR = CursorKind(124) |
| 671 | |
| 672 | # C++'s dynamic_cast<> expression. |
| 673 | CursorKind.CXX_DYNAMIC_CAST_EXPR = CursorKind(125) |
| 674 | |
| 675 | # C++'s reinterpret_cast<> expression. |
| 676 | CursorKind.CXX_REINTERPRET_CAST_EXPR = CursorKind(126) |
| 677 | |
| 678 | # C++'s const_cast<> expression. |
| 679 | CursorKind.CXX_CONST_CAST_EXPR = CursorKind(127) |
| 680 | |
| 681 | # Represents an explicit C++ type conversion that uses "functional" |
| 682 | # notion (C++ [expr.type.conv]). |
| 683 | # |
| 684 | # Example: |
| 685 | # \code |
| 686 | # x = int(0.5); |
| 687 | # \endcode |
| 688 | CursorKind.CXX_FUNCTIONAL_CAST_EXPR = CursorKind(128) |
| 689 | |
| 690 | # A C++ typeid expression (C++ [expr.typeid]). |
| 691 | CursorKind.CXX_TYPEID_EXPR = CursorKind(129) |
| 692 | |
| 693 | # [C++ 2.13.5] C++ Boolean Literal. |
| 694 | CursorKind.CXX_BOOL_LITERAL_EXPR = CursorKind(130) |
| 695 | |
| 696 | # [C++0x 2.14.7] C++ Pointer Literal. |
| 697 | CursorKind.CXX_NULL_PTR_LITERAL_EXPR = CursorKind(131) |
| 698 | |
| 699 | # Represents the "this" expression in C++ |
| 700 | CursorKind.CXX_THIS_EXPR = CursorKind(132) |
| 701 | |
| 702 | # [C++ 15] C++ Throw Expression. |
| 703 | # |
| 704 | # This handles 'throw' and 'throw' assignment-expression. When |
| 705 | # assignment-expression isn't present, Op will be null. |
| 706 | CursorKind.CXX_THROW_EXPR = CursorKind(133) |
| 707 | |
| 708 | # A new expression for memory allocation and constructor calls, e.g: |
| 709 | # "new CXXNewExpr(foo)". |
| 710 | CursorKind.CXX_NEW_EXPR = CursorKind(134) |
| 711 | |
| 712 | # A delete expression for memory deallocation and destructor calls, |
| 713 | # e.g. "delete[] pArray". |
| 714 | CursorKind.CXX_DELETE_EXPR = CursorKind(135) |
| 715 | |
| 716 | # Represents a unary expression. |
| 717 | CursorKind.CXX_UNARY_EXPR = CursorKind(136) |
| 718 | |
| 719 | # ObjCStringLiteral, used for Objective-C string literals i.e. "foo". |
| 720 | CursorKind.OBJC_STRING_LITERAL = CursorKind(137) |
| 721 | |
| 722 | # ObjCEncodeExpr, used for in Objective-C. |
| 723 | CursorKind.OBJC_ENCODE_EXPR = CursorKind(138) |
| 724 | |
| 725 | # ObjCSelectorExpr used for in Objective-C. |
| 726 | CursorKind.OBJC_SELECTOR_EXPR = CursorKind(139) |
| 727 | |
| 728 | # Objective-C's protocol expression. |
| 729 | CursorKind.OBJC_PROTOCOL_EXPR = CursorKind(140) |
| 730 | |
| 731 | # An Objective-C "bridged" cast expression, which casts between |
| 732 | # Objective-C pointers and C pointers, transferring ownership in the process. |
| 733 | # |
| 734 | # \code |
| 735 | # NSString *str = (__bridge_transfer NSString *)CFCreateString(); |
| 736 | # \endcode |
| 737 | CursorKind.OBJC_BRIDGE_CAST_EXPR = CursorKind(141) |
| 738 | |
| 739 | # Represents a C++0x pack expansion that produces a sequence of |
| 740 | # expressions. |
| 741 | # |
| 742 | # A pack expansion expression contains a pattern (which itself is an |
| 743 | # expression) followed by an ellipsis. For example: |
| 744 | CursorKind.PACK_EXPANSION_EXPR = CursorKind(142) |
| 745 | |
| 746 | # Represents an expression that computes the length of a parameter |
| 747 | # pack. |
| 748 | CursorKind.SIZE_OF_PACK_EXPR = CursorKind(143) |
| 749 | |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 750 | # A statement whose specific kind is not exposed via this interface. |
| 751 | # |
| 752 | # Unexposed statements have the same operations as any other kind of statement; |
| 753 | # one can extract their location information, spelling, children, etc. However, |
| 754 | # the specific kind of the statement is not reported. |
| 755 | CursorKind.UNEXPOSED_STMT = CursorKind(200) |
| 756 | |
Tobias Grosser | 4ed73ce | 2011-02-05 17:53:47 +0000 | [diff] [blame] | 757 | # A labelled statement in a function. |
| 758 | CursorKind.LABEL_STMT = CursorKind(201) |
| 759 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 760 | # A compound statement |
| 761 | CursorKind.COMPOUND_STMT = CursorKind(202) |
| 762 | |
| 763 | # A case statement. |
| 764 | CursorKind.CASE_STMT = CursorKind(203) |
| 765 | |
| 766 | # A default statement. |
| 767 | CursorKind.DEFAULT_STMT = CursorKind(204) |
| 768 | |
| 769 | # An if statement. |
| 770 | CursorKind.IF_STMT = CursorKind(205) |
| 771 | |
| 772 | # A switch statement. |
| 773 | CursorKind.SWITCH_STMT = CursorKind(206) |
| 774 | |
| 775 | # A while statement. |
| 776 | CursorKind.WHILE_STMT = CursorKind(207) |
| 777 | |
| 778 | # A do statement. |
| 779 | CursorKind.DO_STMT = CursorKind(208) |
| 780 | |
| 781 | # A for statement. |
| 782 | CursorKind.FOR_STMT = CursorKind(209) |
| 783 | |
| 784 | # A goto statement. |
| 785 | CursorKind.GOTO_STMT = CursorKind(210) |
| 786 | |
| 787 | # An indirect goto statement. |
| 788 | CursorKind.INDIRECT_GOTO_STMT = CursorKind(211) |
| 789 | |
| 790 | # A continue statement. |
| 791 | CursorKind.CONTINUE_STMT = CursorKind(212) |
| 792 | |
| 793 | # A break statement. |
| 794 | CursorKind.BREAK_STMT = CursorKind(213) |
| 795 | |
| 796 | # A return statement. |
| 797 | CursorKind.RETURN_STMT = CursorKind(214) |
| 798 | |
| 799 | # A GNU-style inline assembler statement. |
| 800 | CursorKind.ASM_STMT = CursorKind(215) |
| 801 | |
| 802 | # Objective-C's overall @try-@catch-@finally statement. |
| 803 | CursorKind.OBJC_AT_TRY_STMT = CursorKind(216) |
| 804 | |
| 805 | # Objective-C's @catch statement. |
| 806 | CursorKind.OBJC_AT_CATCH_STMT = CursorKind(217) |
| 807 | |
| 808 | # Objective-C's @finally statement. |
| 809 | CursorKind.OBJC_AT_FINALLY_STMT = CursorKind(218) |
| 810 | |
| 811 | # Objective-C's @throw statement. |
| 812 | CursorKind.OBJC_AT_THROW_STMT = CursorKind(219) |
| 813 | |
| 814 | # Objective-C's @synchronized statement. |
| 815 | CursorKind.OBJC_AT_SYNCHRONIZED_STMT = CursorKind(220) |
| 816 | |
| 817 | # Objective-C's autorealease pool statement. |
| 818 | CursorKind.OBJC_AUTORELEASE_POOL_STMT = CursorKind(221) |
| 819 | |
| 820 | # Objective-C's for collection statement. |
| 821 | CursorKind.OBJC_FOR_COLLECTION_STMT = CursorKind(222) |
| 822 | |
| 823 | # C++'s catch statement. |
| 824 | CursorKind.CXX_CATCH_STMT = CursorKind(223) |
| 825 | |
| 826 | # C++'s try statement. |
| 827 | CursorKind.CXX_TRY_STMT = CursorKind(224) |
| 828 | |
| 829 | # C++'s for (* : *) statement. |
| 830 | CursorKind.CXX_FOR_RANGE_STMT = CursorKind(225) |
| 831 | |
| 832 | # Windows Structured Exception Handling's try statement. |
| 833 | CursorKind.SEH_TRY_STMT = CursorKind(226) |
| 834 | |
| 835 | # Windows Structured Exception Handling's except statement. |
| 836 | CursorKind.SEH_EXCEPT_STMT = CursorKind(227) |
| 837 | |
| 838 | # Windows Structured Exception Handling's finally statement. |
| 839 | CursorKind.SEH_FINALLY_STMT = CursorKind(228) |
| 840 | |
| 841 | # The null statement. |
| 842 | CursorKind.NULL_STMT = CursorKind(230) |
| 843 | |
| 844 | # Adaptor class for mixing declarations with statements and expressions. |
| 845 | CursorKind.DECL_STMT = CursorKind(231) |
Tobias Grosser | 4ed73ce | 2011-02-05 17:53:47 +0000 | [diff] [blame] | 846 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 847 | ### |
| 848 | # Other Kinds |
| 849 | |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 850 | # Cursor that represents the translation unit itself. |
| 851 | # |
| 852 | # The translation unit cursor exists primarily to act as the root cursor for |
| 853 | # traversing the contents of a translation unit. |
| 854 | CursorKind.TRANSLATION_UNIT = CursorKind(300) |
| 855 | |
Tobias Grosser | 4ed73ce | 2011-02-05 17:53:47 +0000 | [diff] [blame] | 856 | ### |
| 857 | # Attributes |
| 858 | |
| 859 | # An attribute whoe specific kind is note exposed via this interface |
| 860 | CursorKind.UNEXPOSED_ATTR = CursorKind(400) |
| 861 | |
| 862 | CursorKind.IB_ACTION_ATTR = CursorKind(401) |
| 863 | CursorKind.IB_OUTLET_ATTR = CursorKind(402) |
| 864 | CursorKind.IB_OUTLET_COLLECTION_ATTR = CursorKind(403) |
| 865 | |
Rafael Espindola | bf3cc73 | 2011-12-30 15:27:22 +0000 | [diff] [blame] | 866 | CursorKind.CXX_FINAL_ATTR = CursorKind(404) |
| 867 | CursorKind.CXX_OVERRIDE_ATTR = CursorKind(405) |
| 868 | CursorKind.ANNOTATE_ATTR = CursorKind(406) |
| 869 | CursorKind.ASM_LABEL_ATTR = CursorKind(407) |
| 870 | |
Tobias Grosser | 4ed73ce | 2011-02-05 17:53:47 +0000 | [diff] [blame] | 871 | ### |
| 872 | # Preprocessing |
| 873 | CursorKind.PREPROCESSING_DIRECTIVE = CursorKind(500) |
| 874 | CursorKind.MACRO_DEFINITION = CursorKind(501) |
| 875 | CursorKind.MACRO_INSTANTIATION = CursorKind(502) |
| 876 | CursorKind.INCLUSION_DIRECTIVE = CursorKind(503) |
| 877 | |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 878 | ### Cursors ### |
| 879 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 880 | class Cursor(Structure): |
| 881 | """ |
| 882 | The Cursor class represents a reference to an element within the AST. It |
| 883 | acts as a kind of iterator. |
| 884 | """ |
Douglas Gregor | 2abfec3 | 2011-10-19 05:47:46 +0000 | [diff] [blame] | 885 | _fields_ = [("_kind_id", c_int), ("xdata", c_int), ("data", c_void_p * 3)] |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 886 | |
Tobias Grosser | 58ba8c9 | 2011-10-31 00:31:32 +0000 | [diff] [blame] | 887 | @staticmethod |
| 888 | def from_location(tu, location): |
| 889 | return Cursor_get(tu, location) |
| 890 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 891 | def __eq__(self, other): |
| 892 | return Cursor_eq(self, other) |
| 893 | |
| 894 | def __ne__(self, other): |
| 895 | return not Cursor_eq(self, other) |
| 896 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 897 | def is_definition(self): |
| 898 | """ |
| 899 | Returns true if the declaration pointed at by the cursor is also a |
| 900 | definition of that entity. |
| 901 | """ |
| 902 | return Cursor_is_def(self) |
| 903 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 904 | def get_definition(self): |
| 905 | """ |
| 906 | If the cursor is a reference to a declaration or a declaration of |
| 907 | some entity, return a cursor that points to the definition of that |
| 908 | entity. |
| 909 | """ |
| 910 | # TODO: Should probably check that this is either a reference or |
| 911 | # declaration prior to issuing the lookup. |
| 912 | return Cursor_def(self) |
| 913 | |
Daniel Dunbar | 3d855f8 | 2010-01-24 21:20:13 +0000 | [diff] [blame] | 914 | def get_usr(self): |
| 915 | """Return the Unified Symbol Resultion (USR) for the entity referenced |
| 916 | by the given cursor (or None). |
| 917 | |
| 918 | A Unified Symbol Resolution (USR) is a string that identifies a |
| 919 | particular entity (function, class, variable, etc.) within a |
| 920 | program. USRs can be compared across translation units to determine, |
| 921 | e.g., when references in one translation refer to an entity defined in |
| 922 | another translation unit.""" |
| 923 | return Cursor_usr(self) |
| 924 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 925 | @property |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 926 | def kind(self): |
| 927 | """Return the kind of this cursor.""" |
| 928 | return CursorKind.from_id(self._kind_id) |
| 929 | |
| 930 | @property |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 931 | def spelling(self): |
| 932 | """Return the spelling of the entity pointed at by the cursor.""" |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 933 | if not self.kind.is_declaration(): |
Daniel Dunbar | 3d855f8 | 2010-01-24 21:20:13 +0000 | [diff] [blame] | 934 | # FIXME: clang_getCursorSpelling should be fixed to not assert on |
| 935 | # this, for consistency with clang_getCursorUSR. |
| 936 | return None |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 937 | if not hasattr(self, '_spelling'): |
| 938 | self._spelling = Cursor_spelling(self) |
| 939 | return self._spelling |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 940 | |
| 941 | @property |
Douglas Gregor | b60a2be | 2011-08-30 00:15:34 +0000 | [diff] [blame] | 942 | def displayname(self): |
| 943 | """ |
| 944 | Return the display name for the entity referenced by this cursor. |
| 945 | |
| 946 | The display name contains extra information that helps identify the cursor, |
| 947 | such as the parameters of a function or template or the arguments of a |
| 948 | class template specialization. |
| 949 | """ |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 950 | if not hasattr(self, '_displayname'): |
| 951 | self._displayname = Cursor_displayname(self) |
| 952 | return self._displayname |
Douglas Gregor | b60a2be | 2011-08-30 00:15:34 +0000 | [diff] [blame] | 953 | |
| 954 | @property |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 955 | def location(self): |
| 956 | """ |
| 957 | Return the source location (the starting character) of the entity |
| 958 | pointed at by the cursor. |
| 959 | """ |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 960 | if not hasattr(self, '_loc'): |
| 961 | self._loc = Cursor_loc(self) |
| 962 | return self._loc |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 963 | |
| 964 | @property |
| 965 | def extent(self): |
| 966 | """ |
| 967 | Return the source range (the range of text) occupied by the entity |
| 968 | pointed at by the cursor. |
| 969 | """ |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 970 | if not hasattr(self, '_extent'): |
| 971 | self._extent = Cursor_extent(self) |
| 972 | return self._extent |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 973 | |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 974 | @property |
| 975 | def type(self): |
| 976 | """ |
Tobias Grosser | 2d10680 | 2012-02-05 12:15:56 +0000 | [diff] [blame] | 977 | Retrieve the Type (if any) of the entity pointed at by the cursor. |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 978 | """ |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 979 | if not hasattr(self, '_type'): |
| 980 | self._type = Cursor_type(self) |
| 981 | return self._type |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 982 | |
Tobias Grosser | 64e7bdc | 2012-02-05 11:42:03 +0000 | [diff] [blame] | 983 | @property |
Tobias Grosser | 28d939f | 2012-02-05 11:42:20 +0000 | [diff] [blame] | 984 | def underlying_typedef_type(self): |
| 985 | """Return the underlying type of a typedef declaration. |
| 986 | |
Tobias Grosser | 2d10680 | 2012-02-05 12:15:56 +0000 | [diff] [blame] | 987 | Returns a Type for the typedef this cursor is a declaration for. If |
Tobias Grosser | 28d939f | 2012-02-05 11:42:20 +0000 | [diff] [blame] | 988 | the current cursor is not a typedef, this raises. |
| 989 | """ |
| 990 | if not hasattr(self, '_underlying_type'): |
| 991 | assert self.kind.is_declaration() |
| 992 | self._underlying_type = Cursor_underlying_type(self) |
| 993 | |
| 994 | return self._underlying_type |
| 995 | |
| 996 | @property |
Tobias Grosser | eb9ff2e | 2012-02-05 11:42:25 +0000 | [diff] [blame] | 997 | def enum_type(self): |
| 998 | """Return the integer type of an enum declaration. |
| 999 | |
Tobias Grosser | 2d10680 | 2012-02-05 12:15:56 +0000 | [diff] [blame] | 1000 | Returns a Type corresponding to an integer. If the cursor is not for an |
Tobias Grosser | eb9ff2e | 2012-02-05 11:42:25 +0000 | [diff] [blame] | 1001 | enum, this raises. |
| 1002 | """ |
| 1003 | if not hasattr(self, '_enum_type'): |
| 1004 | assert self.kind == CursorKind.ENUM_DECL |
| 1005 | self._enum_type = Cursor_enum_type(self) |
| 1006 | |
| 1007 | return self._enum_type |
| 1008 | |
| 1009 | @property |
Tobias Grosser | 64e7bdc | 2012-02-05 11:42:03 +0000 | [diff] [blame] | 1010 | def hash(self): |
| 1011 | """Returns a hash of the cursor as an int.""" |
| 1012 | if not hasattr(self, '_hash'): |
| 1013 | self._hash = Cursor_hash(self) |
| 1014 | |
| 1015 | return self._hash |
| 1016 | |
Daniel Dunbar | de3b8e5 | 2010-01-24 04:10:22 +0000 | [diff] [blame] | 1017 | def get_children(self): |
Daniel Dunbar | 12bf15c | 2010-01-24 21:20:29 +0000 | [diff] [blame] | 1018 | """Return an iterator for accessing the children of this cursor.""" |
Daniel Dunbar | de3b8e5 | 2010-01-24 04:10:22 +0000 | [diff] [blame] | 1019 | |
| 1020 | # FIXME: Expose iteration from CIndex, PR6125. |
| 1021 | def visitor(child, parent, children): |
Daniel Dunbar | fb8ae17 | 2010-01-24 21:20:05 +0000 | [diff] [blame] | 1022 | # FIXME: Document this assertion in API. |
| 1023 | # FIXME: There should just be an isNull method. |
| 1024 | assert child != Cursor_null() |
Daniel Dunbar | de3b8e5 | 2010-01-24 04:10:22 +0000 | [diff] [blame] | 1025 | children.append(child) |
| 1026 | return 1 # continue |
| 1027 | children = [] |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1028 | Cursor_visit(self, Cursor_visit_callback(visitor), children) |
Daniel Dunbar | de3b8e5 | 2010-01-24 04:10:22 +0000 | [diff] [blame] | 1029 | return iter(children) |
| 1030 | |
Daniel Dunbar | fb8ae17 | 2010-01-24 21:20:05 +0000 | [diff] [blame] | 1031 | @staticmethod |
| 1032 | def from_result(res, fn, args): |
| 1033 | assert isinstance(res, Cursor) |
| 1034 | # FIXME: There should just be an isNull method. |
| 1035 | if res == Cursor_null(): |
| 1036 | return None |
| 1037 | return res |
| 1038 | |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 1039 | |
| 1040 | ### Type Kinds ### |
| 1041 | |
| 1042 | class TypeKind(object): |
| 1043 | """ |
| 1044 | Describes the kind of type. |
| 1045 | """ |
| 1046 | |
| 1047 | # The unique kind objects, indexed by id. |
| 1048 | _kinds = [] |
| 1049 | _name_map = None |
| 1050 | |
| 1051 | def __init__(self, value): |
| 1052 | if value >= len(TypeKind._kinds): |
| 1053 | TypeKind._kinds += [None] * (value - len(TypeKind._kinds) + 1) |
| 1054 | if TypeKind._kinds[value] is not None: |
| 1055 | raise ValueError,'TypeKind already loaded' |
| 1056 | self.value = value |
| 1057 | TypeKind._kinds[value] = self |
| 1058 | TypeKind._name_map = None |
| 1059 | |
| 1060 | def from_param(self): |
| 1061 | return self.value |
| 1062 | |
| 1063 | @property |
| 1064 | def name(self): |
| 1065 | """Get the enumeration name of this cursor kind.""" |
| 1066 | if self._name_map is None: |
| 1067 | self._name_map = {} |
| 1068 | for key,value in TypeKind.__dict__.items(): |
| 1069 | if isinstance(value,TypeKind): |
| 1070 | self._name_map[value] = key |
| 1071 | return self._name_map[self] |
| 1072 | |
| 1073 | @staticmethod |
| 1074 | def from_id(id): |
| 1075 | if id >= len(TypeKind._kinds) or TypeKind._kinds[id] is None: |
Douglas Gregor | 9d342ab | 2011-10-19 05:49:29 +0000 | [diff] [blame] | 1076 | raise ValueError,'Unknown type kind %d' % id |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 1077 | return TypeKind._kinds[id] |
| 1078 | |
| 1079 | def __repr__(self): |
| 1080 | return 'TypeKind.%s' % (self.name,) |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | TypeKind.INVALID = TypeKind(0) |
| 1085 | TypeKind.UNEXPOSED = TypeKind(1) |
| 1086 | TypeKind.VOID = TypeKind(2) |
| 1087 | TypeKind.BOOL = TypeKind(3) |
| 1088 | TypeKind.CHAR_U = TypeKind(4) |
| 1089 | TypeKind.UCHAR = TypeKind(5) |
| 1090 | TypeKind.CHAR16 = TypeKind(6) |
| 1091 | TypeKind.CHAR32 = TypeKind(7) |
| 1092 | TypeKind.USHORT = TypeKind(8) |
| 1093 | TypeKind.UINT = TypeKind(9) |
| 1094 | TypeKind.ULONG = TypeKind(10) |
| 1095 | TypeKind.ULONGLONG = TypeKind(11) |
| 1096 | TypeKind.UINT128 = TypeKind(12) |
| 1097 | TypeKind.CHAR_S = TypeKind(13) |
| 1098 | TypeKind.SCHAR = TypeKind(14) |
| 1099 | TypeKind.WCHAR = TypeKind(15) |
| 1100 | TypeKind.SHORT = TypeKind(16) |
| 1101 | TypeKind.INT = TypeKind(17) |
| 1102 | TypeKind.LONG = TypeKind(18) |
| 1103 | TypeKind.LONGLONG = TypeKind(19) |
| 1104 | TypeKind.INT128 = TypeKind(20) |
| 1105 | TypeKind.FLOAT = TypeKind(21) |
| 1106 | TypeKind.DOUBLE = TypeKind(22) |
| 1107 | TypeKind.LONGDOUBLE = TypeKind(23) |
| 1108 | TypeKind.NULLPTR = TypeKind(24) |
| 1109 | TypeKind.OVERLOAD = TypeKind(25) |
| 1110 | TypeKind.DEPENDENT = TypeKind(26) |
| 1111 | TypeKind.OBJCID = TypeKind(27) |
| 1112 | TypeKind.OBJCCLASS = TypeKind(28) |
| 1113 | TypeKind.OBJCSEL = TypeKind(29) |
| 1114 | TypeKind.COMPLEX = TypeKind(100) |
| 1115 | TypeKind.POINTER = TypeKind(101) |
| 1116 | TypeKind.BLOCKPOINTER = TypeKind(102) |
| 1117 | TypeKind.LVALUEREFERENCE = TypeKind(103) |
| 1118 | TypeKind.RVALUEREFERENCE = TypeKind(104) |
| 1119 | TypeKind.RECORD = TypeKind(105) |
| 1120 | TypeKind.ENUM = TypeKind(106) |
| 1121 | TypeKind.TYPEDEF = TypeKind(107) |
| 1122 | TypeKind.OBJCINTERFACE = TypeKind(108) |
| 1123 | TypeKind.OBJCOBJECTPOINTER = TypeKind(109) |
| 1124 | TypeKind.FUNCTIONNOPROTO = TypeKind(110) |
| 1125 | TypeKind.FUNCTIONPROTO = TypeKind(111) |
Douglas Gregor | 38d2d55 | 2011-10-19 05:50:34 +0000 | [diff] [blame] | 1126 | TypeKind.CONSTANTARRAY = TypeKind(112) |
Tobias Grosser | 250d217 | 2012-02-05 11:42:14 +0000 | [diff] [blame] | 1127 | TypeKind.VECTOR = TypeKind(113) |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 1128 | |
| 1129 | class Type(Structure): |
| 1130 | """ |
| 1131 | The type of an element in the abstract syntax tree. |
| 1132 | """ |
| 1133 | _fields_ = [("_kind_id", c_int), ("data", c_void_p * 2)] |
| 1134 | |
| 1135 | @property |
| 1136 | def kind(self): |
| 1137 | """Return the kind of this type.""" |
| 1138 | return TypeKind.from_id(self._kind_id) |
| 1139 | |
Gregory Szorc | 8605760 | 2012-02-17 07:44:46 +0000 | [diff] [blame] | 1140 | @property |
| 1141 | def element_type(self): |
| 1142 | """Retrieve the Type of elements within this Type. |
| 1143 | |
| 1144 | If accessed on a type that is not an array, complex, or vector type, an |
| 1145 | exception will be raised. |
| 1146 | """ |
| 1147 | result = Type_get_element_type(self) |
| 1148 | if result.kind == TypeKind.INVALID: |
| 1149 | raise Exception('Element type not available on this type.') |
| 1150 | |
| 1151 | return result |
| 1152 | |
Gregory Szorc | bf8ca00 | 2012-02-17 07:47:38 +0000 | [diff] [blame] | 1153 | @property |
| 1154 | def element_count(self): |
| 1155 | """Retrieve the number of elements in this type. |
| 1156 | |
| 1157 | Returns an int. |
| 1158 | |
| 1159 | If the Type is not an array or vector, this raises. |
| 1160 | """ |
| 1161 | result = Type_get_num_elements(self) |
| 1162 | if result < 0: |
| 1163 | raise Exception('Type does not have elements.') |
| 1164 | |
| 1165 | return result |
| 1166 | |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 1167 | @staticmethod |
| 1168 | def from_result(res, fn, args): |
| 1169 | assert isinstance(res, Type) |
| 1170 | return res |
| 1171 | |
| 1172 | def get_canonical(self): |
| 1173 | """ |
| 1174 | Return the canonical type for a Type. |
| 1175 | |
| 1176 | Clang's type system explicitly models typedefs and all the |
| 1177 | ways a specific type can be represented. The canonical type |
| 1178 | is the underlying type with all the "sugar" removed. For |
| 1179 | example, if 'T' is a typedef for 'int', the canonical type for |
| 1180 | 'T' would be 'int'. |
| 1181 | """ |
| 1182 | return Type_get_canonical(self) |
| 1183 | |
| 1184 | def is_const_qualified(self): |
| 1185 | """ |
| 1186 | Determine whether a Type has the "const" qualifier set, |
| 1187 | without looking through typedefs that may have added "const" |
| 1188 | at a different level. |
| 1189 | """ |
| 1190 | return Type_is_const_qualified(self) |
| 1191 | |
| 1192 | def is_volatile_qualified(self): |
| 1193 | """ |
| 1194 | Determine whether a Type has the "volatile" qualifier set, |
| 1195 | without looking through typedefs that may have added |
| 1196 | "volatile" at a different level. |
| 1197 | """ |
| 1198 | return Type_is_volatile_qualified(self) |
| 1199 | |
| 1200 | def is_restrict_qualified(self): |
| 1201 | """ |
| 1202 | Determine whether a Type has the "restrict" qualifier set, |
| 1203 | without looking through typedefs that may have added |
| 1204 | "restrict" at a different level. |
| 1205 | """ |
| 1206 | return Type_is_restrict_qualified(self) |
| 1207 | |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame^] | 1208 | def is_function_variadic(self): |
| 1209 | """Determine whether this function Type is a variadic function type.""" |
| 1210 | assert self.kind == TypeKind.FUNCTIONPROTO |
| 1211 | |
| 1212 | return Type_is_variadic(self) |
| 1213 | |
Gregory Szorc | 96ad633 | 2012-02-05 19:42:06 +0000 | [diff] [blame] | 1214 | def is_pod(self): |
| 1215 | """Determine whether this Type represents plain old data (POD).""" |
| 1216 | return Type_is_pod(self) |
| 1217 | |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 1218 | def get_pointee(self): |
| 1219 | """ |
| 1220 | For pointer types, returns the type of the pointee. |
| 1221 | """ |
| 1222 | return Type_get_pointee(self) |
| 1223 | |
| 1224 | def get_declaration(self): |
| 1225 | """ |
| 1226 | Return the cursor for the declaration of the given type. |
| 1227 | """ |
| 1228 | return Type_get_declaration(self) |
| 1229 | |
| 1230 | def get_result(self): |
| 1231 | """ |
| 1232 | Retrieve the result type associated with a function type. |
| 1233 | """ |
| 1234 | return Type_get_result(self) |
| 1235 | |
Douglas Gregor | 13102ff | 2011-10-19 05:51:43 +0000 | [diff] [blame] | 1236 | def get_array_element_type(self): |
| 1237 | """ |
| 1238 | Retrieve the type of the elements of the array type. |
| 1239 | """ |
| 1240 | return Type_get_array_element(self) |
| 1241 | |
| 1242 | def get_array_size(self): |
| 1243 | """ |
| 1244 | Retrieve the size of the constant array. |
| 1245 | """ |
| 1246 | return Type_get_array_size(self) |
| 1247 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1248 | ## CIndex Objects ## |
| 1249 | |
| 1250 | # CIndex objects (derived from ClangObject) are essentially lightweight |
| 1251 | # wrappers attached to some underlying object, which is exposed via CIndex as |
| 1252 | # a void*. |
| 1253 | |
| 1254 | class ClangObject(object): |
| 1255 | """ |
| 1256 | A helper for Clang objects. This class helps act as an intermediary for |
| 1257 | the ctypes library and the Clang CIndex library. |
| 1258 | """ |
| 1259 | def __init__(self, obj): |
| 1260 | assert isinstance(obj, c_object_p) and obj |
| 1261 | self.obj = self._as_parameter_ = obj |
| 1262 | |
| 1263 | def from_param(self): |
| 1264 | return self._as_parameter_ |
| 1265 | |
Daniel Dunbar | 5b534f6 | 2010-01-25 00:44:11 +0000 | [diff] [blame] | 1266 | |
| 1267 | class _CXUnsavedFile(Structure): |
| 1268 | """Helper for passing unsaved file arguments.""" |
| 1269 | _fields_ = [("name", c_char_p), ("contents", c_char_p), ('length', c_ulong)] |
| 1270 | |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1271 | ## Diagnostic Conversion ## |
| 1272 | |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1273 | _clang_getNumDiagnostics = lib.clang_getNumDiagnostics |
| 1274 | _clang_getNumDiagnostics.argtypes = [c_object_p] |
| 1275 | _clang_getNumDiagnostics.restype = c_uint |
| 1276 | |
| 1277 | _clang_getDiagnostic = lib.clang_getDiagnostic |
| 1278 | _clang_getDiagnostic.argtypes = [c_object_p, c_uint] |
| 1279 | _clang_getDiagnostic.restype = c_object_p |
| 1280 | |
| 1281 | _clang_disposeDiagnostic = lib.clang_disposeDiagnostic |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 1282 | _clang_disposeDiagnostic.argtypes = [Diagnostic] |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1283 | |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1284 | _clang_getDiagnosticSeverity = lib.clang_getDiagnosticSeverity |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 1285 | _clang_getDiagnosticSeverity.argtypes = [Diagnostic] |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1286 | _clang_getDiagnosticSeverity.restype = c_int |
| 1287 | |
| 1288 | _clang_getDiagnosticLocation = lib.clang_getDiagnosticLocation |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 1289 | _clang_getDiagnosticLocation.argtypes = [Diagnostic] |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1290 | _clang_getDiagnosticLocation.restype = SourceLocation |
| 1291 | |
| 1292 | _clang_getDiagnosticSpelling = lib.clang_getDiagnosticSpelling |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 1293 | _clang_getDiagnosticSpelling.argtypes = [Diagnostic] |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1294 | _clang_getDiagnosticSpelling.restype = _CXString |
| 1295 | _clang_getDiagnosticSpelling.errcheck = _CXString.from_result |
| 1296 | |
Daniel Dunbar | b51abe9 | 2010-02-13 18:33:03 +0000 | [diff] [blame] | 1297 | _clang_getDiagnosticNumRanges = lib.clang_getDiagnosticNumRanges |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 1298 | _clang_getDiagnosticNumRanges.argtypes = [Diagnostic] |
Daniel Dunbar | b51abe9 | 2010-02-13 18:33:03 +0000 | [diff] [blame] | 1299 | _clang_getDiagnosticNumRanges.restype = c_uint |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1300 | |
Daniel Dunbar | b51abe9 | 2010-02-13 18:33:03 +0000 | [diff] [blame] | 1301 | _clang_getDiagnosticRange = lib.clang_getDiagnosticRange |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 1302 | _clang_getDiagnosticRange.argtypes = [Diagnostic, c_uint] |
Daniel Dunbar | b51abe9 | 2010-02-13 18:33:03 +0000 | [diff] [blame] | 1303 | _clang_getDiagnosticRange.restype = SourceRange |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1304 | |
| 1305 | _clang_getDiagnosticNumFixIts = lib.clang_getDiagnosticNumFixIts |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 1306 | _clang_getDiagnosticNumFixIts.argtypes = [Diagnostic] |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1307 | _clang_getDiagnosticNumFixIts.restype = c_uint |
| 1308 | |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1309 | _clang_getDiagnosticFixIt = lib.clang_getDiagnosticFixIt |
Tobias Grosser | ff090ca | 2011-02-05 17:53:48 +0000 | [diff] [blame] | 1310 | _clang_getDiagnosticFixIt.argtypes = [Diagnostic, c_uint, POINTER(SourceRange)] |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1311 | _clang_getDiagnosticFixIt.restype = _CXString |
| 1312 | _clang_getDiagnosticFixIt.errcheck = _CXString.from_result |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1313 | |
Tobias Grosser | ea40382 | 2012-02-05 11:41:58 +0000 | [diff] [blame] | 1314 | _clang_getDiagnosticCategory = lib.clang_getDiagnosticCategory |
| 1315 | _clang_getDiagnosticCategory.argtypes = [Diagnostic] |
| 1316 | _clang_getDiagnosticCategory.restype = c_uint |
| 1317 | |
| 1318 | _clang_getDiagnosticCategoryName = lib.clang_getDiagnosticCategoryName |
| 1319 | _clang_getDiagnosticCategoryName.argtypes = [c_uint] |
| 1320 | _clang_getDiagnosticCategoryName.restype = _CXString |
| 1321 | _clang_getDiagnosticCategoryName.errcheck = _CXString.from_result |
| 1322 | |
| 1323 | _clang_getDiagnosticOption = lib.clang_getDiagnosticOption |
| 1324 | _clang_getDiagnosticOption.argtypes = [Diagnostic, POINTER(_CXString)] |
| 1325 | _clang_getDiagnosticOption.restype = _CXString |
| 1326 | _clang_getDiagnosticOption.errcheck = _CXString.from_result |
| 1327 | |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1328 | ### |
| 1329 | |
Tobias Grosser | a87dbcc | 2011-02-05 17:54:10 +0000 | [diff] [blame] | 1330 | class CompletionChunk: |
| 1331 | class Kind: |
| 1332 | def __init__(self, name): |
| 1333 | self.name = name |
| 1334 | |
| 1335 | def __str__(self): |
| 1336 | return self.name |
| 1337 | |
| 1338 | def __repr__(self): |
| 1339 | return "<ChunkKind: %s>" % self |
| 1340 | |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1341 | def __init__(self, completionString, key): |
| 1342 | self.cs = completionString |
| 1343 | self.key = key |
| 1344 | |
| 1345 | def __repr__(self): |
Tobias Grosser | a87dbcc | 2011-02-05 17:54:10 +0000 | [diff] [blame] | 1346 | return "{'" + self.spelling + "', " + str(self.kind) + "}" |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1347 | |
| 1348 | @property |
| 1349 | def spelling(self): |
| 1350 | return _clang_getCompletionChunkText(self.cs, self.key).spelling |
| 1351 | |
| 1352 | @property |
| 1353 | def kind(self): |
Tobias Grosser | a87dbcc | 2011-02-05 17:54:10 +0000 | [diff] [blame] | 1354 | res = _clang_getCompletionChunkKind(self.cs, self.key) |
| 1355 | return completionChunkKindMap[res] |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1356 | |
| 1357 | @property |
| 1358 | def string(self): |
| 1359 | res = _clang_getCompletionChunkCompletionString(self.cs, self.key) |
| 1360 | |
| 1361 | if (res): |
| 1362 | return CompletionString(res) |
| 1363 | else: |
| 1364 | None |
| 1365 | |
Tobias Grosser | a87dbcc | 2011-02-05 17:54:10 +0000 | [diff] [blame] | 1366 | def isKindOptional(self): |
| 1367 | return self.kind == completionChunkKindMap[0] |
| 1368 | |
| 1369 | def isKindTypedText(self): |
| 1370 | return self.kind == completionChunkKindMap[1] |
| 1371 | |
| 1372 | def isKindPlaceHolder(self): |
| 1373 | return self.kind == completionChunkKindMap[3] |
| 1374 | |
| 1375 | def isKindInformative(self): |
| 1376 | return self.kind == completionChunkKindMap[4] |
| 1377 | |
| 1378 | def isKindResultType(self): |
| 1379 | return self.kind == completionChunkKindMap[15] |
| 1380 | |
| 1381 | completionChunkKindMap = { |
| 1382 | 0: CompletionChunk.Kind("Optional"), |
| 1383 | 1: CompletionChunk.Kind("TypedText"), |
| 1384 | 2: CompletionChunk.Kind("Text"), |
| 1385 | 3: CompletionChunk.Kind("Placeholder"), |
| 1386 | 4: CompletionChunk.Kind("Informative"), |
| 1387 | 5: CompletionChunk.Kind("CurrentParameter"), |
| 1388 | 6: CompletionChunk.Kind("LeftParen"), |
| 1389 | 7: CompletionChunk.Kind("RightParen"), |
| 1390 | 8: CompletionChunk.Kind("LeftBracket"), |
| 1391 | 9: CompletionChunk.Kind("RightBracket"), |
| 1392 | 10: CompletionChunk.Kind("LeftBrace"), |
| 1393 | 11: CompletionChunk.Kind("RightBrace"), |
| 1394 | 12: CompletionChunk.Kind("LeftAngle"), |
| 1395 | 13: CompletionChunk.Kind("RightAngle"), |
| 1396 | 14: CompletionChunk.Kind("Comma"), |
| 1397 | 15: CompletionChunk.Kind("ResultType"), |
| 1398 | 16: CompletionChunk.Kind("Colon"), |
| 1399 | 17: CompletionChunk.Kind("SemiColon"), |
| 1400 | 18: CompletionChunk.Kind("Equal"), |
| 1401 | 19: CompletionChunk.Kind("HorizontalSpace"), |
| 1402 | 20: CompletionChunk.Kind("VerticalSpace")} |
| 1403 | |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1404 | class CompletionString(ClangObject): |
Tobias Grosser | a87dbcc | 2011-02-05 17:54:10 +0000 | [diff] [blame] | 1405 | class Availability: |
| 1406 | def __init__(self, name): |
| 1407 | self.name = name |
| 1408 | |
| 1409 | def __str__(self): |
| 1410 | return self.name |
| 1411 | |
| 1412 | def __repr__(self): |
| 1413 | return "<Availability: %s>" % self |
| 1414 | |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1415 | def __len__(self): |
| 1416 | return _clang_getNumCompletionChunks(self.obj) |
| 1417 | |
| 1418 | def __getitem__(self, key): |
| 1419 | if len(self) <= key: |
| 1420 | raise IndexError |
Tobias Grosser | a87dbcc | 2011-02-05 17:54:10 +0000 | [diff] [blame] | 1421 | return CompletionChunk(self.obj, key) |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1422 | |
| 1423 | @property |
| 1424 | def priority(self): |
| 1425 | return _clang_getCompletionPriority(self.obj) |
| 1426 | |
| 1427 | @property |
| 1428 | def availability(self): |
Tobias Grosser | a87dbcc | 2011-02-05 17:54:10 +0000 | [diff] [blame] | 1429 | res = _clang_getCompletionAvailability(self.obj) |
| 1430 | return availabilityKinds[res] |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1431 | |
| 1432 | def __repr__(self): |
Tobias Grosser | a87dbcc | 2011-02-05 17:54:10 +0000 | [diff] [blame] | 1433 | return " | ".join([str(a) for a in self]) \ |
| 1434 | + " || Priority: " + str(self.priority) \ |
| 1435 | + " || Availability: " + str(self.availability) |
| 1436 | |
| 1437 | availabilityKinds = { |
| 1438 | 0: CompletionChunk.Kind("Available"), |
| 1439 | 1: CompletionChunk.Kind("Deprecated"), |
| 1440 | 2: CompletionChunk.Kind("NotAvailable")} |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1441 | |
Tobias Grosser | 0a16680 | 2011-02-05 17:54:04 +0000 | [diff] [blame] | 1442 | class CodeCompletionResult(Structure): |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1443 | _fields_ = [('cursorKind', c_int), ('completionString', c_object_p)] |
| 1444 | |
| 1445 | def __repr__(self): |
| 1446 | return str(CompletionString(self.completionString)) |
| 1447 | |
| 1448 | @property |
| 1449 | def kind(self): |
| 1450 | return CursorKind.from_id(self.cursorKind) |
| 1451 | |
| 1452 | @property |
| 1453 | def string(self): |
| 1454 | return CompletionString(self.completionString) |
Tobias Grosser | 0a16680 | 2011-02-05 17:54:04 +0000 | [diff] [blame] | 1455 | |
| 1456 | class CCRStructure(Structure): |
| 1457 | _fields_ = [('results', POINTER(CodeCompletionResult)), |
| 1458 | ('numResults', c_int)] |
| 1459 | |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1460 | def __len__(self): |
| 1461 | return self.numResults |
| 1462 | |
| 1463 | def __getitem__(self, key): |
| 1464 | if len(self) <= key: |
| 1465 | raise IndexError |
| 1466 | |
| 1467 | return self.results[key] |
| 1468 | |
Tobias Grosser | 0a16680 | 2011-02-05 17:54:04 +0000 | [diff] [blame] | 1469 | class CodeCompletionResults(ClangObject): |
| 1470 | def __init__(self, ptr): |
| 1471 | assert isinstance(ptr, POINTER(CCRStructure)) and ptr |
| 1472 | self.ptr = self._as_parameter_ = ptr |
| 1473 | |
| 1474 | def from_param(self): |
| 1475 | return self._as_parameter_ |
| 1476 | |
| 1477 | def __del__(self): |
| 1478 | CodeCompletionResults_dispose(self) |
| 1479 | |
| 1480 | @property |
| 1481 | def results(self): |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 1482 | return self.ptr.contents |
Tobias Grosser | 0a16680 | 2011-02-05 17:54:04 +0000 | [diff] [blame] | 1483 | |
| 1484 | @property |
| 1485 | def diagnostics(self): |
| 1486 | class DiagnosticsItr: |
| 1487 | def __init__(self, ccr): |
| 1488 | self.ccr= ccr |
| 1489 | |
| 1490 | def __len__(self): |
| 1491 | return int(_clang_codeCompleteGetNumDiagnostics(self.ccr)) |
| 1492 | |
| 1493 | def __getitem__(self, key): |
| 1494 | return _clang_codeCompleteGetDiagnostic(self.ccr, key) |
| 1495 | |
| 1496 | return DiagnosticsItr(self) |
| 1497 | |
| 1498 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1499 | class Index(ClangObject): |
| 1500 | """ |
| 1501 | The Index type provides the primary interface to the Clang CIndex library, |
| 1502 | primarily by providing an interface for reading and parsing translation |
| 1503 | units. |
| 1504 | """ |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1505 | |
| 1506 | @staticmethod |
Daniel Dunbar | 2791dfc | 2010-01-30 23:58:39 +0000 | [diff] [blame] | 1507 | def create(excludeDecls=False): |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1508 | """ |
| 1509 | Create a new Index. |
| 1510 | Parameters: |
| 1511 | excludeDecls -- Exclude local declarations from translation units. |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1512 | """ |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1513 | return Index(Index_create(excludeDecls, 0)) |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1514 | |
| 1515 | def __del__(self): |
| 1516 | Index_dispose(self) |
| 1517 | |
| 1518 | def read(self, path): |
| 1519 | """Load the translation unit from the given AST file.""" |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1520 | ptr = TranslationUnit_read(self, path) |
Tobias Grosser | ba5d10b | 2011-10-31 02:06:50 +0000 | [diff] [blame] | 1521 | if ptr: |
| 1522 | return TranslationUnit(ptr) |
| 1523 | return None |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1524 | |
Tobias Grosser | 265e6b2 | 2011-02-05 17:54:00 +0000 | [diff] [blame] | 1525 | def parse(self, path, args = [], unsaved_files = [], options = 0): |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1526 | """ |
| 1527 | Load the translation unit from the given source code file by running |
| 1528 | clang and generating the AST before loading. Additional command line |
| 1529 | parameters can be passed to clang via the args parameter. |
Daniel Dunbar | 5b534f6 | 2010-01-25 00:44:11 +0000 | [diff] [blame] | 1530 | |
| 1531 | In-memory contents for files can be provided by passing a list of pairs |
| 1532 | to as unsaved_files, the first item should be the filenames to be mapped |
| 1533 | and the second should be the contents to be substituted for the |
| 1534 | file. The contents may be passed as strings or file objects. |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1535 | """ |
Daniel Dunbar | 061bae9 | 2010-01-25 09:16:55 +0000 | [diff] [blame] | 1536 | arg_array = 0 |
| 1537 | if len(args): |
| 1538 | arg_array = (c_char_p * len(args))(* args) |
| 1539 | unsaved_files_array = 0 |
| 1540 | if len(unsaved_files): |
| 1541 | unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))() |
| 1542 | for i,(name,value) in enumerate(unsaved_files): |
| 1543 | if not isinstance(value, str): |
| 1544 | # FIXME: It would be great to support an efficient version |
| 1545 | # of this, one day. |
| 1546 | value = value.read() |
| 1547 | print value |
| 1548 | if not isinstance(value, str): |
| 1549 | raise TypeError,'Unexpected unsaved file contents.' |
| 1550 | unsaved_files_array[i].name = name |
| 1551 | unsaved_files_array[i].contents = value |
| 1552 | unsaved_files_array[i].length = len(value) |
Tobias Grosser | 265e6b2 | 2011-02-05 17:54:00 +0000 | [diff] [blame] | 1553 | ptr = TranslationUnit_parse(self, path, arg_array, len(args), |
| 1554 | unsaved_files_array, len(unsaved_files), |
| 1555 | options) |
Tobias Grosser | ba5d10b | 2011-10-31 02:06:50 +0000 | [diff] [blame] | 1556 | if ptr: |
| 1557 | return TranslationUnit(ptr) |
| 1558 | return None |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1559 | |
| 1560 | |
| 1561 | class TranslationUnit(ClangObject): |
| 1562 | """ |
| 1563 | The TranslationUnit class represents a source code translation unit and |
| 1564 | provides read-only access to its top-level declarations. |
| 1565 | """ |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1566 | |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1567 | def __init__(self, ptr): |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1568 | ClangObject.__init__(self, ptr) |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1569 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1570 | def __del__(self): |
Daniel Dunbar | 99d593e | 2010-01-24 04:09:51 +0000 | [diff] [blame] | 1571 | TranslationUnit_dispose(self) |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1572 | |
Daniel Dunbar | 1b945a7 | 2010-01-24 04:09:43 +0000 | [diff] [blame] | 1573 | @property |
| 1574 | def cursor(self): |
| 1575 | """Retrieve the cursor that represents the given translation unit.""" |
| 1576 | return TranslationUnit_cursor(self) |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1577 | |
| 1578 | @property |
| 1579 | def spelling(self): |
Daniel Dunbar | 1b945a7 | 2010-01-24 04:09:43 +0000 | [diff] [blame] | 1580 | """Get the original translation unit source file name.""" |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1581 | return TranslationUnit_spelling(self) |
| 1582 | |
Daniel Dunbar | ef7f798 | 2010-02-13 18:33:18 +0000 | [diff] [blame] | 1583 | def get_includes(self): |
| 1584 | """ |
| 1585 | Return an iterable sequence of FileInclusion objects that describe the |
| 1586 | sequence of inclusions in a translation unit. The first object in |
| 1587 | this sequence is always the input file. Note that this method will not |
| 1588 | recursively iterate over header files included through precompiled |
| 1589 | headers. |
| 1590 | """ |
| 1591 | def visitor(fobj, lptr, depth, includes): |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 1592 | if depth > 0: |
| 1593 | loc = lptr.contents |
| 1594 | includes.append(FileInclusion(loc.file, File(fobj), loc, depth)) |
Daniel Dunbar | ef7f798 | 2010-02-13 18:33:18 +0000 | [diff] [blame] | 1595 | |
| 1596 | # Automatically adapt CIndex/ctype pointers to python objects |
| 1597 | includes = [] |
| 1598 | TranslationUnit_includes(self, |
| 1599 | TranslationUnit_includes_callback(visitor), |
| 1600 | includes) |
| 1601 | return iter(includes) |
| 1602 | |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1603 | @property |
| 1604 | def diagnostics(self): |
| 1605 | """ |
| 1606 | Return an iterable (and indexable) object containing the diagnostics. |
| 1607 | """ |
Benjamin Kramer | 1d02ccd | 2010-03-06 15:38:03 +0000 | [diff] [blame] | 1608 | class DiagIterator: |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1609 | def __init__(self, tu): |
| 1610 | self.tu = tu |
| 1611 | |
| 1612 | def __len__(self): |
| 1613 | return int(_clang_getNumDiagnostics(self.tu)) |
| 1614 | |
| 1615 | def __getitem__(self, key): |
Benjamin Kramer | 1d02ccd | 2010-03-06 15:38:03 +0000 | [diff] [blame] | 1616 | diag = _clang_getDiagnostic(self.tu, key) |
| 1617 | if not diag: |
| 1618 | raise IndexError |
| 1619 | return Diagnostic(diag) |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1620 | |
Benjamin Kramer | 1d02ccd | 2010-03-06 15:38:03 +0000 | [diff] [blame] | 1621 | return DiagIterator(self) |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1622 | |
Tobias Grosser | 265e6b2 | 2011-02-05 17:54:00 +0000 | [diff] [blame] | 1623 | def reparse(self, unsaved_files = [], options = 0): |
| 1624 | """ |
| 1625 | Reparse an already parsed translation unit. |
| 1626 | |
| 1627 | In-memory contents for files can be provided by passing a list of pairs |
| 1628 | as unsaved_files, the first items should be the filenames to be mapped |
| 1629 | and the second should be the contents to be substituted for the |
| 1630 | file. The contents may be passed as strings or file objects. |
| 1631 | """ |
| 1632 | unsaved_files_array = 0 |
| 1633 | if len(unsaved_files): |
| 1634 | unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))() |
| 1635 | for i,(name,value) in enumerate(unsaved_files): |
| 1636 | if not isinstance(value, str): |
| 1637 | # FIXME: It would be great to support an efficient version |
| 1638 | # of this, one day. |
| 1639 | value = value.read() |
| 1640 | print value |
| 1641 | if not isinstance(value, str): |
| 1642 | raise TypeError,'Unexpected unsaved file contents.' |
| 1643 | unsaved_files_array[i].name = name |
| 1644 | unsaved_files_array[i].contents = value |
| 1645 | unsaved_files_array[i].length = len(value) |
| 1646 | ptr = TranslationUnit_reparse(self, len(unsaved_files), |
| 1647 | unsaved_files_array, |
| 1648 | options) |
Tobias Grosser | 0a16680 | 2011-02-05 17:54:04 +0000 | [diff] [blame] | 1649 | def codeComplete(self, path, line, column, unsaved_files = [], options = 0): |
| 1650 | """ |
| 1651 | Code complete in this translation unit. |
| 1652 | |
| 1653 | In-memory contents for files can be provided by passing a list of pairs |
| 1654 | as unsaved_files, the first items should be the filenames to be mapped |
| 1655 | and the second should be the contents to be substituted for the |
| 1656 | file. The contents may be passed as strings or file objects. |
| 1657 | """ |
| 1658 | unsaved_files_array = 0 |
| 1659 | if len(unsaved_files): |
| 1660 | unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))() |
| 1661 | for i,(name,value) in enumerate(unsaved_files): |
| 1662 | if not isinstance(value, str): |
| 1663 | # FIXME: It would be great to support an efficient version |
| 1664 | # of this, one day. |
| 1665 | value = value.read() |
| 1666 | print value |
| 1667 | if not isinstance(value, str): |
| 1668 | raise TypeError,'Unexpected unsaved file contents.' |
| 1669 | unsaved_files_array[i].name = name |
| 1670 | unsaved_files_array[i].contents = value |
| 1671 | unsaved_files_array[i].length = len(value) |
| 1672 | ptr = TranslationUnit_codeComplete(self, path, |
| 1673 | line, column, |
| 1674 | unsaved_files_array, |
| 1675 | len(unsaved_files), |
| 1676 | options) |
Tobias Grosser | ba5d10b | 2011-10-31 02:06:50 +0000 | [diff] [blame] | 1677 | if ptr: |
| 1678 | return CodeCompletionResults(ptr) |
| 1679 | return None |
Tobias Grosser | 265e6b2 | 2011-02-05 17:54:00 +0000 | [diff] [blame] | 1680 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1681 | class File(ClangObject): |
| 1682 | """ |
Daniel Dunbar | 7b48b35 | 2010-01-24 04:09:34 +0000 | [diff] [blame] | 1683 | The File class represents a particular source file that is part of a |
| 1684 | translation unit. |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1685 | """ |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1686 | |
Tobias Grosser | a9ea5df | 2011-10-31 00:07:19 +0000 | [diff] [blame] | 1687 | @staticmethod |
| 1688 | def from_name(translation_unit, file_name): |
| 1689 | """Retrieve a file handle within the given translation unit.""" |
| 1690 | return File(File_getFile(translation_unit, file_name)) |
| 1691 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1692 | @property |
| 1693 | def name(self): |
Daniel Dunbar | 4efd632 | 2010-01-25 00:43:08 +0000 | [diff] [blame] | 1694 | """Return the complete file and path name of the file.""" |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 1695 | return _CXString_getCString(File_name(self)) |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1696 | |
| 1697 | @property |
| 1698 | def time(self): |
Daniel Dunbar | 4efd632 | 2010-01-25 00:43:08 +0000 | [diff] [blame] | 1699 | """Return the last modification time of the file.""" |
Daniel Dunbar | 7b48b35 | 2010-01-24 04:09:34 +0000 | [diff] [blame] | 1700 | return File_time(self) |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1701 | |
Tobias Grosser | a9ea5df | 2011-10-31 00:07:19 +0000 | [diff] [blame] | 1702 | def __str__(self): |
| 1703 | return self.name |
| 1704 | |
| 1705 | def __repr__(self): |
| 1706 | return "<File: %s>" % (self.name) |
| 1707 | |
Daniel Dunbar | ef7f798 | 2010-02-13 18:33:18 +0000 | [diff] [blame] | 1708 | class FileInclusion(object): |
| 1709 | """ |
| 1710 | The FileInclusion class represents the inclusion of one source file by |
| 1711 | another via a '#include' directive or as the input file for the translation |
| 1712 | unit. This class provides information about the included file, the including |
| 1713 | file, the location of the '#include' directive and the depth of the included |
| 1714 | file in the stack. Note that the input file has depth 0. |
| 1715 | """ |
| 1716 | |
| 1717 | def __init__(self, src, tgt, loc, depth): |
| 1718 | self.source = src |
| 1719 | self.include = tgt |
| 1720 | self.location = loc |
| 1721 | self.depth = depth |
| 1722 | |
| 1723 | @property |
| 1724 | def is_input_file(self): |
| 1725 | """True if the included file is the input file.""" |
| 1726 | return self.depth == 0 |
| 1727 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1728 | # Additional Functions and Types |
| 1729 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1730 | # String Functions |
Daniel Dunbar | a33dca4 | 2010-01-24 21:19:57 +0000 | [diff] [blame] | 1731 | _CXString_dispose = lib.clang_disposeString |
| 1732 | _CXString_dispose.argtypes = [_CXString] |
| 1733 | |
| 1734 | _CXString_getCString = lib.clang_getCString |
| 1735 | _CXString_getCString.argtypes = [_CXString] |
| 1736 | _CXString_getCString.restype = c_char_p |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1737 | |
| 1738 | # Source Location Functions |
| 1739 | SourceLocation_loc = lib.clang_getInstantiationLocation |
Daniel Dunbar | be0b555 | 2010-01-24 21:19:48 +0000 | [diff] [blame] | 1740 | SourceLocation_loc.argtypes = [SourceLocation, POINTER(c_object_p), |
Daniel Dunbar | 3239a67 | 2010-01-29 17:02:32 +0000 | [diff] [blame] | 1741 | POINTER(c_uint), POINTER(c_uint), |
| 1742 | POINTER(c_uint)] |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1743 | |
Tobias Grosser | 58ba8c9 | 2011-10-31 00:31:32 +0000 | [diff] [blame] | 1744 | SourceLocation_getLocation = lib.clang_getLocation |
| 1745 | SourceLocation_getLocation.argtypes = [TranslationUnit, File, c_uint, c_uint] |
| 1746 | SourceLocation_getLocation.restype = SourceLocation |
| 1747 | |
Tobias Grosser | 7485833 | 2012-02-05 11:40:59 +0000 | [diff] [blame] | 1748 | SourceLocation_equalLocations = lib.clang_equalLocations |
| 1749 | SourceLocation_equalLocations.argtypes = [SourceLocation, SourceLocation] |
| 1750 | SourceLocation_equalLocations.restype = bool |
| 1751 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1752 | # Source Range Functions |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1753 | SourceRange_getRange = lib.clang_getRange |
| 1754 | SourceRange_getRange.argtypes = [SourceLocation, SourceLocation] |
| 1755 | SourceRange_getRange.restype = SourceRange |
| 1756 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1757 | SourceRange_start = lib.clang_getRangeStart |
Daniel Dunbar | 7b48b35 | 2010-01-24 04:09:34 +0000 | [diff] [blame] | 1758 | SourceRange_start.argtypes = [SourceRange] |
| 1759 | SourceRange_start.restype = SourceLocation |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1760 | |
| 1761 | SourceRange_end = lib.clang_getRangeEnd |
Daniel Dunbar | 7b48b35 | 2010-01-24 04:09:34 +0000 | [diff] [blame] | 1762 | SourceRange_end.argtypes = [SourceRange] |
| 1763 | SourceRange_end.restype = SourceLocation |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1764 | |
Tobias Grosser | 7485833 | 2012-02-05 11:40:59 +0000 | [diff] [blame] | 1765 | SourceRange_equalRanges = lib.clang_equalRanges |
| 1766 | SourceRange_equalRanges.argtypes = [SourceRange, SourceRange] |
| 1767 | SourceRange_equalRanges.restype = bool |
| 1768 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 1769 | # CursorKind Functions |
| 1770 | CursorKind_is_decl = lib.clang_isDeclaration |
| 1771 | CursorKind_is_decl.argtypes = [CursorKind] |
| 1772 | CursorKind_is_decl.restype = bool |
| 1773 | |
| 1774 | CursorKind_is_ref = lib.clang_isReference |
| 1775 | CursorKind_is_ref.argtypes = [CursorKind] |
| 1776 | CursorKind_is_ref.restype = bool |
| 1777 | |
| 1778 | CursorKind_is_expr = lib.clang_isExpression |
| 1779 | CursorKind_is_expr.argtypes = [CursorKind] |
| 1780 | CursorKind_is_expr.restype = bool |
| 1781 | |
| 1782 | CursorKind_is_stmt = lib.clang_isStatement |
| 1783 | CursorKind_is_stmt.argtypes = [CursorKind] |
| 1784 | CursorKind_is_stmt.restype = bool |
| 1785 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 1786 | CursorKind_is_attribute = lib.clang_isAttribute |
| 1787 | CursorKind_is_attribute.argtypes = [CursorKind] |
| 1788 | CursorKind_is_attribute.restype = bool |
| 1789 | |
Daniel Dunbar | a6a6499 | 2010-01-24 21:20:39 +0000 | [diff] [blame] | 1790 | CursorKind_is_inv = lib.clang_isInvalid |
| 1791 | CursorKind_is_inv.argtypes = [CursorKind] |
| 1792 | CursorKind_is_inv.restype = bool |
| 1793 | |
Tobias Grosser | eb13634 | 2012-02-05 11:42:09 +0000 | [diff] [blame] | 1794 | CursorKind_is_translation_unit = lib.clang_isTranslationUnit |
| 1795 | CursorKind_is_translation_unit.argtypes = [CursorKind] |
| 1796 | CursorKind_is_translation_unit.restype = bool |
| 1797 | |
| 1798 | CursorKind_is_preprocessing = lib.clang_isPreprocessing |
| 1799 | CursorKind_is_preprocessing.argtypes = [CursorKind] |
| 1800 | CursorKind_is_preprocessing.restype = bool |
| 1801 | |
| 1802 | CursorKind_is_unexposed = lib.clang_isUnexposed |
| 1803 | CursorKind_is_unexposed.argtypes = [CursorKind] |
| 1804 | CursorKind_is_unexposed.restype = bool |
| 1805 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1806 | # Cursor Functions |
| 1807 | # TODO: Implement this function |
| 1808 | Cursor_get = lib.clang_getCursor |
Daniel Dunbar | de3b8e5 | 2010-01-24 04:10:22 +0000 | [diff] [blame] | 1809 | Cursor_get.argtypes = [TranslationUnit, SourceLocation] |
Daniel Dunbar | be0b555 | 2010-01-24 21:19:48 +0000 | [diff] [blame] | 1810 | Cursor_get.restype = Cursor |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1811 | |
| 1812 | Cursor_null = lib.clang_getNullCursor |
| 1813 | Cursor_null.restype = Cursor |
| 1814 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1815 | Cursor_usr = lib.clang_getCursorUSR |
Daniel Dunbar | 3d855f8 | 2010-01-24 21:20:13 +0000 | [diff] [blame] | 1816 | Cursor_usr.argtypes = [Cursor] |
| 1817 | Cursor_usr.restype = _CXString |
| 1818 | Cursor_usr.errcheck = _CXString.from_result |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1819 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1820 | Cursor_is_def = lib.clang_isCursorDefinition |
| 1821 | Cursor_is_def.argtypes = [Cursor] |
Daniel Dunbar | be0b555 | 2010-01-24 21:19:48 +0000 | [diff] [blame] | 1822 | Cursor_is_def.restype = bool |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1823 | |
| 1824 | Cursor_def = lib.clang_getCursorDefinition |
| 1825 | Cursor_def.argtypes = [Cursor] |
| 1826 | Cursor_def.restype = Cursor |
Daniel Dunbar | fb8ae17 | 2010-01-24 21:20:05 +0000 | [diff] [blame] | 1827 | Cursor_def.errcheck = Cursor.from_result |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1828 | |
| 1829 | Cursor_eq = lib.clang_equalCursors |
| 1830 | Cursor_eq.argtypes = [Cursor, Cursor] |
| 1831 | Cursor_eq.restype = c_uint |
| 1832 | |
Tobias Grosser | 64e7bdc | 2012-02-05 11:42:03 +0000 | [diff] [blame] | 1833 | Cursor_hash = lib.clang_hashCursor |
| 1834 | Cursor_hash.argtypes = [Cursor] |
| 1835 | Cursor_hash.restype = c_uint |
| 1836 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1837 | Cursor_spelling = lib.clang_getCursorSpelling |
| 1838 | Cursor_spelling.argtypes = [Cursor] |
Daniel Dunbar | a33dca4 | 2010-01-24 21:19:57 +0000 | [diff] [blame] | 1839 | Cursor_spelling.restype = _CXString |
| 1840 | Cursor_spelling.errcheck = _CXString.from_result |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1841 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 1842 | Cursor_displayname = lib.clang_getCursorDisplayName |
| 1843 | Cursor_displayname.argtypes = [Cursor] |
| 1844 | Cursor_displayname.restype = _CXString |
| 1845 | Cursor_displayname.errcheck = _CXString.from_result |
| 1846 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1847 | Cursor_loc = lib.clang_getCursorLocation |
| 1848 | Cursor_loc.argtypes = [Cursor] |
| 1849 | Cursor_loc.restype = SourceLocation |
| 1850 | |
| 1851 | Cursor_extent = lib.clang_getCursorExtent |
| 1852 | Cursor_extent.argtypes = [Cursor] |
| 1853 | Cursor_extent.restype = SourceRange |
| 1854 | |
| 1855 | Cursor_ref = lib.clang_getCursorReferenced |
| 1856 | Cursor_ref.argtypes = [Cursor] |
| 1857 | Cursor_ref.restype = Cursor |
Daniel Dunbar | fb8ae17 | 2010-01-24 21:20:05 +0000 | [diff] [blame] | 1858 | Cursor_ref.errcheck = Cursor.from_result |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1859 | |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 1860 | Cursor_type = lib.clang_getCursorType |
| 1861 | Cursor_type.argtypes = [Cursor] |
| 1862 | Cursor_type.restype = Type |
| 1863 | Cursor_type.errcheck = Type.from_result |
| 1864 | |
Tobias Grosser | 28d939f | 2012-02-05 11:42:20 +0000 | [diff] [blame] | 1865 | Cursor_underlying_type = lib.clang_getTypedefDeclUnderlyingType |
| 1866 | Cursor_underlying_type.argtypes = [Cursor] |
| 1867 | Cursor_underlying_type.restype = Type |
| 1868 | Cursor_underlying_type.errcheck = Type.from_result |
| 1869 | |
Tobias Grosser | eb9ff2e | 2012-02-05 11:42:25 +0000 | [diff] [blame] | 1870 | Cursor_enum_type = lib.clang_getEnumDeclIntegerType |
| 1871 | Cursor_enum_type.argtypes = [Cursor] |
| 1872 | Cursor_enum_type.restype = Type |
| 1873 | Cursor_enum_type.errcheck = Type.from_result |
| 1874 | |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1875 | Cursor_visit_callback = CFUNCTYPE(c_int, Cursor, Cursor, py_object) |
Daniel Dunbar | de3b8e5 | 2010-01-24 04:10:22 +0000 | [diff] [blame] | 1876 | Cursor_visit = lib.clang_visitChildren |
Daniel Dunbar | 532fc63 | 2010-01-30 23:59:02 +0000 | [diff] [blame] | 1877 | Cursor_visit.argtypes = [Cursor, Cursor_visit_callback, py_object] |
Daniel Dunbar | de3b8e5 | 2010-01-24 04:10:22 +0000 | [diff] [blame] | 1878 | Cursor_visit.restype = c_uint |
| 1879 | |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 1880 | # Type Functions |
| 1881 | Type_get_canonical = lib.clang_getCanonicalType |
| 1882 | Type_get_canonical.argtypes = [Type] |
| 1883 | Type_get_canonical.restype = Type |
| 1884 | Type_get_canonical.errcheck = Type.from_result |
| 1885 | |
| 1886 | Type_is_const_qualified = lib.clang_isConstQualifiedType |
| 1887 | Type_is_const_qualified.argtypes = [Type] |
| 1888 | Type_is_const_qualified.restype = bool |
| 1889 | |
| 1890 | Type_is_volatile_qualified = lib.clang_isVolatileQualifiedType |
| 1891 | Type_is_volatile_qualified.argtypes = [Type] |
| 1892 | Type_is_volatile_qualified.restype = bool |
| 1893 | |
| 1894 | Type_is_restrict_qualified = lib.clang_isRestrictQualifiedType |
| 1895 | Type_is_restrict_qualified.argtypes = [Type] |
| 1896 | Type_is_restrict_qualified.restype = bool |
| 1897 | |
Gregory Szorc | 96ad633 | 2012-02-05 19:42:06 +0000 | [diff] [blame] | 1898 | Type_is_pod = lib.clang_isPODType |
| 1899 | Type_is_pod.argtypes = [Type] |
| 1900 | Type_is_pod.restype = bool |
| 1901 | |
Gregory Szorc | 31cc38c | 2012-02-19 18:28:33 +0000 | [diff] [blame^] | 1902 | Type_is_variadic = lib.clang_isFunctionTypeVariadic |
| 1903 | Type_is_variadic.argtypes = [Type] |
| 1904 | Type_is_variadic.restype = bool |
| 1905 | |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 1906 | Type_get_pointee = lib.clang_getPointeeType |
| 1907 | Type_get_pointee.argtypes = [Type] |
| 1908 | Type_get_pointee.restype = Type |
| 1909 | Type_get_pointee.errcheck = Type.from_result |
| 1910 | |
| 1911 | Type_get_declaration = lib.clang_getTypeDeclaration |
| 1912 | Type_get_declaration.argtypes = [Type] |
| 1913 | Type_get_declaration.restype = Cursor |
| 1914 | Type_get_declaration.errcheck = Cursor.from_result |
| 1915 | |
| 1916 | Type_get_result = lib.clang_getResultType |
| 1917 | Type_get_result.argtypes = [Type] |
| 1918 | Type_get_result.restype = Type |
| 1919 | Type_get_result.errcheck = Type.from_result |
| 1920 | |
Gregory Szorc | 8605760 | 2012-02-17 07:44:46 +0000 | [diff] [blame] | 1921 | Type_get_element_type = lib.clang_getElementType |
| 1922 | Type_get_element_type.argtypes = [Type] |
| 1923 | Type_get_element_type.restype = Type |
| 1924 | Type_get_element_type.errcheck = Type.from_result |
| 1925 | |
Gregory Szorc | bf8ca00 | 2012-02-17 07:47:38 +0000 | [diff] [blame] | 1926 | Type_get_num_elements = lib.clang_getNumElements |
| 1927 | Type_get_num_elements.argtypes = [Type] |
| 1928 | Type_get_num_elements.restype = c_longlong |
| 1929 | |
Douglas Gregor | 13102ff | 2011-10-19 05:51:43 +0000 | [diff] [blame] | 1930 | Type_get_array_element = lib.clang_getArrayElementType |
| 1931 | Type_get_array_element.argtypes = [Type] |
| 1932 | Type_get_array_element.restype = Type |
| 1933 | Type_get_array_element.errcheck = Type.from_result |
| 1934 | |
| 1935 | Type_get_array_size = lib.clang_getArraySize |
| 1936 | Type_get_array_size.argtype = [Type] |
| 1937 | Type_get_array_size.restype = c_longlong |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 1938 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1939 | # Index Functions |
| 1940 | Index_create = lib.clang_createIndex |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1941 | Index_create.argtypes = [c_int, c_int] |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1942 | Index_create.restype = c_object_p |
| 1943 | |
| 1944 | Index_dispose = lib.clang_disposeIndex |
| 1945 | Index_dispose.argtypes = [Index] |
| 1946 | |
| 1947 | # Translation Unit Functions |
| 1948 | TranslationUnit_read = lib.clang_createTranslationUnit |
Benjamin Kramer | 3b0cf09 | 2010-03-06 14:53:07 +0000 | [diff] [blame] | 1949 | TranslationUnit_read.argtypes = [Index, c_char_p] |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1950 | TranslationUnit_read.restype = c_object_p |
| 1951 | |
Tobias Grosser | 265e6b2 | 2011-02-05 17:54:00 +0000 | [diff] [blame] | 1952 | TranslationUnit_parse = lib.clang_parseTranslationUnit |
| 1953 | TranslationUnit_parse.argtypes = [Index, c_char_p, c_void_p, |
| 1954 | c_int, c_void_p, c_int, c_int] |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1955 | TranslationUnit_parse.restype = c_object_p |
| 1956 | |
Tobias Grosser | 265e6b2 | 2011-02-05 17:54:00 +0000 | [diff] [blame] | 1957 | TranslationUnit_reparse = lib.clang_reparseTranslationUnit |
| 1958 | TranslationUnit_reparse.argtypes = [TranslationUnit, c_int, c_void_p, c_int] |
| 1959 | TranslationUnit_reparse.restype = c_int |
| 1960 | |
Tobias Grosser | 0a16680 | 2011-02-05 17:54:04 +0000 | [diff] [blame] | 1961 | TranslationUnit_codeComplete = lib.clang_codeCompleteAt |
| 1962 | TranslationUnit_codeComplete.argtypes = [TranslationUnit, c_char_p, c_int, |
| 1963 | c_int, c_void_p, c_int, c_int] |
| 1964 | TranslationUnit_codeComplete.restype = POINTER(CCRStructure) |
| 1965 | |
Daniel Dunbar | 1b945a7 | 2010-01-24 04:09:43 +0000 | [diff] [blame] | 1966 | TranslationUnit_cursor = lib.clang_getTranslationUnitCursor |
| 1967 | TranslationUnit_cursor.argtypes = [TranslationUnit] |
| 1968 | TranslationUnit_cursor.restype = Cursor |
Daniel Dunbar | fb8ae17 | 2010-01-24 21:20:05 +0000 | [diff] [blame] | 1969 | TranslationUnit_cursor.errcheck = Cursor.from_result |
Daniel Dunbar | 1b945a7 | 2010-01-24 04:09:43 +0000 | [diff] [blame] | 1970 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1971 | TranslationUnit_spelling = lib.clang_getTranslationUnitSpelling |
| 1972 | TranslationUnit_spelling.argtypes = [TranslationUnit] |
Daniel Dunbar | a33dca4 | 2010-01-24 21:19:57 +0000 | [diff] [blame] | 1973 | TranslationUnit_spelling.restype = _CXString |
| 1974 | TranslationUnit_spelling.errcheck = _CXString.from_result |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1975 | |
| 1976 | TranslationUnit_dispose = lib.clang_disposeTranslationUnit |
| 1977 | TranslationUnit_dispose.argtypes = [TranslationUnit] |
| 1978 | |
Daniel Dunbar | ef7f798 | 2010-02-13 18:33:18 +0000 | [diff] [blame] | 1979 | TranslationUnit_includes_callback = CFUNCTYPE(None, |
| 1980 | c_object_p, |
| 1981 | POINTER(SourceLocation), |
| 1982 | c_uint, py_object) |
| 1983 | TranslationUnit_includes = lib.clang_getInclusions |
| 1984 | TranslationUnit_includes.argtypes = [TranslationUnit, |
| 1985 | TranslationUnit_includes_callback, |
| 1986 | py_object] |
| 1987 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1988 | # File Functions |
Tobias Grosser | a9ea5df | 2011-10-31 00:07:19 +0000 | [diff] [blame] | 1989 | File_getFile = lib.clang_getFile |
| 1990 | File_getFile.argtypes = [TranslationUnit, c_char_p] |
| 1991 | File_getFile.restype = c_object_p |
| 1992 | |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1993 | File_name = lib.clang_getFileName |
| 1994 | File_name.argtypes = [File] |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 1995 | File_name.restype = _CXString |
Daniel Dunbar | 30c0f26 | 2010-01-24 02:02:07 +0000 | [diff] [blame] | 1996 | |
| 1997 | File_time = lib.clang_getFileTime |
| 1998 | File_time.argtypes = [File] |
| 1999 | File_time.restype = c_uint |
Daniel Dunbar | 4efd632 | 2010-01-25 00:43:08 +0000 | [diff] [blame] | 2000 | |
Tobias Grosser | 0a16680 | 2011-02-05 17:54:04 +0000 | [diff] [blame] | 2001 | # Code completion |
| 2002 | |
| 2003 | CodeCompletionResults_dispose = lib.clang_disposeCodeCompleteResults |
| 2004 | CodeCompletionResults_dispose.argtypes = [CodeCompletionResults] |
| 2005 | |
| 2006 | _clang_codeCompleteGetNumDiagnostics = lib.clang_codeCompleteGetNumDiagnostics |
| 2007 | _clang_codeCompleteGetNumDiagnostics.argtypes = [CodeCompletionResults] |
| 2008 | _clang_codeCompleteGetNumDiagnostics.restype = c_int |
| 2009 | |
| 2010 | _clang_codeCompleteGetDiagnostic = lib.clang_codeCompleteGetDiagnostic |
| 2011 | _clang_codeCompleteGetDiagnostic.argtypes = [CodeCompletionResults, c_int] |
| 2012 | _clang_codeCompleteGetDiagnostic.restype = Diagnostic |
| 2013 | |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 2014 | _clang_getCompletionChunkText = lib.clang_getCompletionChunkText |
| 2015 | _clang_getCompletionChunkText.argtypes = [c_void_p, c_int] |
| 2016 | _clang_getCompletionChunkText.restype = _CXString |
| 2017 | |
Tobias Grosser | a87dbcc | 2011-02-05 17:54:10 +0000 | [diff] [blame] | 2018 | _clang_getCompletionChunkKind = lib.clang_getCompletionChunkKind |
| 2019 | _clang_getCompletionChunkKind.argtypes = [c_void_p, c_int] |
| 2020 | _clang_getCompletionChunkKind.restype = c_int |
| 2021 | |
| 2022 | _clang_getCompletionChunkCompletionString = lib.clang_getCompletionChunkCompletionString |
| 2023 | _clang_getCompletionChunkCompletionString.argtypes = [c_void_p, c_int] |
| 2024 | _clang_getCompletionChunkCompletionString.restype = c_object_p |
| 2025 | |
Tobias Grosser | 6d2a40c | 2011-02-05 17:54:07 +0000 | [diff] [blame] | 2026 | _clang_getNumCompletionChunks = lib.clang_getNumCompletionChunks |
| 2027 | _clang_getNumCompletionChunks.argtypes = [c_void_p] |
| 2028 | _clang_getNumCompletionChunks.restype = c_int |
| 2029 | |
| 2030 | _clang_getCompletionAvailability = lib.clang_getCompletionAvailability |
| 2031 | _clang_getCompletionAvailability.argtypes = [c_void_p] |
| 2032 | _clang_getCompletionAvailability.restype = c_int |
| 2033 | |
| 2034 | _clang_getCompletionPriority = lib.clang_getCompletionPriority |
| 2035 | _clang_getCompletionPriority.argtypes = [c_void_p] |
| 2036 | _clang_getCompletionPriority.restype = c_int |
| 2037 | |
| 2038 | |
Daniel Dunbar | 4efd632 | 2010-01-25 00:43:08 +0000 | [diff] [blame] | 2039 | ### |
| 2040 | |
Argyrios Kyrtzidis | d7933e6 | 2011-08-17 00:43:03 +0000 | [diff] [blame] | 2041 | __all__ = ['Index', 'TranslationUnit', 'Cursor', 'CursorKind', 'Type', 'TypeKind', |
Tobias Grosser | 0a16680 | 2011-02-05 17:54:04 +0000 | [diff] [blame] | 2042 | 'Diagnostic', 'FixIt', 'CodeCompletionResults', 'SourceRange', |
| 2043 | 'SourceLocation', 'File'] |