Class: Clang::Rbind::Cursor

Inherits:
FFI::Struct
  • Object
show all
Extended by:
Rbind::Logger
Defined in:
lib/rbind/clang/clang.rb,
lib/rbind/clang/clang_types.rb

Overview

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. clang_getTranslationUnitCursor() produces a cursor for a translation unit, from which one can use clang_visitChildren() to explore the rest of the translation unit. clang_getCursor() 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.

Fields:

:kind

(Symbol from enum_cursor_kind)

:xdata

(Integer)

:data

(Array<FFI::Pointer(*Void)>)

Instance Attribute Summary

Attributes included from Rbind::Logger

#log

Instance Method Summary collapse

Methods included from Rbind::Logger

extend_object

Instance Method Details

#columnObject



120
121
122
# File 'lib/rbind/clang/clang_types.rb', line 120

def column
    location[2]
end

#complete?Boolean

Returns:



224
225
226
# File 'lib/rbind/clang/clang_types.rb', line 224

def complete?
    !incomplete?
end

#cxx_access_specifierObject



136
137
138
# File 'lib/rbind/clang/clang_types.rb', line 136

def cxx_access_specifier
    Rbind::get_cxx_access_specifier self
end

#definitionObject



232
233
234
# File 'lib/rbind/clang/clang_types.rb', line 232

def definition
    Rbind::get_cursor_definition self
end

#display_nameObject



196
197
198
# File 'lib/rbind/clang/clang_types.rb', line 196

def display_name
    Rbind::get_cursor_display_name(self).to_s
end

#expressionObject



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rbind/clang/clang_types.rb', line 171

def expression
    num = FFI::MemoryPointer.new(:uint,1)
    tokens = FFI::MemoryPointer.new(:pointer,1)
    tu = translation_unit
    Rbind::tokenize(tu,extent,tokens,num)
    ptr = FFI::Pointer.new(Token,tokens.read_pointer)
    result = 0.upto(num.read_uint-1).map do |i|
        Rbind::get_token_spelling(tu,ptr[i])
    end
    Rbind::dispose_tokens(tu,ptr,num.get_uint(0))
    result
end

#extentObject



140
141
142
# File 'lib/rbind/clang/clang_types.rb', line 140

def extent
    Rbind::get_cursor_extent self
end

#file_nameObject



112
113
114
# File 'lib/rbind/clang/clang_types.rb', line 112

def file_name
    location[0]
end

#incomplete?Boolean

Returns:



228
229
230
# File 'lib/rbind/clang/clang_types.rb', line 228

def incomplete?
    definition.null?
end

#invalid_file?Boolean

Returns:



167
168
169
# File 'lib/rbind/clang/clang_types.rb', line 167

def invalid_file?
    kind == :invalid_file
end

#kindObject



200
201
202
# File 'lib/rbind/clang/clang_types.rb', line 200

def kind
    Rbind::get_cursor_kind(self)
end

#kind_spellingObject



188
189
190
# File 'lib/rbind/clang/clang_types.rb', line 188

def kind_spelling
    Rbind::get_cursor_kind_spelling(self).to_s
end

#lineObject



116
117
118
# File 'lib/rbind/clang/clang_types.rb', line 116

def line
    location[1]
end

#locationObject



94
95
96
97
98
99
100
101
102
# File 'lib/rbind/clang/clang_types.rb', line 94

def location
    line = FFI::MemoryPointer.new(:uint,1)
    col = FFI::MemoryPointer.new(:uint,1)
    location = Rbind::get_cursor_location(self)
    fstr = String.new
    Rbind::get_presumed_location(location,fstr,line,col)
    result = [fstr.to_s,line.get_uint(0),col.get_uint(0)]
    result
end

#location_intObject



104
105
106
# File 'lib/rbind/clang/clang_types.rb', line 104

def location_int
    Rbind::get_cursor_location(self)[:int_data]
end

#namespaceObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rbind/clang/clang_types.rb', line 148

def namespace
    namespace = []
    cursor = semantic_parent
    while !cursor.translation_unit?
        namespace << cursor.spelling
        cursor = cursor.semantic_parent
        if cursor.invalid_file?
            Cursor.log.warn "Cursor #{self} with invalid file"
            break
        end
    end
    namespace = namespace.select{|s| !s.empty?}
    namespace.reverse.join("::")
end

#null?Boolean

Returns:



90
91
92
# File 'lib/rbind/clang/clang_types.rb', line 90

def null?
    1 == Rbind::cursor_is_null(self)
end

#private?Boolean

Returns:



124
125
126
# File 'lib/rbind/clang/clang_types.rb', line 124

def private?
    cxx_access_specifier == :x_private
end

#protected?Boolean

Returns:



132
133
134
# File 'lib/rbind/clang/clang_types.rb', line 132

def protected?
    cxx_access_specifier == :x_protected
end

#public?Boolean

Returns:



128
129
130
# File 'lib/rbind/clang/clang_types.rb', line 128

def public?
    cxx_access_specifier == :x_public
end

#referencedObject



108
109
110
# File 'lib/rbind/clang/clang_types.rb', line 108

def referenced
    Rbind::get_cursor_referenced self
end

#result_typeObject



184
185
186
# File 'lib/rbind/clang/clang_types.rb', line 184

def result_type
    Rbind::get_cursor_result_type self
end

#semantic_parentObject



144
145
146
# File 'lib/rbind/clang/clang_types.rb', line 144

def semantic_parent
    Rbind::get_cursor_semantic_parent self
end

#specialized_templateObject



216
217
218
# File 'lib/rbind/clang/clang_types.rb', line 216

def specialized_template
    Rbind::get_specialized_cursor_template self
end

#spellingObject



192
193
194
# File 'lib/rbind/clang/clang_types.rb', line 192

def spelling
    Rbind::get_cursor_spelling(self).to_s
end

#static?Boolean

Returns:



236
237
238
# File 'lib/rbind/clang/clang_types.rb', line 236

def static?
    1 == Rbind::cxx_method_is_static(self)
end

#template_kindObject



220
221
222
# File 'lib/rbind/clang/clang_types.rb', line 220

def template_kind
    Rbind::get_template_cursor_kind self
end

#translation_unitObject



204
205
206
# File 'lib/rbind/clang/clang_types.rb', line 204

def translation_unit
    Rbind::cursor_get_translation_unit(self)
end

#translation_unit?Boolean

Returns:



163
164
165
# File 'lib/rbind/clang/clang_types.rb', line 163

def translation_unit?
    kind == :translation_unit
end

#typeObject



208
209
210
# File 'lib/rbind/clang/clang_types.rb', line 208

def type
    Rbind::get_cursor_type self
end

#virtual?Boolean

Returns:



240
241
242
# File 'lib/rbind/clang/clang_types.rb', line 240

def virtual?
    1 == Rbind::cxx_method_is_virtual(self)
end

#virtual_base?Boolean

Returns:



212
213
214
# File 'lib/rbind/clang/clang_types.rb', line 212

def virtual_base?
    1 == Rbind::is_virtual_base(self)
end

#visit_children(recurse = false, *reg_filters, &block) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/rbind/clang/clang_types.rb', line 244

def visit_children(recurse=false,*reg_filters,&block)
    if reg_filters.empty?
        reg_filters << Regexp.new("#{File.dirname(translation_unit.spelling)}")
    end
    p = proc do |cur,parent,data|
      #  puts "#{cur.kind} #{cur.spelling} #{cur.template_kind} #{cur.specialized_template.kind} #{cur.location}"
        if(reg_filters.find{|reg| cur.file_name =~ reg})
            block.call(cur,parent)
            if recurse
                :recurse
            else
                :continue
            end
        else
            :continue
        end
    end
    Rbind::visit_children(self,p,FFI::Pointer.new(0))
end