blob: 6f0d25f1a8f595d8ec03a4cd9ba48349fafdb80f [file] [log] [blame]
Daniel Dunbar4efd6322010-01-25 00:43:08 +00001#===- 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
10r"""
11Clang Indexing Library Bindings
12===============================
13
14This module provides an interface to the Clang indexing library. It is a
15low-level interface to the indexing library which attempts to match the Clang
16API directly while also being "pythonic". Notable differences from the C API
17are:
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
25The 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
44Most object information is exposed using properties, when the underlying API
45call is efficient.
46"""
47
48# TODO
49# ====
50#
Daniel Dunbar532fc632010-01-30 23:59:02 +000051# 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 Dunbar4efd6322010-01-25 00:43:08 +000055# 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 Dunbar30c0f262010-01-24 02:02:07 +000064
65from ctypes import *
Gregory Szorc826fce52012-02-20 17:45:30 +000066import collections
Daniel Dunbar30c0f262010-01-24 02:02:07 +000067
68def 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 Dunbarf51f20f2010-04-30 21:51:10 +000075 return cdll.LoadLibrary('libclang.dylib')
Daniel Dunbar30c0f262010-01-24 02:02:07 +000076 elif name == 'Windows':
Daniel Dunbarf51f20f2010-04-30 21:51:10 +000077 return cdll.LoadLibrary('libclang.dll')
Daniel Dunbar30c0f262010-01-24 02:02:07 +000078 else:
Daniel Dunbarf51f20f2010-04-30 21:51:10 +000079 return cdll.LoadLibrary('libclang.so')
Daniel Dunbar12bf15c2010-01-24 21:20:29 +000080
Daniel Dunbar30c0f262010-01-24 02:02:07 +000081# 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**.
85c_object_p = POINTER(c_void_p)
86
87lib = get_cindex_library()
88
Daniel Dunbar30c0f262010-01-24 02:02:07 +000089### Structures and Utility Classes ###
90
Daniel Dunbara33dca42010-01-24 21:19:57 +000091class _CXString(Structure):
92 """Helper for transforming CXString results."""
Daniel Dunbar30c0f262010-01-24 02:02:07 +000093
Daniel Dunbar30c0f262010-01-24 02:02:07 +000094 _fields_ = [("spelling", c_char_p), ("free", c_int)]
95
96 def __del__(self):
Daniel Dunbara33dca42010-01-24 21:19:57 +000097 _CXString_dispose(self)
Daniel Dunbar30c0f262010-01-24 02:02:07 +000098
Daniel Dunbara33dca42010-01-24 21:19:57 +000099 @staticmethod
100 def from_result(res, fn, args):
101 assert isinstance(res, _CXString)
102 return _CXString_getCString(res)
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000103
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000104class SourceLocation(Structure):
105 """
Daniel Dunbar149f38a2010-01-24 04:09:58 +0000106 A SourceLocation represents a particular location within a source file.
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000107 """
Daniel Dunbare32af422010-01-30 23:58:50 +0000108 _fields_ = [("ptr_data", c_void_p * 2), ("int_data", c_uint)]
Daniel Dunbarf8690832010-01-24 21:20:21 +0000109 _data = None
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000110
Daniel Dunbarf8690832010-01-24 21:20:21 +0000111 def _get_instantiation(self):
112 if self._data is None:
Daniel Dunbar3239a672010-01-29 17:02:32 +0000113 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 Grosser81982882011-10-31 00:49:07 +0000115 if f:
116 f = File(f)
117 else:
118 f = None
Argyrios Kyrtzidis6b046232011-08-17 17:20:24 +0000119 self._data = (f, int(l.value), int(c.value), int(o.value))
Daniel Dunbarf8690832010-01-24 21:20:21 +0000120 return self._data
121
Tobias Grosser58ba8c92011-10-31 00:31:32 +0000122 @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 Dunbarf8690832010-01-24 21:20:21 +0000130 @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 Dunbar30c0f262010-01-24 02:02:07 +0000144
Daniel Dunbar3239a672010-01-29 17:02:32 +0000145 @property
146 def offset(self):
147 """Get the file offset represented by this source location."""
148 return self._get_instantiation()[3]
149
Tobias Grosser74858332012-02-05 11:40:59 +0000150 def __eq__(self, other):
151 return SourceLocation_equalLocations(self, other)
152
153 def __ne__(self, other):
154 return not self.__eq__(other)
155
Daniel Dunbar7b48b352010-01-24 04:09:34 +0000156 def __repr__(self):
Tobias Grosser81982882011-10-31 00:49:07 +0000157 if self.file:
158 filename = self.file.name
159 else:
160 filename = None
Daniel Dunbar7b48b352010-01-24 04:09:34 +0000161 return "<SourceLocation file %r, line %r, column %r>" % (
Tobias Grosser81982882011-10-31 00:49:07 +0000162 filename, self.line, self.column)
Daniel Dunbar7b48b352010-01-24 04:09:34 +0000163
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000164class SourceRange(Structure):
165 """
166 A SourceRange describes a range of source locations within the source
167 code.
168 """
169 _fields_ = [
Daniel Dunbare32af422010-01-30 23:58:50 +0000170 ("ptr_data", c_void_p * 2),
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000171 ("begin_int_data", c_uint),
172 ("end_int_data", c_uint)]
173
Daniel Dunbar532fc632010-01-30 23:59:02 +0000174 # 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 Dunbar7b48b352010-01-24 04:09:34 +0000180 @property
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000181 def start(self):
182 """
183 Return a SourceLocation representing the first character within a
184 source range.
185 """
Daniel Dunbarf8690832010-01-24 21:20:21 +0000186 return SourceRange_start(self)
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000187
Daniel Dunbar7b48b352010-01-24 04:09:34 +0000188 @property
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000189 def end(self):
190 """
191 Return a SourceLocation representing the last character within a
192 source range.
193 """
Daniel Dunbarf8690832010-01-24 21:20:21 +0000194 return SourceRange_end(self)
195
Tobias Grosser74858332012-02-05 11:40:59 +0000196 def __eq__(self, other):
197 return SourceRange_equalRanges(self, other)
198
199 def __ne__(self, other):
200 return not self.__eq__(other)
201
Daniel Dunbarf8690832010-01-24 21:20:21 +0000202 def __repr__(self):
203 return "<SourceRange start %r, end %r>" % (self.start, self.end)
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000204
Daniel Dunbar532fc632010-01-30 23:59:02 +0000205class 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 Kramer3b0cf092010-03-06 14:53:07 +0000218 def __init__(self, ptr):
219 self.ptr = ptr
220
221 def __del__(self):
Tobias Grosserff090ca2011-02-05 17:53:48 +0000222 _clang_disposeDiagnostic(self)
Benjamin Kramer3b0cf092010-03-06 14:53:07 +0000223
224 @property
225 def severity(self):
Tobias Grosserff090ca2011-02-05 17:53:48 +0000226 return _clang_getDiagnosticSeverity(self)
Benjamin Kramer3b0cf092010-03-06 14:53:07 +0000227
228 @property
229 def location(self):
Tobias Grosserff090ca2011-02-05 17:53:48 +0000230 return _clang_getDiagnosticLocation(self)
Benjamin Kramer3b0cf092010-03-06 14:53:07 +0000231
232 @property
233 def spelling(self):
Tobias Grosserff090ca2011-02-05 17:53:48 +0000234 return _clang_getDiagnosticSpelling(self)
Benjamin Kramer3b0cf092010-03-06 14:53:07 +0000235
236 @property
237 def ranges(self):
Benjamin Kramer1d02ccd2010-03-06 15:38:03 +0000238 class RangeIterator:
Benjamin Kramer3b0cf092010-03-06 14:53:07 +0000239 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 Grosserba5d10b2011-10-31 02:06:50 +0000246 if (key >= len(self)):
247 raise IndexError
Benjamin Kramer3b0cf092010-03-06 14:53:07 +0000248 return _clang_getDiagnosticRange(self.diag, key)
249
Tobias Grosserff090ca2011-02-05 17:53:48 +0000250 return RangeIterator(self)
Benjamin Kramer3b0cf092010-03-06 14:53:07 +0000251
252 @property
253 def fixits(self):
Benjamin Kramer1d02ccd2010-03-06 15:38:03 +0000254 class FixItIterator:
Benjamin Kramer3b0cf092010-03-06 14:53:07 +0000255 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 Kramer1d02ccd2010-03-06 15:38:03 +0000264 if len(value) == 0:
265 raise IndexError
Benjamin Kramer3b0cf092010-03-06 14:53:07 +0000266
267 return FixIt(range, value)
268
Tobias Grosserff090ca2011-02-05 17:53:48 +0000269 return FixItIterator(self)
Daniel Dunbar532fc632010-01-30 23:59:02 +0000270
Tobias Grosserea403822012-02-05 11:41:58 +0000271 @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 Dunbar532fc632010-01-30 23:59:02 +0000294 def __repr__(self):
295 return "<Diagnostic severity %r, location %r, spelling %r>" % (
296 self.severity, self.location, self.spelling)
297
Tobias Grosserff090ca2011-02-05 17:53:48 +0000298 def from_param(self):
299 return self.ptr
300
Daniel Dunbar532fc632010-01-30 23:59:02 +0000301class 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 Dunbar12bf15c2010-01-24 21:20:29 +0000315### Cursor Kinds ###
316
317class 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 Dunbara6a64992010-01-24 21:20:39 +0000354 @staticmethod
355 def get_all_kinds():
Daniel Dunbar4efd6322010-01-25 00:43:08 +0000356 """Return all CursorKind enumeration instances."""
Daniel Dunbara6a64992010-01-24 21:20:39 +0000357 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 Gregor8be80e12011-07-06 03:00:34 +0000375 def is_attribute(self):
376 """Test if this is an attribute kind."""
377 return CursorKind_is_attribute(self)
378
Daniel Dunbara6a64992010-01-24 21:20:39 +0000379 def is_invalid(self):
380 """Test if this is an invalid kind."""
381 return CursorKind_is_inv(self)
382
Tobias Grossereb136342012-02-05 11:42:09 +0000383 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 Dunbar12bf15c2010-01-24 21:20:29 +0000395 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 Dunbara6a64992010-01-24 21:20:39 +0000403###
404# Declaration Kinds
405
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000406# 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.
412CursorKind.UNEXPOSED_DECL = CursorKind(1)
413
414# A C or C++ struct.
415CursorKind.STRUCT_DECL = CursorKind(2)
416
417# A C or C++ union.
418CursorKind.UNION_DECL = CursorKind(3)
419
420# A C++ class.
421CursorKind.CLASS_DECL = CursorKind(4)
422
423# An enumeration.
424CursorKind.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.
428CursorKind.FIELD_DECL = CursorKind(6)
429
430# An enumerator constant.
431CursorKind.ENUM_CONSTANT_DECL = CursorKind(7)
432
433# A function.
434CursorKind.FUNCTION_DECL = CursorKind(8)
435
436# A variable.
437CursorKind.VAR_DECL = CursorKind(9)
438
439# A function or method parameter.
440CursorKind.PARM_DECL = CursorKind(10)
441
442# An Objective-C @interface.
443CursorKind.OBJC_INTERFACE_DECL = CursorKind(11)
444
445# An Objective-C @interface for a category.
446CursorKind.OBJC_CATEGORY_DECL = CursorKind(12)
447
448# An Objective-C @protocol declaration.
449CursorKind.OBJC_PROTOCOL_DECL = CursorKind(13)
450
451# An Objective-C @property declaration.
452CursorKind.OBJC_PROPERTY_DECL = CursorKind(14)
453
454# An Objective-C instance variable.
455CursorKind.OBJC_IVAR_DECL = CursorKind(15)
456
457# An Objective-C instance method.
458CursorKind.OBJC_INSTANCE_METHOD_DECL = CursorKind(16)
459
460# An Objective-C class method.
461CursorKind.OBJC_CLASS_METHOD_DECL = CursorKind(17)
462
463# An Objective-C @implementation.
464CursorKind.OBJC_IMPLEMENTATION_DECL = CursorKind(18)
465
466# An Objective-C @implementation for a category.
467CursorKind.OBJC_CATEGORY_IMPL_DECL = CursorKind(19)
468
Daniel Dunbara6a64992010-01-24 21:20:39 +0000469# A typedef.
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000470CursorKind.TYPEDEF_DECL = CursorKind(20)
471
Tobias Grosser4ed73ce2011-02-05 17:53:47 +0000472# A C++ class method.
473CursorKind.CXX_METHOD = CursorKind(21)
474
475# A C++ namespace.
476CursorKind.NAMESPACE = CursorKind(22)
477
478# A linkage specification, e.g. 'extern "C"'.
479CursorKind.LINKAGE_SPEC = CursorKind(23)
480
481# A C++ constructor.
482CursorKind.CONSTRUCTOR = CursorKind(24)
483
484# A C++ destructor.
485CursorKind.DESTRUCTOR = CursorKind(25)
486
487# A C++ conversion function.
488CursorKind.CONVERSION_FUNCTION = CursorKind(26)
489
490# A C++ template type parameter
491CursorKind.TEMPLATE_TYPE_PARAMETER = CursorKind(27)
492
493# A C++ non-type template paramater.
494CursorKind.TEMPLATE_NON_TYPE_PARAMETER = CursorKind(28)
495
496# A C++ template template parameter.
497CursorKind.TEMPLATE_TEMPLATE_PARAMTER = CursorKind(29)
498
499# A C++ function template.
500CursorKind.FUNCTION_TEMPLATE = CursorKind(30)
501
502# A C++ class template.
503CursorKind.CLASS_TEMPLATE = CursorKind(31)
504
505# A C++ class template partial specialization.
506CursorKind.CLASS_TEMPLATE_PARTIAL_SPECIALIZATION = CursorKind(32)
507
508# A C++ namespace alias declaration.
509CursorKind.NAMESPACE_ALIAS = CursorKind(33)
510
511# A C++ using directive
512CursorKind.USING_DIRECTIVE = CursorKind(34)
513
514# A C++ using declaration
515CursorKind.USING_DECLARATION = CursorKind(35)
516
Douglas Gregor42b29842011-10-05 19:00:14 +0000517# A Type alias decl.
518CursorKind.TYPE_ALIAS_DECL = CursorKind(36)
519
520# A Objective-C synthesize decl
521CursorKind.OBJC_SYNTHESIZE_DECL = CursorKind(37)
522
523# A Objective-C dynamic decl
524CursorKind.OBJC_DYNAMIC_DECL = CursorKind(38)
525
526# A C++ access specifier decl.
527CursorKind.CXX_ACCESS_SPEC_DECL = CursorKind(39)
528
529
Daniel Dunbara6a64992010-01-24 21:20:39 +0000530###
531# Reference Kinds
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000532
533CursorKind.OBJC_SUPER_CLASS_REF = CursorKind(40)
534CursorKind.OBJC_PROTOCOL_REF = CursorKind(41)
535CursorKind.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.
547CursorKind.TYPE_REF = CursorKind(43)
Tobias Grosser4ed73ce2011-02-05 17:53:47 +0000548CursorKind.CXX_BASE_SPECIFIER = CursorKind(44)
549
550# A reference to a class template, function template, template
551# template parameter, or class template partial specialization.
552CursorKind.TEMPLATE_REF = CursorKind(45)
553
554# A reference to a namespace or namepsace alias.
555CursorKind.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.
559CursorKind.MEMBER_REF = CursorKind(47)
560
561# A reference to a labeled statement.
562CursorKind.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.
566CursorKind.OVERLOADED_DECL_REF = CursorKind(49)
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000567
Daniel Dunbara6a64992010-01-24 21:20:39 +0000568###
569# Invalid/Error Kinds
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000570
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000571CursorKind.INVALID_FILE = CursorKind(70)
572CursorKind.NO_DECL_FOUND = CursorKind(71)
573CursorKind.NOT_IMPLEMENTED = CursorKind(72)
Tobias Grosser4ed73ce2011-02-05 17:53:47 +0000574CursorKind.INVALID_CODE = CursorKind(73)
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000575
Daniel Dunbara6a64992010-01-24 21:20:39 +0000576###
577# Expression Kinds
578
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000579# 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.
584CursorKind.UNEXPOSED_EXPR = CursorKind(100)
585
586# An expression that refers to some value declaration, such as a function,
587# varible, or enumerator.
588CursorKind.DECL_REF_EXPR = CursorKind(101)
589
590# An expression that refers to a member of a struct, union, class, Objective-C
591# class, etc.
592CursorKind.MEMBER_REF_EXPR = CursorKind(102)
593
594# An expression that calls a function.
595CursorKind.CALL_EXPR = CursorKind(103)
596
597# An expression that sends a message to an Objective-C object or class.
598CursorKind.OBJC_MESSAGE_EXPR = CursorKind(104)
599
Tobias Grosser4ed73ce2011-02-05 17:53:47 +0000600# An expression that represents a block literal.
601CursorKind.BLOCK_EXPR = CursorKind(105)
602
Douglas Gregor42b29842011-10-05 19:00:14 +0000603# An integer literal.
604CursorKind.INTEGER_LITERAL = CursorKind(106)
605
606# A floating point number literal.
607CursorKind.FLOATING_LITERAL = CursorKind(107)
608
609# An imaginary number literal.
610CursorKind.IMAGINARY_LITERAL = CursorKind(108)
611
612# A string literal.
613CursorKind.STRING_LITERAL = CursorKind(109)
614
615# A character literal.
616CursorKind.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.
621CursorKind.PAREN_EXPR = CursorKind(111)
622
623# This represents the unary-expression's (except sizeof and
624# alignof).
625CursorKind.UNARY_OPERATOR = CursorKind(112)
626
627# [C99 6.5.2.1] Array Subscripting.
628CursorKind.ARRAY_SUBSCRIPT_EXPR = CursorKind(113)
629
630# A builtin binary operation expression such as "x + y" or
631# "x <= y".
632CursorKind.BINARY_OPERATOR = CursorKind(114)
633
634# Compound assignment such as "+=".
635CursorKind.COMPOUND_ASSIGNMENT_OPERATOR = CursorKind(115)
636
637# The ?: ternary operator.
638CursorKind.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.
644CursorKind.CSTYLE_CAST_EXPR = CursorKind(117)
645
646# [C99 6.5.2.5]
647CursorKind.COMPOUND_LITERAL_EXPR = CursorKind(118)
648
649# Describes an C or C++ initializer list.
650CursorKind.INIT_LIST_EXPR = CursorKind(119)
651
652# The GNU address of label extension, representing &&label.
653CursorKind.ADDR_LABEL_EXPR = CursorKind(120)
654
655# This is the GNU Statement Expression extension: ({int X=4; X;})
656CursorKind.StmtExpr = CursorKind(121)
657
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000658# Represents a C11 generic selection.
Douglas Gregor42b29842011-10-05 19:00:14 +0000659CursorKind.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).
668CursorKind.GNU_NULL_EXPR = CursorKind(123)
669
670# C++'s static_cast<> expression.
671CursorKind.CXX_STATIC_CAST_EXPR = CursorKind(124)
672
673# C++'s dynamic_cast<> expression.
674CursorKind.CXX_DYNAMIC_CAST_EXPR = CursorKind(125)
675
676# C++'s reinterpret_cast<> expression.
677CursorKind.CXX_REINTERPRET_CAST_EXPR = CursorKind(126)
678
679# C++'s const_cast<> expression.
680CursorKind.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
689CursorKind.CXX_FUNCTIONAL_CAST_EXPR = CursorKind(128)
690
691# A C++ typeid expression (C++ [expr.typeid]).
692CursorKind.CXX_TYPEID_EXPR = CursorKind(129)
693
694# [C++ 2.13.5] C++ Boolean Literal.
695CursorKind.CXX_BOOL_LITERAL_EXPR = CursorKind(130)
696
697# [C++0x 2.14.7] C++ Pointer Literal.
698CursorKind.CXX_NULL_PTR_LITERAL_EXPR = CursorKind(131)
699
700# Represents the "this" expression in C++
701CursorKind.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.
707CursorKind.CXX_THROW_EXPR = CursorKind(133)
708
709# A new expression for memory allocation and constructor calls, e.g:
710# "new CXXNewExpr(foo)".
711CursorKind.CXX_NEW_EXPR = CursorKind(134)
712
713# A delete expression for memory deallocation and destructor calls,
714# e.g. "delete[] pArray".
715CursorKind.CXX_DELETE_EXPR = CursorKind(135)
716
717# Represents a unary expression.
718CursorKind.CXX_UNARY_EXPR = CursorKind(136)
719
720# ObjCStringLiteral, used for Objective-C string literals i.e. "foo".
721CursorKind.OBJC_STRING_LITERAL = CursorKind(137)
722
723# ObjCEncodeExpr, used for in Objective-C.
724CursorKind.OBJC_ENCODE_EXPR = CursorKind(138)
725
726# ObjCSelectorExpr used for in Objective-C.
727CursorKind.OBJC_SELECTOR_EXPR = CursorKind(139)
728
729# Objective-C's protocol expression.
730CursorKind.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
738CursorKind.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:
745CursorKind.PACK_EXPANSION_EXPR = CursorKind(142)
746
747# Represents an expression that computes the length of a parameter
748# pack.
749CursorKind.SIZE_OF_PACK_EXPR = CursorKind(143)
750
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000751# 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.
756CursorKind.UNEXPOSED_STMT = CursorKind(200)
757
Tobias Grosser4ed73ce2011-02-05 17:53:47 +0000758# A labelled statement in a function.
759CursorKind.LABEL_STMT = CursorKind(201)
760
Douglas Gregor42b29842011-10-05 19:00:14 +0000761# A compound statement
762CursorKind.COMPOUND_STMT = CursorKind(202)
763
764# A case statement.
765CursorKind.CASE_STMT = CursorKind(203)
766
767# A default statement.
768CursorKind.DEFAULT_STMT = CursorKind(204)
769
770# An if statement.
771CursorKind.IF_STMT = CursorKind(205)
772
773# A switch statement.
774CursorKind.SWITCH_STMT = CursorKind(206)
775
776# A while statement.
777CursorKind.WHILE_STMT = CursorKind(207)
778
779# A do statement.
780CursorKind.DO_STMT = CursorKind(208)
781
782# A for statement.
783CursorKind.FOR_STMT = CursorKind(209)
784
785# A goto statement.
786CursorKind.GOTO_STMT = CursorKind(210)
787
788# An indirect goto statement.
789CursorKind.INDIRECT_GOTO_STMT = CursorKind(211)
790
791# A continue statement.
792CursorKind.CONTINUE_STMT = CursorKind(212)
793
794# A break statement.
795CursorKind.BREAK_STMT = CursorKind(213)
796
797# A return statement.
798CursorKind.RETURN_STMT = CursorKind(214)
799
800# A GNU-style inline assembler statement.
801CursorKind.ASM_STMT = CursorKind(215)
802
803# Objective-C's overall @try-@catch-@finally statement.
804CursorKind.OBJC_AT_TRY_STMT = CursorKind(216)
805
806# Objective-C's @catch statement.
807CursorKind.OBJC_AT_CATCH_STMT = CursorKind(217)
808
809# Objective-C's @finally statement.
810CursorKind.OBJC_AT_FINALLY_STMT = CursorKind(218)
811
812# Objective-C's @throw statement.
813CursorKind.OBJC_AT_THROW_STMT = CursorKind(219)
814
815# Objective-C's @synchronized statement.
816CursorKind.OBJC_AT_SYNCHRONIZED_STMT = CursorKind(220)
817
818# Objective-C's autorealease pool statement.
819CursorKind.OBJC_AUTORELEASE_POOL_STMT = CursorKind(221)
820
821# Objective-C's for collection statement.
822CursorKind.OBJC_FOR_COLLECTION_STMT = CursorKind(222)
823
824# C++'s catch statement.
825CursorKind.CXX_CATCH_STMT = CursorKind(223)
826
827# C++'s try statement.
828CursorKind.CXX_TRY_STMT = CursorKind(224)
829
830# C++'s for (* : *) statement.
831CursorKind.CXX_FOR_RANGE_STMT = CursorKind(225)
832
833# Windows Structured Exception Handling's try statement.
834CursorKind.SEH_TRY_STMT = CursorKind(226)
835
836# Windows Structured Exception Handling's except statement.
837CursorKind.SEH_EXCEPT_STMT = CursorKind(227)
838
839# Windows Structured Exception Handling's finally statement.
840CursorKind.SEH_FINALLY_STMT = CursorKind(228)
841
842# The null statement.
843CursorKind.NULL_STMT = CursorKind(230)
844
845# Adaptor class for mixing declarations with statements and expressions.
846CursorKind.DECL_STMT = CursorKind(231)
Tobias Grosser4ed73ce2011-02-05 17:53:47 +0000847
Daniel Dunbara6a64992010-01-24 21:20:39 +0000848###
849# Other Kinds
850
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000851# 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.
855CursorKind.TRANSLATION_UNIT = CursorKind(300)
856
Tobias Grosser4ed73ce2011-02-05 17:53:47 +0000857###
858# Attributes
859
860# An attribute whoe specific kind is note exposed via this interface
861CursorKind.UNEXPOSED_ATTR = CursorKind(400)
862
863CursorKind.IB_ACTION_ATTR = CursorKind(401)
864CursorKind.IB_OUTLET_ATTR = CursorKind(402)
865CursorKind.IB_OUTLET_COLLECTION_ATTR = CursorKind(403)
866
Rafael Espindolabf3cc732011-12-30 15:27:22 +0000867CursorKind.CXX_FINAL_ATTR = CursorKind(404)
868CursorKind.CXX_OVERRIDE_ATTR = CursorKind(405)
869CursorKind.ANNOTATE_ATTR = CursorKind(406)
870CursorKind.ASM_LABEL_ATTR = CursorKind(407)
871
Tobias Grosser4ed73ce2011-02-05 17:53:47 +0000872###
873# Preprocessing
874CursorKind.PREPROCESSING_DIRECTIVE = CursorKind(500)
875CursorKind.MACRO_DEFINITION = CursorKind(501)
876CursorKind.MACRO_INSTANTIATION = CursorKind(502)
877CursorKind.INCLUSION_DIRECTIVE = CursorKind(503)
878
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000879### Cursors ###
880
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000881class 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 Gregor2abfec32011-10-19 05:47:46 +0000886 _fields_ = [("_kind_id", c_int), ("xdata", c_int), ("data", c_void_p * 3)]
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000887
Tobias Grosser58ba8c92011-10-31 00:31:32 +0000888 @staticmethod
889 def from_location(tu, location):
890 return Cursor_get(tu, location)
891
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000892 def __eq__(self, other):
893 return Cursor_eq(self, other)
894
895 def __ne__(self, other):
Gregory Szorc9d008fd2012-03-05 00:42:15 +0000896 return not self.__eq__(other)
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000897
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000898 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 Dunbar30c0f262010-01-24 02:02:07 +0000905 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 Dunbar3d855f82010-01-24 21:20:13 +0000915 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 Dunbar30c0f262010-01-24 02:02:07 +0000926 @property
Daniel Dunbar12bf15c2010-01-24 21:20:29 +0000927 def kind(self):
928 """Return the kind of this cursor."""
929 return CursorKind.from_id(self._kind_id)
930
931 @property
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000932 def spelling(self):
933 """Return the spelling of the entity pointed at by the cursor."""
Daniel Dunbara6a64992010-01-24 21:20:39 +0000934 if not self.kind.is_declaration():
Daniel Dunbar3d855f82010-01-24 21:20:13 +0000935 # FIXME: clang_getCursorSpelling should be fixed to not assert on
936 # this, for consistency with clang_getCursorUSR.
937 return None
Douglas Gregor42b29842011-10-05 19:00:14 +0000938 if not hasattr(self, '_spelling'):
939 self._spelling = Cursor_spelling(self)
940 return self._spelling
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000941
942 @property
Douglas Gregorb60a2be2011-08-30 00:15:34 +0000943 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 Gregor42b29842011-10-05 19:00:14 +0000951 if not hasattr(self, '_displayname'):
952 self._displayname = Cursor_displayname(self)
953 return self._displayname
Douglas Gregorb60a2be2011-08-30 00:15:34 +0000954
955 @property
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000956 def location(self):
957 """
958 Return the source location (the starting character) of the entity
959 pointed at by the cursor.
960 """
Douglas Gregor42b29842011-10-05 19:00:14 +0000961 if not hasattr(self, '_loc'):
962 self._loc = Cursor_loc(self)
963 return self._loc
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000964
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 Gregor42b29842011-10-05 19:00:14 +0000971 if not hasattr(self, '_extent'):
972 self._extent = Cursor_extent(self)
973 return self._extent
Daniel Dunbar30c0f262010-01-24 02:02:07 +0000974
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +0000975 @property
976 def type(self):
977 """
Tobias Grosser2d106802012-02-05 12:15:56 +0000978 Retrieve the Type (if any) of the entity pointed at by the cursor.
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +0000979 """
Douglas Gregor42b29842011-10-05 19:00:14 +0000980 if not hasattr(self, '_type'):
981 self._type = Cursor_type(self)
982 return self._type
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +0000983
Tobias Grosser64e7bdc2012-02-05 11:42:03 +0000984 @property
Tobias Grosser28d939f2012-02-05 11:42:20 +0000985 def underlying_typedef_type(self):
986 """Return the underlying type of a typedef declaration.
987
Tobias Grosser2d106802012-02-05 12:15:56 +0000988 Returns a Type for the typedef this cursor is a declaration for. If
Tobias Grosser28d939f2012-02-05 11:42:20 +0000989 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 Grossereb9ff2e2012-02-05 11:42:25 +0000998 def enum_type(self):
999 """Return the integer type of an enum declaration.
1000
Tobias Grosser2d106802012-02-05 12:15:56 +00001001 Returns a Type corresponding to an integer. If the cursor is not for an
Tobias Grossereb9ff2e2012-02-05 11:42:25 +00001002 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
Gregory Szorc5cc67872012-03-10 22:23:27 +00001011 def objc_type_encoding(self):
1012 """Return the Objective-C type encoding as a str."""
1013 if not hasattr(self, '_objc_type_encoding'):
1014 self._objc_type_encoding = Cursor_objc_type_encoding(self)
1015
1016 return self._objc_type_encoding
1017
1018 @property
Tobias Grosser64e7bdc2012-02-05 11:42:03 +00001019 def hash(self):
1020 """Returns a hash of the cursor as an int."""
1021 if not hasattr(self, '_hash'):
1022 self._hash = Cursor_hash(self)
1023
1024 return self._hash
1025
Daniel Dunbarde3b8e52010-01-24 04:10:22 +00001026 def get_children(self):
Daniel Dunbar12bf15c2010-01-24 21:20:29 +00001027 """Return an iterator for accessing the children of this cursor."""
Daniel Dunbarde3b8e52010-01-24 04:10:22 +00001028
1029 # FIXME: Expose iteration from CIndex, PR6125.
1030 def visitor(child, parent, children):
Daniel Dunbarfb8ae172010-01-24 21:20:05 +00001031 # FIXME: Document this assertion in API.
1032 # FIXME: There should just be an isNull method.
1033 assert child != Cursor_null()
Daniel Dunbarde3b8e52010-01-24 04:10:22 +00001034 children.append(child)
1035 return 1 # continue
1036 children = []
Daniel Dunbar532fc632010-01-30 23:59:02 +00001037 Cursor_visit(self, Cursor_visit_callback(visitor), children)
Daniel Dunbarde3b8e52010-01-24 04:10:22 +00001038 return iter(children)
1039
Daniel Dunbarfb8ae172010-01-24 21:20:05 +00001040 @staticmethod
1041 def from_result(res, fn, args):
1042 assert isinstance(res, Cursor)
1043 # FIXME: There should just be an isNull method.
1044 if res == Cursor_null():
1045 return None
1046 return res
1047
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001048
1049### Type Kinds ###
1050
1051class TypeKind(object):
1052 """
1053 Describes the kind of type.
1054 """
1055
1056 # The unique kind objects, indexed by id.
1057 _kinds = []
1058 _name_map = None
1059
1060 def __init__(self, value):
1061 if value >= len(TypeKind._kinds):
1062 TypeKind._kinds += [None] * (value - len(TypeKind._kinds) + 1)
1063 if TypeKind._kinds[value] is not None:
1064 raise ValueError,'TypeKind already loaded'
1065 self.value = value
1066 TypeKind._kinds[value] = self
1067 TypeKind._name_map = None
1068
1069 def from_param(self):
1070 return self.value
1071
1072 @property
1073 def name(self):
1074 """Get the enumeration name of this cursor kind."""
1075 if self._name_map is None:
1076 self._name_map = {}
1077 for key,value in TypeKind.__dict__.items():
1078 if isinstance(value,TypeKind):
1079 self._name_map[value] = key
1080 return self._name_map[self]
1081
Gregory Szorc6e67eed2012-04-15 18:51:10 +00001082 @property
1083 def spelling(self):
1084 """Retrieve the spelling of this TypeKind."""
1085 return TypeKind_spelling(self.value)
1086
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001087 @staticmethod
1088 def from_id(id):
1089 if id >= len(TypeKind._kinds) or TypeKind._kinds[id] is None:
Douglas Gregor9d342ab2011-10-19 05:49:29 +00001090 raise ValueError,'Unknown type kind %d' % id
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001091 return TypeKind._kinds[id]
1092
1093 def __repr__(self):
1094 return 'TypeKind.%s' % (self.name,)
1095
Gregory Szorc6e67eed2012-04-15 18:51:10 +00001096TypeKind_spelling = lib.clang_getTypeKindSpelling
1097TypeKind_spelling.argtypes = [c_uint]
1098TypeKind_spelling.restype = _CXString
1099TypeKind_spelling.errcheck = _CXString.from_result
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001100
1101
1102TypeKind.INVALID = TypeKind(0)
1103TypeKind.UNEXPOSED = TypeKind(1)
1104TypeKind.VOID = TypeKind(2)
1105TypeKind.BOOL = TypeKind(3)
1106TypeKind.CHAR_U = TypeKind(4)
1107TypeKind.UCHAR = TypeKind(5)
1108TypeKind.CHAR16 = TypeKind(6)
1109TypeKind.CHAR32 = TypeKind(7)
1110TypeKind.USHORT = TypeKind(8)
1111TypeKind.UINT = TypeKind(9)
1112TypeKind.ULONG = TypeKind(10)
1113TypeKind.ULONGLONG = TypeKind(11)
1114TypeKind.UINT128 = TypeKind(12)
1115TypeKind.CHAR_S = TypeKind(13)
1116TypeKind.SCHAR = TypeKind(14)
1117TypeKind.WCHAR = TypeKind(15)
1118TypeKind.SHORT = TypeKind(16)
1119TypeKind.INT = TypeKind(17)
1120TypeKind.LONG = TypeKind(18)
1121TypeKind.LONGLONG = TypeKind(19)
1122TypeKind.INT128 = TypeKind(20)
1123TypeKind.FLOAT = TypeKind(21)
1124TypeKind.DOUBLE = TypeKind(22)
1125TypeKind.LONGDOUBLE = TypeKind(23)
1126TypeKind.NULLPTR = TypeKind(24)
1127TypeKind.OVERLOAD = TypeKind(25)
1128TypeKind.DEPENDENT = TypeKind(26)
1129TypeKind.OBJCID = TypeKind(27)
1130TypeKind.OBJCCLASS = TypeKind(28)
1131TypeKind.OBJCSEL = TypeKind(29)
1132TypeKind.COMPLEX = TypeKind(100)
1133TypeKind.POINTER = TypeKind(101)
1134TypeKind.BLOCKPOINTER = TypeKind(102)
1135TypeKind.LVALUEREFERENCE = TypeKind(103)
1136TypeKind.RVALUEREFERENCE = TypeKind(104)
1137TypeKind.RECORD = TypeKind(105)
1138TypeKind.ENUM = TypeKind(106)
1139TypeKind.TYPEDEF = TypeKind(107)
1140TypeKind.OBJCINTERFACE = TypeKind(108)
1141TypeKind.OBJCOBJECTPOINTER = TypeKind(109)
1142TypeKind.FUNCTIONNOPROTO = TypeKind(110)
1143TypeKind.FUNCTIONPROTO = TypeKind(111)
Douglas Gregor38d2d552011-10-19 05:50:34 +00001144TypeKind.CONSTANTARRAY = TypeKind(112)
Tobias Grosser250d2172012-02-05 11:42:14 +00001145TypeKind.VECTOR = TypeKind(113)
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001146
1147class Type(Structure):
1148 """
1149 The type of an element in the abstract syntax tree.
1150 """
1151 _fields_ = [("_kind_id", c_int), ("data", c_void_p * 2)]
1152
1153 @property
1154 def kind(self):
1155 """Return the kind of this type."""
1156 return TypeKind.from_id(self._kind_id)
1157
Gregory Szorc826fce52012-02-20 17:45:30 +00001158 def argument_types(self):
1159 """Retrieve a container for the non-variadic arguments for this type.
1160
1161 The returned object is iterable and indexable. Each item in the
1162 container is a Type instance.
1163 """
1164 class ArgumentsIterator(collections.Sequence):
1165 def __init__(self, parent):
1166 self.parent = parent
1167 self.length = None
1168
1169 def __len__(self):
1170 if self.length is None:
1171 self.length = Type_get_num_arg_types(self.parent)
1172
1173 return self.length
1174
1175 def __getitem__(self, key):
1176 # FIXME Support slice objects.
1177 if not isinstance(key, int):
1178 raise TypeError("Must supply a non-negative int.")
1179
1180 if key < 0:
1181 raise IndexError("Only non-negative indexes are accepted.")
1182
1183 if key >= len(self):
1184 raise IndexError("Index greater than container length: "
1185 "%d > %d" % ( key, len(self) ))
1186
1187 result = Type_get_arg_type(self.parent, key)
1188 if result.kind == TypeKind.INVALID:
1189 raise IndexError("Argument could not be retrieved.")
1190
1191 return result
1192
1193 assert self.kind == TypeKind.FUNCTIONPROTO
1194 return ArgumentsIterator(self)
1195
Gregory Szorc86057602012-02-17 07:44:46 +00001196 @property
1197 def element_type(self):
1198 """Retrieve the Type of elements within this Type.
1199
1200 If accessed on a type that is not an array, complex, or vector type, an
1201 exception will be raised.
1202 """
1203 result = Type_get_element_type(self)
1204 if result.kind == TypeKind.INVALID:
1205 raise Exception('Element type not available on this type.')
1206
1207 return result
1208
Gregory Szorcbf8ca002012-02-17 07:47:38 +00001209 @property
1210 def element_count(self):
1211 """Retrieve the number of elements in this type.
1212
1213 Returns an int.
1214
1215 If the Type is not an array or vector, this raises.
1216 """
1217 result = Type_get_num_elements(self)
1218 if result < 0:
1219 raise Exception('Type does not have elements.')
1220
1221 return result
1222
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001223 @staticmethod
1224 def from_result(res, fn, args):
1225 assert isinstance(res, Type)
1226 return res
1227
1228 def get_canonical(self):
1229 """
1230 Return the canonical type for a Type.
1231
1232 Clang's type system explicitly models typedefs and all the
1233 ways a specific type can be represented. The canonical type
1234 is the underlying type with all the "sugar" removed. For
1235 example, if 'T' is a typedef for 'int', the canonical type for
1236 'T' would be 'int'.
1237 """
1238 return Type_get_canonical(self)
1239
1240 def is_const_qualified(self):
Gregory Szorc82613452012-02-20 17:58:40 +00001241 """Determine whether a Type has the "const" qualifier set.
1242
1243 This does not look through typedefs that may have added "const"
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001244 at a different level.
1245 """
1246 return Type_is_const_qualified(self)
1247
1248 def is_volatile_qualified(self):
Gregory Szorc82613452012-02-20 17:58:40 +00001249 """Determine whether a Type has the "volatile" qualifier set.
1250
1251 This does not look through typedefs that may have added "volatile"
1252 at a different level.
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001253 """
1254 return Type_is_volatile_qualified(self)
1255
1256 def is_restrict_qualified(self):
Gregory Szorc82613452012-02-20 17:58:40 +00001257 """Determine whether a Type has the "restrict" qualifier set.
1258
1259 This does not look through typedefs that may have added "restrict" at
1260 a different level.
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001261 """
1262 return Type_is_restrict_qualified(self)
1263
Gregory Szorc31cc38c2012-02-19 18:28:33 +00001264 def is_function_variadic(self):
1265 """Determine whether this function Type is a variadic function type."""
1266 assert self.kind == TypeKind.FUNCTIONPROTO
1267
1268 return Type_is_variadic(self)
1269
Gregory Szorc96ad6332012-02-05 19:42:06 +00001270 def is_pod(self):
1271 """Determine whether this Type represents plain old data (POD)."""
1272 return Type_is_pod(self)
1273
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001274 def get_pointee(self):
1275 """
1276 For pointer types, returns the type of the pointee.
1277 """
1278 return Type_get_pointee(self)
1279
1280 def get_declaration(self):
1281 """
1282 Return the cursor for the declaration of the given type.
1283 """
1284 return Type_get_declaration(self)
1285
1286 def get_result(self):
1287 """
1288 Retrieve the result type associated with a function type.
1289 """
1290 return Type_get_result(self)
1291
Douglas Gregor13102ff2011-10-19 05:51:43 +00001292 def get_array_element_type(self):
1293 """
1294 Retrieve the type of the elements of the array type.
1295 """
1296 return Type_get_array_element(self)
1297
1298 def get_array_size(self):
1299 """
1300 Retrieve the size of the constant array.
1301 """
1302 return Type_get_array_size(self)
1303
Gregory Szorc7eb691a2012-02-20 17:44:49 +00001304 def __eq__(self, other):
1305 if type(other) != type(self):
1306 return False
1307
1308 return Type_equal(self, other)
1309
1310 def __ne__(self, other):
1311 return not self.__eq__(other)
1312
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001313## CIndex Objects ##
1314
1315# CIndex objects (derived from ClangObject) are essentially lightweight
1316# wrappers attached to some underlying object, which is exposed via CIndex as
1317# a void*.
1318
1319class ClangObject(object):
1320 """
1321 A helper for Clang objects. This class helps act as an intermediary for
1322 the ctypes library and the Clang CIndex library.
1323 """
1324 def __init__(self, obj):
1325 assert isinstance(obj, c_object_p) and obj
1326 self.obj = self._as_parameter_ = obj
1327
1328 def from_param(self):
1329 return self._as_parameter_
1330
Daniel Dunbar5b534f62010-01-25 00:44:11 +00001331
1332class _CXUnsavedFile(Structure):
1333 """Helper for passing unsaved file arguments."""
1334 _fields_ = [("name", c_char_p), ("contents", c_char_p), ('length', c_ulong)]
1335
Daniel Dunbar532fc632010-01-30 23:59:02 +00001336## Diagnostic Conversion ##
1337
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001338_clang_getNumDiagnostics = lib.clang_getNumDiagnostics
1339_clang_getNumDiagnostics.argtypes = [c_object_p]
1340_clang_getNumDiagnostics.restype = c_uint
1341
1342_clang_getDiagnostic = lib.clang_getDiagnostic
1343_clang_getDiagnostic.argtypes = [c_object_p, c_uint]
1344_clang_getDiagnostic.restype = c_object_p
1345
1346_clang_disposeDiagnostic = lib.clang_disposeDiagnostic
Tobias Grosserff090ca2011-02-05 17:53:48 +00001347_clang_disposeDiagnostic.argtypes = [Diagnostic]
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001348
Daniel Dunbar532fc632010-01-30 23:59:02 +00001349_clang_getDiagnosticSeverity = lib.clang_getDiagnosticSeverity
Tobias Grosserff090ca2011-02-05 17:53:48 +00001350_clang_getDiagnosticSeverity.argtypes = [Diagnostic]
Daniel Dunbar532fc632010-01-30 23:59:02 +00001351_clang_getDiagnosticSeverity.restype = c_int
1352
1353_clang_getDiagnosticLocation = lib.clang_getDiagnosticLocation
Tobias Grosserff090ca2011-02-05 17:53:48 +00001354_clang_getDiagnosticLocation.argtypes = [Diagnostic]
Daniel Dunbar532fc632010-01-30 23:59:02 +00001355_clang_getDiagnosticLocation.restype = SourceLocation
1356
1357_clang_getDiagnosticSpelling = lib.clang_getDiagnosticSpelling
Tobias Grosserff090ca2011-02-05 17:53:48 +00001358_clang_getDiagnosticSpelling.argtypes = [Diagnostic]
Daniel Dunbar532fc632010-01-30 23:59:02 +00001359_clang_getDiagnosticSpelling.restype = _CXString
1360_clang_getDiagnosticSpelling.errcheck = _CXString.from_result
1361
Daniel Dunbarb51abe92010-02-13 18:33:03 +00001362_clang_getDiagnosticNumRanges = lib.clang_getDiagnosticNumRanges
Tobias Grosserff090ca2011-02-05 17:53:48 +00001363_clang_getDiagnosticNumRanges.argtypes = [Diagnostic]
Daniel Dunbarb51abe92010-02-13 18:33:03 +00001364_clang_getDiagnosticNumRanges.restype = c_uint
Daniel Dunbar532fc632010-01-30 23:59:02 +00001365
Daniel Dunbarb51abe92010-02-13 18:33:03 +00001366_clang_getDiagnosticRange = lib.clang_getDiagnosticRange
Tobias Grosserff090ca2011-02-05 17:53:48 +00001367_clang_getDiagnosticRange.argtypes = [Diagnostic, c_uint]
Daniel Dunbarb51abe92010-02-13 18:33:03 +00001368_clang_getDiagnosticRange.restype = SourceRange
Daniel Dunbar532fc632010-01-30 23:59:02 +00001369
1370_clang_getDiagnosticNumFixIts = lib.clang_getDiagnosticNumFixIts
Tobias Grosserff090ca2011-02-05 17:53:48 +00001371_clang_getDiagnosticNumFixIts.argtypes = [Diagnostic]
Daniel Dunbar532fc632010-01-30 23:59:02 +00001372_clang_getDiagnosticNumFixIts.restype = c_uint
1373
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001374_clang_getDiagnosticFixIt = lib.clang_getDiagnosticFixIt
Tobias Grosserff090ca2011-02-05 17:53:48 +00001375_clang_getDiagnosticFixIt.argtypes = [Diagnostic, c_uint, POINTER(SourceRange)]
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001376_clang_getDiagnosticFixIt.restype = _CXString
1377_clang_getDiagnosticFixIt.errcheck = _CXString.from_result
Daniel Dunbar532fc632010-01-30 23:59:02 +00001378
Tobias Grosserea403822012-02-05 11:41:58 +00001379_clang_getDiagnosticCategory = lib.clang_getDiagnosticCategory
1380_clang_getDiagnosticCategory.argtypes = [Diagnostic]
1381_clang_getDiagnosticCategory.restype = c_uint
1382
1383_clang_getDiagnosticCategoryName = lib.clang_getDiagnosticCategoryName
1384_clang_getDiagnosticCategoryName.argtypes = [c_uint]
1385_clang_getDiagnosticCategoryName.restype = _CXString
1386_clang_getDiagnosticCategoryName.errcheck = _CXString.from_result
1387
1388_clang_getDiagnosticOption = lib.clang_getDiagnosticOption
1389_clang_getDiagnosticOption.argtypes = [Diagnostic, POINTER(_CXString)]
1390_clang_getDiagnosticOption.restype = _CXString
1391_clang_getDiagnosticOption.errcheck = _CXString.from_result
1392
Daniel Dunbar532fc632010-01-30 23:59:02 +00001393###
1394
Tobias Grossera87dbcc2011-02-05 17:54:10 +00001395class CompletionChunk:
1396 class Kind:
1397 def __init__(self, name):
1398 self.name = name
1399
1400 def __str__(self):
1401 return self.name
1402
1403 def __repr__(self):
1404 return "<ChunkKind: %s>" % self
1405
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001406 def __init__(self, completionString, key):
1407 self.cs = completionString
1408 self.key = key
1409
1410 def __repr__(self):
Tobias Grossera87dbcc2011-02-05 17:54:10 +00001411 return "{'" + self.spelling + "', " + str(self.kind) + "}"
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001412
1413 @property
1414 def spelling(self):
1415 return _clang_getCompletionChunkText(self.cs, self.key).spelling
1416
1417 @property
1418 def kind(self):
Tobias Grossera87dbcc2011-02-05 17:54:10 +00001419 res = _clang_getCompletionChunkKind(self.cs, self.key)
1420 return completionChunkKindMap[res]
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001421
1422 @property
1423 def string(self):
1424 res = _clang_getCompletionChunkCompletionString(self.cs, self.key)
1425
1426 if (res):
1427 return CompletionString(res)
1428 else:
1429 None
1430
Tobias Grossera87dbcc2011-02-05 17:54:10 +00001431 def isKindOptional(self):
1432 return self.kind == completionChunkKindMap[0]
1433
1434 def isKindTypedText(self):
1435 return self.kind == completionChunkKindMap[1]
1436
1437 def isKindPlaceHolder(self):
1438 return self.kind == completionChunkKindMap[3]
1439
1440 def isKindInformative(self):
1441 return self.kind == completionChunkKindMap[4]
1442
1443 def isKindResultType(self):
1444 return self.kind == completionChunkKindMap[15]
1445
1446completionChunkKindMap = {
1447 0: CompletionChunk.Kind("Optional"),
1448 1: CompletionChunk.Kind("TypedText"),
1449 2: CompletionChunk.Kind("Text"),
1450 3: CompletionChunk.Kind("Placeholder"),
1451 4: CompletionChunk.Kind("Informative"),
1452 5: CompletionChunk.Kind("CurrentParameter"),
1453 6: CompletionChunk.Kind("LeftParen"),
1454 7: CompletionChunk.Kind("RightParen"),
1455 8: CompletionChunk.Kind("LeftBracket"),
1456 9: CompletionChunk.Kind("RightBracket"),
1457 10: CompletionChunk.Kind("LeftBrace"),
1458 11: CompletionChunk.Kind("RightBrace"),
1459 12: CompletionChunk.Kind("LeftAngle"),
1460 13: CompletionChunk.Kind("RightAngle"),
1461 14: CompletionChunk.Kind("Comma"),
1462 15: CompletionChunk.Kind("ResultType"),
1463 16: CompletionChunk.Kind("Colon"),
1464 17: CompletionChunk.Kind("SemiColon"),
1465 18: CompletionChunk.Kind("Equal"),
1466 19: CompletionChunk.Kind("HorizontalSpace"),
1467 20: CompletionChunk.Kind("VerticalSpace")}
1468
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001469class CompletionString(ClangObject):
Tobias Grossera87dbcc2011-02-05 17:54:10 +00001470 class Availability:
1471 def __init__(self, name):
1472 self.name = name
1473
1474 def __str__(self):
1475 return self.name
1476
1477 def __repr__(self):
1478 return "<Availability: %s>" % self
1479
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001480 def __len__(self):
1481 return _clang_getNumCompletionChunks(self.obj)
1482
1483 def __getitem__(self, key):
1484 if len(self) <= key:
1485 raise IndexError
Tobias Grossera87dbcc2011-02-05 17:54:10 +00001486 return CompletionChunk(self.obj, key)
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001487
1488 @property
1489 def priority(self):
1490 return _clang_getCompletionPriority(self.obj)
1491
1492 @property
1493 def availability(self):
Tobias Grossera87dbcc2011-02-05 17:54:10 +00001494 res = _clang_getCompletionAvailability(self.obj)
1495 return availabilityKinds[res]
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001496
1497 def __repr__(self):
Tobias Grossera87dbcc2011-02-05 17:54:10 +00001498 return " | ".join([str(a) for a in self]) \
1499 + " || Priority: " + str(self.priority) \
1500 + " || Availability: " + str(self.availability)
1501
1502availabilityKinds = {
1503 0: CompletionChunk.Kind("Available"),
1504 1: CompletionChunk.Kind("Deprecated"),
1505 2: CompletionChunk.Kind("NotAvailable")}
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001506
Tobias Grosser0a166802011-02-05 17:54:04 +00001507class CodeCompletionResult(Structure):
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001508 _fields_ = [('cursorKind', c_int), ('completionString', c_object_p)]
1509
1510 def __repr__(self):
1511 return str(CompletionString(self.completionString))
1512
1513 @property
1514 def kind(self):
1515 return CursorKind.from_id(self.cursorKind)
1516
1517 @property
1518 def string(self):
1519 return CompletionString(self.completionString)
Tobias Grosser0a166802011-02-05 17:54:04 +00001520
1521class CCRStructure(Structure):
1522 _fields_ = [('results', POINTER(CodeCompletionResult)),
1523 ('numResults', c_int)]
1524
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001525 def __len__(self):
1526 return self.numResults
1527
1528 def __getitem__(self, key):
1529 if len(self) <= key:
1530 raise IndexError
1531
1532 return self.results[key]
1533
Tobias Grosser0a166802011-02-05 17:54:04 +00001534class CodeCompletionResults(ClangObject):
1535 def __init__(self, ptr):
1536 assert isinstance(ptr, POINTER(CCRStructure)) and ptr
1537 self.ptr = self._as_parameter_ = ptr
1538
1539 def from_param(self):
1540 return self._as_parameter_
1541
1542 def __del__(self):
1543 CodeCompletionResults_dispose(self)
1544
1545 @property
1546 def results(self):
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00001547 return self.ptr.contents
Tobias Grosser0a166802011-02-05 17:54:04 +00001548
1549 @property
1550 def diagnostics(self):
1551 class DiagnosticsItr:
1552 def __init__(self, ccr):
1553 self.ccr= ccr
1554
1555 def __len__(self):
1556 return int(_clang_codeCompleteGetNumDiagnostics(self.ccr))
1557
1558 def __getitem__(self, key):
1559 return _clang_codeCompleteGetDiagnostic(self.ccr, key)
1560
1561 return DiagnosticsItr(self)
1562
1563
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001564class Index(ClangObject):
1565 """
1566 The Index type provides the primary interface to the Clang CIndex library,
1567 primarily by providing an interface for reading and parsing translation
1568 units.
1569 """
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001570
1571 @staticmethod
Daniel Dunbar2791dfc2010-01-30 23:58:39 +00001572 def create(excludeDecls=False):
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001573 """
1574 Create a new Index.
1575 Parameters:
1576 excludeDecls -- Exclude local declarations from translation units.
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001577 """
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001578 return Index(Index_create(excludeDecls, 0))
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001579
1580 def __del__(self):
1581 Index_dispose(self)
1582
1583 def read(self, path):
1584 """Load the translation unit from the given AST file."""
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001585 ptr = TranslationUnit_read(self, path)
Tobias Grosserba5d10b2011-10-31 02:06:50 +00001586 if ptr:
1587 return TranslationUnit(ptr)
1588 return None
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001589
Tobias Grosser265e6b22011-02-05 17:54:00 +00001590 def parse(self, path, args = [], unsaved_files = [], options = 0):
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001591 """
1592 Load the translation unit from the given source code file by running
1593 clang and generating the AST before loading. Additional command line
1594 parameters can be passed to clang via the args parameter.
Daniel Dunbar5b534f62010-01-25 00:44:11 +00001595
1596 In-memory contents for files can be provided by passing a list of pairs
1597 to as unsaved_files, the first item should be the filenames to be mapped
1598 and the second should be the contents to be substituted for the
1599 file. The contents may be passed as strings or file objects.
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001600 """
Daniel Dunbar061bae92010-01-25 09:16:55 +00001601 arg_array = 0
1602 if len(args):
1603 arg_array = (c_char_p * len(args))(* args)
1604 unsaved_files_array = 0
1605 if len(unsaved_files):
1606 unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
1607 for i,(name,value) in enumerate(unsaved_files):
1608 if not isinstance(value, str):
1609 # FIXME: It would be great to support an efficient version
1610 # of this, one day.
1611 value = value.read()
1612 print value
1613 if not isinstance(value, str):
1614 raise TypeError,'Unexpected unsaved file contents.'
1615 unsaved_files_array[i].name = name
1616 unsaved_files_array[i].contents = value
1617 unsaved_files_array[i].length = len(value)
Tobias Grosser265e6b22011-02-05 17:54:00 +00001618 ptr = TranslationUnit_parse(self, path, arg_array, len(args),
1619 unsaved_files_array, len(unsaved_files),
1620 options)
Tobias Grosserba5d10b2011-10-31 02:06:50 +00001621 if ptr:
1622 return TranslationUnit(ptr)
1623 return None
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001624
1625
1626class TranslationUnit(ClangObject):
1627 """
1628 The TranslationUnit class represents a source code translation unit and
1629 provides read-only access to its top-level declarations.
1630 """
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001631
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001632 def __init__(self, ptr):
Daniel Dunbar532fc632010-01-30 23:59:02 +00001633 ClangObject.__init__(self, ptr)
Daniel Dunbar532fc632010-01-30 23:59:02 +00001634
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001635 def __del__(self):
Daniel Dunbar99d593e2010-01-24 04:09:51 +00001636 TranslationUnit_dispose(self)
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001637
Daniel Dunbar1b945a72010-01-24 04:09:43 +00001638 @property
1639 def cursor(self):
1640 """Retrieve the cursor that represents the given translation unit."""
1641 return TranslationUnit_cursor(self)
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001642
1643 @property
1644 def spelling(self):
Daniel Dunbar1b945a72010-01-24 04:09:43 +00001645 """Get the original translation unit source file name."""
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001646 return TranslationUnit_spelling(self)
1647
Daniel Dunbaref7f7982010-02-13 18:33:18 +00001648 def get_includes(self):
1649 """
1650 Return an iterable sequence of FileInclusion objects that describe the
1651 sequence of inclusions in a translation unit. The first object in
1652 this sequence is always the input file. Note that this method will not
1653 recursively iterate over header files included through precompiled
1654 headers.
1655 """
1656 def visitor(fobj, lptr, depth, includes):
Douglas Gregor8be80e12011-07-06 03:00:34 +00001657 if depth > 0:
1658 loc = lptr.contents
1659 includes.append(FileInclusion(loc.file, File(fobj), loc, depth))
Daniel Dunbaref7f7982010-02-13 18:33:18 +00001660
1661 # Automatically adapt CIndex/ctype pointers to python objects
1662 includes = []
1663 TranslationUnit_includes(self,
1664 TranslationUnit_includes_callback(visitor),
1665 includes)
1666 return iter(includes)
1667
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001668 @property
1669 def diagnostics(self):
1670 """
1671 Return an iterable (and indexable) object containing the diagnostics.
1672 """
Benjamin Kramer1d02ccd2010-03-06 15:38:03 +00001673 class DiagIterator:
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001674 def __init__(self, tu):
1675 self.tu = tu
1676
1677 def __len__(self):
1678 return int(_clang_getNumDiagnostics(self.tu))
1679
1680 def __getitem__(self, key):
Benjamin Kramer1d02ccd2010-03-06 15:38:03 +00001681 diag = _clang_getDiagnostic(self.tu, key)
1682 if not diag:
1683 raise IndexError
1684 return Diagnostic(diag)
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001685
Benjamin Kramer1d02ccd2010-03-06 15:38:03 +00001686 return DiagIterator(self)
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00001687
Tobias Grosser265e6b22011-02-05 17:54:00 +00001688 def reparse(self, unsaved_files = [], options = 0):
1689 """
1690 Reparse an already parsed translation unit.
1691
1692 In-memory contents for files can be provided by passing a list of pairs
1693 as unsaved_files, the first items should be the filenames to be mapped
1694 and the second should be the contents to be substituted for the
1695 file. The contents may be passed as strings or file objects.
1696 """
1697 unsaved_files_array = 0
1698 if len(unsaved_files):
1699 unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
1700 for i,(name,value) in enumerate(unsaved_files):
1701 if not isinstance(value, str):
1702 # FIXME: It would be great to support an efficient version
1703 # of this, one day.
1704 value = value.read()
1705 print value
1706 if not isinstance(value, str):
1707 raise TypeError,'Unexpected unsaved file contents.'
1708 unsaved_files_array[i].name = name
1709 unsaved_files_array[i].contents = value
1710 unsaved_files_array[i].length = len(value)
1711 ptr = TranslationUnit_reparse(self, len(unsaved_files),
1712 unsaved_files_array,
1713 options)
Tobias Grosser0a166802011-02-05 17:54:04 +00001714 def codeComplete(self, path, line, column, unsaved_files = [], options = 0):
1715 """
1716 Code complete in this translation unit.
1717
1718 In-memory contents for files can be provided by passing a list of pairs
1719 as unsaved_files, the first items should be the filenames to be mapped
1720 and the second should be the contents to be substituted for the
1721 file. The contents may be passed as strings or file objects.
1722 """
1723 unsaved_files_array = 0
1724 if len(unsaved_files):
1725 unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
1726 for i,(name,value) in enumerate(unsaved_files):
1727 if not isinstance(value, str):
1728 # FIXME: It would be great to support an efficient version
1729 # of this, one day.
1730 value = value.read()
1731 print value
1732 if not isinstance(value, str):
1733 raise TypeError,'Unexpected unsaved file contents.'
1734 unsaved_files_array[i].name = name
1735 unsaved_files_array[i].contents = value
1736 unsaved_files_array[i].length = len(value)
1737 ptr = TranslationUnit_codeComplete(self, path,
1738 line, column,
1739 unsaved_files_array,
1740 len(unsaved_files),
1741 options)
Tobias Grosserba5d10b2011-10-31 02:06:50 +00001742 if ptr:
1743 return CodeCompletionResults(ptr)
1744 return None
Tobias Grosser265e6b22011-02-05 17:54:00 +00001745
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001746class File(ClangObject):
1747 """
Daniel Dunbar7b48b352010-01-24 04:09:34 +00001748 The File class represents a particular source file that is part of a
1749 translation unit.
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001750 """
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001751
Tobias Grossera9ea5df2011-10-31 00:07:19 +00001752 @staticmethod
1753 def from_name(translation_unit, file_name):
1754 """Retrieve a file handle within the given translation unit."""
1755 return File(File_getFile(translation_unit, file_name))
1756
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001757 @property
1758 def name(self):
Daniel Dunbar4efd6322010-01-25 00:43:08 +00001759 """Return the complete file and path name of the file."""
Douglas Gregor8be80e12011-07-06 03:00:34 +00001760 return _CXString_getCString(File_name(self))
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001761
1762 @property
1763 def time(self):
Daniel Dunbar4efd6322010-01-25 00:43:08 +00001764 """Return the last modification time of the file."""
Daniel Dunbar7b48b352010-01-24 04:09:34 +00001765 return File_time(self)
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001766
Tobias Grossera9ea5df2011-10-31 00:07:19 +00001767 def __str__(self):
1768 return self.name
1769
1770 def __repr__(self):
1771 return "<File: %s>" % (self.name)
1772
Daniel Dunbaref7f7982010-02-13 18:33:18 +00001773class FileInclusion(object):
1774 """
1775 The FileInclusion class represents the inclusion of one source file by
1776 another via a '#include' directive or as the input file for the translation
1777 unit. This class provides information about the included file, the including
1778 file, the location of the '#include' directive and the depth of the included
1779 file in the stack. Note that the input file has depth 0.
1780 """
1781
1782 def __init__(self, src, tgt, loc, depth):
1783 self.source = src
1784 self.include = tgt
1785 self.location = loc
1786 self.depth = depth
1787
1788 @property
1789 def is_input_file(self):
1790 """True if the included file is the input file."""
1791 return self.depth == 0
1792
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001793# Additional Functions and Types
1794
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001795# String Functions
Daniel Dunbara33dca42010-01-24 21:19:57 +00001796_CXString_dispose = lib.clang_disposeString
1797_CXString_dispose.argtypes = [_CXString]
1798
1799_CXString_getCString = lib.clang_getCString
1800_CXString_getCString.argtypes = [_CXString]
1801_CXString_getCString.restype = c_char_p
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001802
1803# Source Location Functions
1804SourceLocation_loc = lib.clang_getInstantiationLocation
Daniel Dunbarbe0b5552010-01-24 21:19:48 +00001805SourceLocation_loc.argtypes = [SourceLocation, POINTER(c_object_p),
Daniel Dunbar3239a672010-01-29 17:02:32 +00001806 POINTER(c_uint), POINTER(c_uint),
1807 POINTER(c_uint)]
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001808
Tobias Grosser58ba8c92011-10-31 00:31:32 +00001809SourceLocation_getLocation = lib.clang_getLocation
1810SourceLocation_getLocation.argtypes = [TranslationUnit, File, c_uint, c_uint]
1811SourceLocation_getLocation.restype = SourceLocation
1812
Tobias Grosser74858332012-02-05 11:40:59 +00001813SourceLocation_equalLocations = lib.clang_equalLocations
1814SourceLocation_equalLocations.argtypes = [SourceLocation, SourceLocation]
1815SourceLocation_equalLocations.restype = bool
1816
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001817# Source Range Functions
Daniel Dunbar532fc632010-01-30 23:59:02 +00001818SourceRange_getRange = lib.clang_getRange
1819SourceRange_getRange.argtypes = [SourceLocation, SourceLocation]
1820SourceRange_getRange.restype = SourceRange
1821
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001822SourceRange_start = lib.clang_getRangeStart
Daniel Dunbar7b48b352010-01-24 04:09:34 +00001823SourceRange_start.argtypes = [SourceRange]
1824SourceRange_start.restype = SourceLocation
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001825
1826SourceRange_end = lib.clang_getRangeEnd
Daniel Dunbar7b48b352010-01-24 04:09:34 +00001827SourceRange_end.argtypes = [SourceRange]
1828SourceRange_end.restype = SourceLocation
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001829
Tobias Grosser74858332012-02-05 11:40:59 +00001830SourceRange_equalRanges = lib.clang_equalRanges
1831SourceRange_equalRanges.argtypes = [SourceRange, SourceRange]
1832SourceRange_equalRanges.restype = bool
1833
Daniel Dunbara6a64992010-01-24 21:20:39 +00001834# CursorKind Functions
1835CursorKind_is_decl = lib.clang_isDeclaration
1836CursorKind_is_decl.argtypes = [CursorKind]
1837CursorKind_is_decl.restype = bool
1838
1839CursorKind_is_ref = lib.clang_isReference
1840CursorKind_is_ref.argtypes = [CursorKind]
1841CursorKind_is_ref.restype = bool
1842
1843CursorKind_is_expr = lib.clang_isExpression
1844CursorKind_is_expr.argtypes = [CursorKind]
1845CursorKind_is_expr.restype = bool
1846
1847CursorKind_is_stmt = lib.clang_isStatement
1848CursorKind_is_stmt.argtypes = [CursorKind]
1849CursorKind_is_stmt.restype = bool
1850
Douglas Gregor8be80e12011-07-06 03:00:34 +00001851CursorKind_is_attribute = lib.clang_isAttribute
1852CursorKind_is_attribute.argtypes = [CursorKind]
1853CursorKind_is_attribute.restype = bool
1854
Daniel Dunbara6a64992010-01-24 21:20:39 +00001855CursorKind_is_inv = lib.clang_isInvalid
1856CursorKind_is_inv.argtypes = [CursorKind]
1857CursorKind_is_inv.restype = bool
1858
Tobias Grossereb136342012-02-05 11:42:09 +00001859CursorKind_is_translation_unit = lib.clang_isTranslationUnit
1860CursorKind_is_translation_unit.argtypes = [CursorKind]
1861CursorKind_is_translation_unit.restype = bool
1862
1863CursorKind_is_preprocessing = lib.clang_isPreprocessing
1864CursorKind_is_preprocessing.argtypes = [CursorKind]
1865CursorKind_is_preprocessing.restype = bool
1866
1867CursorKind_is_unexposed = lib.clang_isUnexposed
1868CursorKind_is_unexposed.argtypes = [CursorKind]
1869CursorKind_is_unexposed.restype = bool
1870
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001871# Cursor Functions
1872# TODO: Implement this function
1873Cursor_get = lib.clang_getCursor
Daniel Dunbarde3b8e52010-01-24 04:10:22 +00001874Cursor_get.argtypes = [TranslationUnit, SourceLocation]
Daniel Dunbarbe0b5552010-01-24 21:19:48 +00001875Cursor_get.restype = Cursor
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001876
1877Cursor_null = lib.clang_getNullCursor
1878Cursor_null.restype = Cursor
1879
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001880Cursor_usr = lib.clang_getCursorUSR
Daniel Dunbar3d855f82010-01-24 21:20:13 +00001881Cursor_usr.argtypes = [Cursor]
1882Cursor_usr.restype = _CXString
1883Cursor_usr.errcheck = _CXString.from_result
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001884
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001885Cursor_is_def = lib.clang_isCursorDefinition
1886Cursor_is_def.argtypes = [Cursor]
Daniel Dunbarbe0b5552010-01-24 21:19:48 +00001887Cursor_is_def.restype = bool
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001888
1889Cursor_def = lib.clang_getCursorDefinition
1890Cursor_def.argtypes = [Cursor]
1891Cursor_def.restype = Cursor
Daniel Dunbarfb8ae172010-01-24 21:20:05 +00001892Cursor_def.errcheck = Cursor.from_result
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001893
1894Cursor_eq = lib.clang_equalCursors
1895Cursor_eq.argtypes = [Cursor, Cursor]
Gregory Szorc9d008fd2012-03-05 00:42:15 +00001896Cursor_eq.restype = bool
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001897
Tobias Grosser64e7bdc2012-02-05 11:42:03 +00001898Cursor_hash = lib.clang_hashCursor
1899Cursor_hash.argtypes = [Cursor]
1900Cursor_hash.restype = c_uint
1901
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001902Cursor_spelling = lib.clang_getCursorSpelling
1903Cursor_spelling.argtypes = [Cursor]
Daniel Dunbara33dca42010-01-24 21:19:57 +00001904Cursor_spelling.restype = _CXString
1905Cursor_spelling.errcheck = _CXString.from_result
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001906
Douglas Gregor8be80e12011-07-06 03:00:34 +00001907Cursor_displayname = lib.clang_getCursorDisplayName
1908Cursor_displayname.argtypes = [Cursor]
1909Cursor_displayname.restype = _CXString
1910Cursor_displayname.errcheck = _CXString.from_result
1911
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001912Cursor_loc = lib.clang_getCursorLocation
1913Cursor_loc.argtypes = [Cursor]
1914Cursor_loc.restype = SourceLocation
1915
1916Cursor_extent = lib.clang_getCursorExtent
1917Cursor_extent.argtypes = [Cursor]
1918Cursor_extent.restype = SourceRange
1919
1920Cursor_ref = lib.clang_getCursorReferenced
1921Cursor_ref.argtypes = [Cursor]
1922Cursor_ref.restype = Cursor
Daniel Dunbarfb8ae172010-01-24 21:20:05 +00001923Cursor_ref.errcheck = Cursor.from_result
Daniel Dunbar30c0f262010-01-24 02:02:07 +00001924
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001925Cursor_type = lib.clang_getCursorType
1926Cursor_type.argtypes = [Cursor]
1927Cursor_type.restype = Type
1928Cursor_type.errcheck = Type.from_result
1929
Tobias Grosser28d939f2012-02-05 11:42:20 +00001930Cursor_underlying_type = lib.clang_getTypedefDeclUnderlyingType
1931Cursor_underlying_type.argtypes = [Cursor]
1932Cursor_underlying_type.restype = Type
1933Cursor_underlying_type.errcheck = Type.from_result
1934
Tobias Grossereb9ff2e2012-02-05 11:42:25 +00001935Cursor_enum_type = lib.clang_getEnumDeclIntegerType
1936Cursor_enum_type.argtypes = [Cursor]
1937Cursor_enum_type.restype = Type
1938Cursor_enum_type.errcheck = Type.from_result
1939
Gregory Szorc5cc67872012-03-10 22:23:27 +00001940Cursor_objc_type_encoding = lib.clang_getDeclObjCTypeEncoding
1941Cursor_objc_type_encoding.argtypes = [Cursor]
1942Cursor_objc_type_encoding.restype = _CXString
1943Cursor_objc_type_encoding.errcheck = _CXString.from_result
1944
Daniel Dunbar532fc632010-01-30 23:59:02 +00001945Cursor_visit_callback = CFUNCTYPE(c_int, Cursor, Cursor, py_object)
Daniel Dunbarde3b8e52010-01-24 04:10:22 +00001946Cursor_visit = lib.clang_visitChildren
Daniel Dunbar532fc632010-01-30 23:59:02 +00001947Cursor_visit.argtypes = [Cursor, Cursor_visit_callback, py_object]
Daniel Dunbarde3b8e52010-01-24 04:10:22 +00001948Cursor_visit.restype = c_uint
1949
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001950# Type Functions
1951Type_get_canonical = lib.clang_getCanonicalType
1952Type_get_canonical.argtypes = [Type]
1953Type_get_canonical.restype = Type
1954Type_get_canonical.errcheck = Type.from_result
1955
1956Type_is_const_qualified = lib.clang_isConstQualifiedType
1957Type_is_const_qualified.argtypes = [Type]
1958Type_is_const_qualified.restype = bool
1959
1960Type_is_volatile_qualified = lib.clang_isVolatileQualifiedType
1961Type_is_volatile_qualified.argtypes = [Type]
1962Type_is_volatile_qualified.restype = bool
1963
1964Type_is_restrict_qualified = lib.clang_isRestrictQualifiedType
1965Type_is_restrict_qualified.argtypes = [Type]
1966Type_is_restrict_qualified.restype = bool
1967
Gregory Szorc96ad6332012-02-05 19:42:06 +00001968Type_is_pod = lib.clang_isPODType
1969Type_is_pod.argtypes = [Type]
1970Type_is_pod.restype = bool
1971
Gregory Szorc31cc38c2012-02-19 18:28:33 +00001972Type_is_variadic = lib.clang_isFunctionTypeVariadic
1973Type_is_variadic.argtypes = [Type]
1974Type_is_variadic.restype = bool
1975
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00001976Type_get_pointee = lib.clang_getPointeeType
1977Type_get_pointee.argtypes = [Type]
1978Type_get_pointee.restype = Type
1979Type_get_pointee.errcheck = Type.from_result
1980
1981Type_get_declaration = lib.clang_getTypeDeclaration
1982Type_get_declaration.argtypes = [Type]
1983Type_get_declaration.restype = Cursor
1984Type_get_declaration.errcheck = Cursor.from_result
1985
1986Type_get_result = lib.clang_getResultType
1987Type_get_result.argtypes = [Type]
1988Type_get_result.restype = Type
1989Type_get_result.errcheck = Type.from_result
1990
Gregory Szorc826fce52012-02-20 17:45:30 +00001991Type_get_num_arg_types = lib.clang_getNumArgTypes
1992Type_get_num_arg_types.argtypes = [Type]
1993Type_get_num_arg_types.restype = c_uint
1994
1995Type_get_arg_type = lib.clang_getArgType
1996Type_get_arg_type.argtypes = [Type, c_uint]
1997Type_get_arg_type.restype = Type
1998Type_get_arg_type.errcheck = Type.from_result
Gregory Szorc86057602012-02-17 07:44:46 +00001999Type_get_element_type = lib.clang_getElementType
Gregory Szorc826fce52012-02-20 17:45:30 +00002000
Gregory Szorc86057602012-02-17 07:44:46 +00002001Type_get_element_type.argtypes = [Type]
2002Type_get_element_type.restype = Type
2003Type_get_element_type.errcheck = Type.from_result
2004
Gregory Szorcbf8ca002012-02-17 07:47:38 +00002005Type_get_num_elements = lib.clang_getNumElements
2006Type_get_num_elements.argtypes = [Type]
2007Type_get_num_elements.restype = c_longlong
2008
Douglas Gregor13102ff2011-10-19 05:51:43 +00002009Type_get_array_element = lib.clang_getArrayElementType
2010Type_get_array_element.argtypes = [Type]
2011Type_get_array_element.restype = Type
2012Type_get_array_element.errcheck = Type.from_result
2013
2014Type_get_array_size = lib.clang_getArraySize
2015Type_get_array_size.argtype = [Type]
2016Type_get_array_size.restype = c_longlong
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00002017
Gregory Szorc7eb691a2012-02-20 17:44:49 +00002018Type_equal = lib.clang_equalTypes
2019Type_equal.argtypes = [Type, Type]
2020Type_equal.restype = bool
2021
Daniel Dunbar30c0f262010-01-24 02:02:07 +00002022# Index Functions
2023Index_create = lib.clang_createIndex
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00002024Index_create.argtypes = [c_int, c_int]
Daniel Dunbar30c0f262010-01-24 02:02:07 +00002025Index_create.restype = c_object_p
2026
2027Index_dispose = lib.clang_disposeIndex
2028Index_dispose.argtypes = [Index]
2029
2030# Translation Unit Functions
2031TranslationUnit_read = lib.clang_createTranslationUnit
Benjamin Kramer3b0cf092010-03-06 14:53:07 +00002032TranslationUnit_read.argtypes = [Index, c_char_p]
Daniel Dunbar30c0f262010-01-24 02:02:07 +00002033TranslationUnit_read.restype = c_object_p
2034
Tobias Grosser265e6b22011-02-05 17:54:00 +00002035TranslationUnit_parse = lib.clang_parseTranslationUnit
2036TranslationUnit_parse.argtypes = [Index, c_char_p, c_void_p,
2037 c_int, c_void_p, c_int, c_int]
Daniel Dunbar30c0f262010-01-24 02:02:07 +00002038TranslationUnit_parse.restype = c_object_p
2039
Tobias Grosser265e6b22011-02-05 17:54:00 +00002040TranslationUnit_reparse = lib.clang_reparseTranslationUnit
2041TranslationUnit_reparse.argtypes = [TranslationUnit, c_int, c_void_p, c_int]
2042TranslationUnit_reparse.restype = c_int
2043
Tobias Grosser0a166802011-02-05 17:54:04 +00002044TranslationUnit_codeComplete = lib.clang_codeCompleteAt
2045TranslationUnit_codeComplete.argtypes = [TranslationUnit, c_char_p, c_int,
2046 c_int, c_void_p, c_int, c_int]
2047TranslationUnit_codeComplete.restype = POINTER(CCRStructure)
2048
Daniel Dunbar1b945a72010-01-24 04:09:43 +00002049TranslationUnit_cursor = lib.clang_getTranslationUnitCursor
2050TranslationUnit_cursor.argtypes = [TranslationUnit]
2051TranslationUnit_cursor.restype = Cursor
Daniel Dunbarfb8ae172010-01-24 21:20:05 +00002052TranslationUnit_cursor.errcheck = Cursor.from_result
Daniel Dunbar1b945a72010-01-24 04:09:43 +00002053
Daniel Dunbar30c0f262010-01-24 02:02:07 +00002054TranslationUnit_spelling = lib.clang_getTranslationUnitSpelling
2055TranslationUnit_spelling.argtypes = [TranslationUnit]
Daniel Dunbara33dca42010-01-24 21:19:57 +00002056TranslationUnit_spelling.restype = _CXString
2057TranslationUnit_spelling.errcheck = _CXString.from_result
Daniel Dunbar30c0f262010-01-24 02:02:07 +00002058
2059TranslationUnit_dispose = lib.clang_disposeTranslationUnit
2060TranslationUnit_dispose.argtypes = [TranslationUnit]
2061
Daniel Dunbaref7f7982010-02-13 18:33:18 +00002062TranslationUnit_includes_callback = CFUNCTYPE(None,
2063 c_object_p,
2064 POINTER(SourceLocation),
2065 c_uint, py_object)
2066TranslationUnit_includes = lib.clang_getInclusions
2067TranslationUnit_includes.argtypes = [TranslationUnit,
2068 TranslationUnit_includes_callback,
2069 py_object]
2070
Daniel Dunbar30c0f262010-01-24 02:02:07 +00002071# File Functions
Tobias Grossera9ea5df2011-10-31 00:07:19 +00002072File_getFile = lib.clang_getFile
2073File_getFile.argtypes = [TranslationUnit, c_char_p]
2074File_getFile.restype = c_object_p
2075
Daniel Dunbar30c0f262010-01-24 02:02:07 +00002076File_name = lib.clang_getFileName
2077File_name.argtypes = [File]
Douglas Gregor8be80e12011-07-06 03:00:34 +00002078File_name.restype = _CXString
Daniel Dunbar30c0f262010-01-24 02:02:07 +00002079
2080File_time = lib.clang_getFileTime
2081File_time.argtypes = [File]
2082File_time.restype = c_uint
Daniel Dunbar4efd6322010-01-25 00:43:08 +00002083
Tobias Grosser0a166802011-02-05 17:54:04 +00002084# Code completion
2085
2086CodeCompletionResults_dispose = lib.clang_disposeCodeCompleteResults
2087CodeCompletionResults_dispose.argtypes = [CodeCompletionResults]
2088
2089_clang_codeCompleteGetNumDiagnostics = lib.clang_codeCompleteGetNumDiagnostics
2090_clang_codeCompleteGetNumDiagnostics.argtypes = [CodeCompletionResults]
2091_clang_codeCompleteGetNumDiagnostics.restype = c_int
2092
2093_clang_codeCompleteGetDiagnostic = lib.clang_codeCompleteGetDiagnostic
2094_clang_codeCompleteGetDiagnostic.argtypes = [CodeCompletionResults, c_int]
2095_clang_codeCompleteGetDiagnostic.restype = Diagnostic
2096
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00002097_clang_getCompletionChunkText = lib.clang_getCompletionChunkText
2098_clang_getCompletionChunkText.argtypes = [c_void_p, c_int]
2099_clang_getCompletionChunkText.restype = _CXString
2100
Tobias Grossera87dbcc2011-02-05 17:54:10 +00002101_clang_getCompletionChunkKind = lib.clang_getCompletionChunkKind
2102_clang_getCompletionChunkKind.argtypes = [c_void_p, c_int]
2103_clang_getCompletionChunkKind.restype = c_int
2104
2105_clang_getCompletionChunkCompletionString = lib.clang_getCompletionChunkCompletionString
2106_clang_getCompletionChunkCompletionString.argtypes = [c_void_p, c_int]
2107_clang_getCompletionChunkCompletionString.restype = c_object_p
2108
Tobias Grosser6d2a40c2011-02-05 17:54:07 +00002109_clang_getNumCompletionChunks = lib.clang_getNumCompletionChunks
2110_clang_getNumCompletionChunks.argtypes = [c_void_p]
2111_clang_getNumCompletionChunks.restype = c_int
2112
2113_clang_getCompletionAvailability = lib.clang_getCompletionAvailability
2114_clang_getCompletionAvailability.argtypes = [c_void_p]
2115_clang_getCompletionAvailability.restype = c_int
2116
2117_clang_getCompletionPriority = lib.clang_getCompletionPriority
2118_clang_getCompletionPriority.argtypes = [c_void_p]
2119_clang_getCompletionPriority.restype = c_int
2120
2121
Daniel Dunbar4efd6322010-01-25 00:43:08 +00002122###
2123
Argyrios Kyrtzidisd7933e62011-08-17 00:43:03 +00002124__all__ = ['Index', 'TranslationUnit', 'Cursor', 'CursorKind', 'Type', 'TypeKind',
Tobias Grosser0a166802011-02-05 17:54:04 +00002125 'Diagnostic', 'FixIt', 'CodeCompletionResults', 'SourceRange',
2126 'SourceLocation', 'File']