Cursor

public protocol Cursor: CustomStringConvertible

A cursor representing some element in the abstract syntax tree for a translation unit.

The cursor abstraction unifies the different kinds of entities in a program–declaration, statements, expressions, references to declarations, etc.–under a single cursor abstraction with a common set of operations. Common operation for a cursor include: getting the physical location in a source file where the cursor points, getting the name associated with a cursor, and retrieving cursors for any child nodes of a particular cursor. Cursors can be produced in two specific ways.

TranslationUnit.cursor produces a cursor for a translation unit, from which one can use `children() to explore the rest of the translation unit.

SourceLocation.cursor maps from a physical source location to the entity that resides at that location, allowing one to map from the source code into the AST.

  • Converts this cursor value to a CXCursor value to be consumed by libclang APIs

    Declaration

    Swift

    func asClang() -> CXCursor
  • description Extension method

    Retrieve a name for the entity referenced by this cursor.

    Declaration

    Swift

    public var description: String
  • usr Extension method

    Retrieve a Unified Symbol Resolution (USR) for the entity referenced by the given cursor. A Unified Symbol Resolution (USR) is a string that identifies a particular entity (function, class, variable, etc.) within a program. USRs can be compared across translation units to determine, e.g., when references in one translation refer to an entity defined in another translation unit.

    Declaration

    Swift

    public var usr: String
  • definition Extension method

    For a cursor that is either a reference to or a declaration of some entity, retrieve a cursor that describes the definition of that entity. Some entities can be declared multiple times within a translation unit, but only one of those declarations can also be a definition. For example, given:

    int f(int, int);
    int g(int x, int y) { return f(x, y); }
    int f(int a, int b) { return a + b; }
    int f(int, int);
    

    there are three declarations of the function f, but only the second one is a definition. This variable, accessed on any cursor pointing to a declaration of f (the first or fourth lines of the example) or a cursor referenced that uses f (the call to f’ insideg), will return a declaration cursor pointing to the definition (the secondf" declaration).

    If given a cursor for which there is no corresponding definition, e.g., because there is no definition of that entity within this translation unit, returns a NULL cursor.

    Declaration

    Swift

    public var definition: Cursor?
  • displayName Extension method

    Retrieve the display name for the entity referenced by this cursor. The display name contains extra information that helps identify the cursor, such as the parameters of a function or template or the arguments of a class template specialization.

    Declaration

    Swift

    public var displayName: String
  • lexicalParent Extension method

    Determine the lexical parent of the given cursor. The lexical parent of a cursor is the cursor in which the given cursor was actually written. For many declarations, the lexical and semantic parents are equivalent. They diverge when declarations or definitions are provided out-of-line. For example:

    class C {
      void f();
    };
    void C::f() { }
    

    In the out-of-line definition of C::f, the semantic parent is the class C, of which this function is a member. The lexical parent is the place where the declaration actually occurs in the source code; in this case, the definition occurs in the translation unit. In general, the lexical parent for a given entity can change without affecting the semantics of the program, and the lexical parent of different declarations of the same entity may be different. Changing the semantic parent of a declaration, on the other hand, can have a major impact on semantics, and redeclarations of a particular entity should all have the same semantic context.

    In the example above, both declarations of C::f have C as their semantic context, while the lexical context of the first C::f is C and the lexical context of the second C::f is the translation unit. For global declarations, the semantic parent is the translation unit.

    Declaration

    Swift

    public var lexicalParent: Cursor?
  • semanticParent Extension method

    Determine the semantic parent of the given cursor. The semantic parent of a cursor is the cursor that semantically contains the given cursor. For many declarations, the lexical and semantic parents are equivalent. They diverge when declarations or definitions are provided out-of-line. For example:

    class C {
      void f();
    };
    void C::f() { }
    

    In the out-of-line definition of C::f, the semantic parent is the class C, of which this function is a member. The lexical parent is the place where the declaration actually occurs in the source code; in this case, the definition occurs in the translation unit. In general, the lexical parent for a given entity can change without affecting the semantics of the program, and the lexical parent of different declarations of the same entity may be different. Changing the semantic parent of a declaration, on the other hand, can have a major impact on semantics, and redeclarations of a particular entity should all have the same semantic context.

    In the example above, both declarations of C::f have C as their semantic context, while the lexical context of the first C::f is C and the lexical context of the second C::f is the translation unit. For global declarations, the semantic parent is the translation unit.

    Declaration

    Swift

    public var semanticParent: Cursor?
  • referenced Extension method

    For a cursor that is a reference, retrieve a cursor representing the entity that it references. Reference cursors refer to other entities in the AST. For example, an Objective-C superclass reference cursor refers to an Objective-C class. This function produces the cursor for the Objective-C class from the cursor for the superclass reference. If the input cursor is a declaration or definition, it returns that declaration or definition unchanged. Otherwise, returns the NULL cursor.

    Declaration

    Swift

    public var referenced: Cursor?
  • type Extension method

    Retrieves the type of this cursor (if any).

    Declaration

    Swift

    public var type: CType?
  • translationUnit Extension method

    Returns the translation unit that a cursor originated from.

    Declaration

    Swift

    public var translationUnit: TranslationUnit
  • children() Extension method

    Retrieves all the children of the provided cursor.

    Declaration

    Swift

    public func children() -> [Cursor]

    Return Value

    An array of Cursors that are children of this cursor.

  • visiblity Extension method

    Describe the visibility of the entity referred to by a cursor. This returns the default visibility if not explicitly specified by a visibility attribute. The default visibility may be changed by commandline arguments.

    Declaration

    Swift

    public var visiblity: VisibilityKind?
  • range Extension method

    Retrieve the physical extent of the source construct referenced by the given cursor. The extent of a cursor starts with the file/line/column pointing at the first character within the source construct that the cursor refers to and ends with the last character within that source construct. For a declaration, the extent covers the declaration itself. For a reference, the extent covers the location of the reference (e.g., where the referenced entity was actually used).

    Declaration

    Swift

    public var range: SourceRange
  • availability Extension method

    Describes any availability information that specifies if the declaration in question is deprecated or marked unavailable, and on which platforms and versions this availability declaration applies.

    Declaration

    Swift

    public var availability: Availability
  • storageClass Extension method

    Returns the storage class for a function or variable declaration.

    Declaration

    Swift

    public var storageClass: StorageClass?
  • accessSpecifier Extension method

    Returns the access control level for the referenced object. If the cursor refers to a C++ declaration, its access control level within its parent scope is returned. Otherwise, if the cursor refers to a base specifier or access specifier, the specifier itself is returned.

    Declaration

    Swift

    public var accessSpecifier: CXXAccessSpecifierKind?
  • fullComment Extension method

    Given a cursor that represents a documentable entity (e.g., declaration), return the associated parsed comment

    Declaration

    Swift

    public var fullComment: FullComment?
  • rawComment Extension method

    Given a cursor that represents a declaration, return the associated comment text, including comment markers.

    Declaration

    Swift

    public var rawComment: String?
  • briefComment Extension method

    Given a cursor that represents a documentable entity (e.g., declaration), return the associated \brief paragraph; otherwise return the first paragraph.

    Declaration

    Swift

    public var briefComment: String?
  • language Extension method

    Determine the language of the entity referred to by a given cursor.

    Declaration

    Swift

    public var language: Language?
  • evaluate() Extension method

    If cursor is a statement declaration tries to evaluate the statement and if its variable, tries to evaluate its initializer, into its corresponding type.

    Declaration

    Swift

    public func evaluate() -> EvalResult?
  • visitChildren(_:) Extension method

    Visits each of the children of this cursor, calling the provided callback for each child.

    Note

    The returned value of this callback defines what the next item visited will be. See ChildVisitResult for a list of possible results.

    Declaration

    Swift

    public func visitChildren(_ perCursorCallback: (Cursor) -> ChildVisitResult)

    Parameters

    perCursorCallback

    The callback to call with each child in the receiver.