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