carbon-core 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +17 -0
  3. data/.gitignore +0 -0
  4. data/.rspec +0 -0
  5. data/.rubocop.yml +0 -0
  6. data/.travis.yml +0 -0
  7. data/.yardopts +0 -0
  8. data/CODE_OF_CONDUCT.md +0 -0
  9. data/Gemfile +0 -0
  10. data/LICENSE.txt +0 -0
  11. data/README.md +0 -0
  12. data/Rakefile +0 -0
  13. data/Vagrantfile +0 -0
  14. data/carbon.gemspec +0 -0
  15. data/lib/carbon.rb +25 -1
  16. data/lib/carbon/concrete.rb +4 -2
  17. data/lib/carbon/concrete/build.rb +21 -13
  18. data/lib/carbon/concrete/index.rb +53 -159
  19. data/lib/carbon/concrete/item.rb +0 -0
  20. data/lib/carbon/concrete/item/base.rb +14 -27
  21. data/lib/carbon/concrete/item/class.rb +71 -0
  22. data/lib/carbon/concrete/item/class/element.rb +42 -0
  23. data/lib/carbon/concrete/item/data.rb +0 -0
  24. data/lib/carbon/concrete/item/function.rb +35 -39
  25. data/lib/carbon/concrete/item/internal.rb +17 -19
  26. data/lib/carbon/concrete/item/struct.rb +12 -7
  27. data/lib/carbon/concrete/item/struct/element.rb +0 -0
  28. data/lib/carbon/concrete/item/trait.rb +9 -10
  29. data/lib/carbon/concrete/item/trait/expectation.rb +0 -0
  30. data/lib/carbon/concrete/request.rb +137 -136
  31. data/lib/carbon/concrete/type.rb +126 -21
  32. data/lib/carbon/concrete/type/function.rb +26 -10
  33. data/lib/carbon/concrete/type/generic.rb +19 -3
  34. data/lib/carbon/concrete/type/name.rb +0 -0
  35. data/lib/carbon/concrete/type/parse.rb +1 -0
  36. data/lib/carbon/concrete/type/part.rb +9 -1
  37. data/lib/carbon/core.rb +6 -1
  38. data/lib/carbon/core/int.rb +0 -0
  39. data/lib/carbon/core/integer.rb +0 -0
  40. data/lib/carbon/core/integer/cast.rb +0 -0
  41. data/lib/carbon/core/integer/math.rb +0 -0
  42. data/lib/carbon/core/integer/misc.rb +17 -1
  43. data/lib/carbon/core/integer/pole.rb +0 -0
  44. data/lib/carbon/core/integer/ship.rb +8 -4
  45. data/lib/carbon/core/integer/sign.rb +0 -0
  46. data/lib/carbon/core/integer/type.rb +0 -0
  47. data/lib/carbon/core/integer/zero.rb +0 -0
  48. data/lib/carbon/core/main.rb +50 -0
  49. data/lib/carbon/core/pointer.rb +0 -0
  50. data/lib/carbon/core/pointer/access.rb +1 -1
  51. data/lib/carbon/core/pointer/cast.rb +0 -0
  52. data/lib/carbon/core/pointer/math.rb +14 -0
  53. data/lib/carbon/core/pointer/memory.rb +2 -4
  54. data/lib/carbon/core/pointer/type.rb +0 -0
  55. data/lib/carbon/core/void.rb +20 -0
  56. data/lib/carbon/counter.rb +27 -0
  57. data/lib/carbon/errors.rb +13 -0
  58. data/lib/carbon/tacky.rb +0 -0
  59. data/lib/carbon/tacky/block.rb +0 -0
  60. data/lib/carbon/tacky/builder.rb +3 -6
  61. data/lib/carbon/tacky/context.rb +4 -10
  62. data/lib/carbon/tacky/function.rb +0 -24
  63. data/lib/carbon/tacky/instruction.rb +2 -5
  64. data/lib/carbon/tacky/instruction/generation.rb +19 -41
  65. data/lib/carbon/tacky/parameter.rb +0 -0
  66. data/lib/carbon/tacky/reference.rb +0 -0
  67. data/lib/carbon/tacky/typed.rb +0 -0
  68. data/lib/carbon/tacky/value.rb +0 -2
  69. data/lib/carbon/version.rb +1 -1
  70. data/scripts/core.rb +0 -0
  71. data/scripts/test.rb +5 -7
  72. metadata +9 -4
  73. data/lib/carbon/tacky/instruction/dependencies.rb +0 -33
  74. data/lib/carbon/tacky/instruction/typeof.rb +0 -25
@@ -9,7 +9,6 @@ module Carbon
9
9
  # function, the generics associated with the function, and the parameters
10
10
  # of the function.
11
11
  #
12
- # @todo TODO: add generic parameters.
13
12
  # @note
14
13
  # **This class is frozen upon initialization.** This means that any
15
14
  # attempt to modify it will result in an error. In most cases, the
@@ -26,7 +25,7 @@ module Carbon
26
25
  # @api public
27
26
  # @example
28
27
  # type = Carbon::Type("A.+(A, B)")
29
- # func = type.name.function
28
+ # func = type.function
30
29
  # func.name # => "+"
31
30
  # @return [::String] The name of the function.
32
31
  attr_reader :name
@@ -38,23 +37,35 @@ module Carbon
38
37
  # @api public
39
38
  # @example
40
39
  # type = Carbon::Type("A.+(A, B)")
41
- # func = type.name.function
40
+ # func = type.function
42
41
  # func.parameters
43
- # # => [#<Carbon::Concrete::Type::Name A>,
44
- # # #<Carbon::Concrete::Type::Name B>]
45
- # @return [::Array<Type::Name>]
42
+ # # => [#<Carbon::Concrete::Type A>,
43
+ # # #<Carbon::Concrete::Type B>]
44
+ # @return [::Array<Type>]
46
45
  attr_reader :parameters
47
46
 
47
+ # The generics used in the function definition.
48
+ #
49
+ # @api public
50
+ # @example
51
+ # type = Carbon::Type("A.+<T>(A, T)")
52
+ # func = type.function
53
+ # func.generics
54
+ # # => [#<Carbon::Concrete::Type::Generic T>]
55
+ # @return [::Array<Type>]
56
+ attr_reader :generics
57
+
48
58
  # Initialize the function.
49
59
  #
50
60
  # @see #name
51
61
  # @see #params
52
62
  # @param name [::String] The name of the function.
53
- # @param params [::Array<Type::Name>] The parameters of the function.
63
+ # @param params [::Array<Type>] The parameters of the function.
54
64
  # This is frozen before stored.
55
- def initialize(name, params)
56
- @name = name
65
+ def initialize(name, params, generics = [])
66
+ @name = name.to_s
57
67
  @parameters = params
68
+ @generics = generics
58
69
  deep_freeze!
59
70
  end
60
71
 
@@ -68,7 +79,12 @@ module Carbon
68
79
  # func.to_s # => "+(Test<T>)"
69
80
  # @return [::String]
70
81
  def to_s
71
- "#{@name}(#{@parameters.map(&:to_s).join(', ')})".freeze
82
+ if @generics.any?
83
+ "#{@name}<#{@generics.map(&:to_s).join(',')}>" \
84
+ "(#{@parameters.map(&:to_s).join(', ')})".freeze
85
+ else
86
+ "#{@name}(#{@parameters.map(&:to_s).join(', ')})".freeze
87
+ end
72
88
  end
73
89
 
74
90
  # Returns the interned version of this function. In contrast to
@@ -41,6 +41,13 @@ module Carbon
41
41
  # @return [Set<Type>] The traits the generic has to implement.
42
42
  attr_reader :implements
43
43
 
44
+ # The location of the type. This is used for interfacing with other
45
+ # programs that require a location of some sort.
46
+ #
47
+ # @api semiprivate
48
+ # @return [Object]
49
+ attr_reader :location
50
+
44
51
  # Initialize the generic part of the type.
45
52
  #
46
53
  # @see #name
@@ -48,7 +55,8 @@ module Carbon
48
55
  # @param name [Type] The name of the generic.
49
56
  # @param implements [Set<Type>] The traits the generic must
50
57
  # implement, if any.
51
- def initialize(name, implements)
58
+ def initialize(name, implements, location: nil)
59
+ @location = location
52
60
  @name = name
53
61
  @implements = Set.new(implements)
54
62
  deep_freeze!
@@ -67,12 +75,12 @@ module Carbon
67
75
  # other.is_a?(Generic) # => true
68
76
  # result = generic.sub("T" => other)
69
77
  # result.to_s # => "Carbon::String: Carbon::Sized"
70
- # @param mapping [{::String => Type::Generic}] The mapping.
78
+ # @param mapping [{::String => Type}] The mapping.
71
79
  # @return [Type::Generic] A different instance of the generic, if it's
72
80
  # in the mapping.
73
81
  # @return [self] otherwise.
74
82
  def sub(mapping)
75
- mapping.fetch(@name.intern, self)
83
+ Generic.new(mapping.fetch(@name, @name), @implements)
76
84
  end
77
85
 
78
86
  # Compares this generic instance to another generic instance. If the
@@ -112,6 +120,14 @@ module Carbon
112
120
  @name.to_s
113
121
  end
114
122
  end
123
+
124
+ # Accepts the current visitor unto itself.
125
+ #
126
+ # @param visitor [#visit]
127
+ # @return [Object]
128
+ def accept(visitor, *params)
129
+ visitor.visit(self, *params)
130
+ end
115
131
  end
116
132
  end
117
133
  end
File without changes
@@ -135,6 +135,7 @@ module Carbon
135
135
  | \-
136
136
  | \* | \/
137
137
  | % | & | \|
138
+ | \[\]=
138
139
  | \[\]
139
140
  | \^
140
141
  )
@@ -36,15 +36,23 @@ module Carbon
36
36
  # @return [<Type::Generic>] The generics.
37
37
  attr_reader :generics
38
38
 
39
+ # The location of the type. This is used for interfacing with other
40
+ # programs that require a location of some sort.
41
+ #
42
+ # @api semiprivate
43
+ # @return [Object]
44
+ attr_reader :location
45
+
39
46
  # Initialize the part with the given value and generics.
40
47
  #
41
48
  # @see #value
42
49
  # @see #generics
43
50
  # @param value [::String] The value.
44
51
  # @param generics [<Type::Generic>] The generics.
45
- def initialize(value, generics)
52
+ def initialize(value, generics, location: nil)
46
53
  @value = value
47
54
  @generics = generics
55
+ @location = location
48
56
  deep_freeze!
49
57
  end
50
58
 
data/lib/carbon/core.rb CHANGED
@@ -40,16 +40,20 @@ module Carbon
40
40
  # @api private
41
41
  # @return [void]
42
42
  def self.finalize
43
+ @index = Concrete::Index.new
43
44
  Core::Integer.define_integer
44
45
  Core::Pointer.define_pointer
45
46
  index.finalize
46
47
  end
47
48
 
49
+ def self.define_main(build, pass)
50
+ Core::Main.define_main_function(build.module, build.index, pass)
51
+ end
52
+
48
53
  def self.find
49
54
  if PATH.exist?
50
55
  Concrete.load(PATH.read)
51
56
  else
52
- @index = Concrete::Index.new
53
57
  finalize.tap { |i| PATH.write(Concrete.dump(i)) }
54
58
  end
55
59
  end
@@ -57,5 +61,6 @@ module Carbon
57
61
  require "carbon/core/int"
58
62
  require "carbon/core/integer"
59
63
  require "carbon/core/pointer"
64
+ require "carbon/core/main"
60
65
  end
61
66
  end
File without changes
File without changes
File without changes
File without changes
@@ -8,6 +8,7 @@ module Carbon
8
8
  # functions that don't fit in any of the other category. These functions
9
9
  # include:
10
10
  #
11
+ # - `.null(): self`
11
12
  # - `.next(self): self`
12
13
  # - `.succ(self): self`
13
14
  # - `.abs(self): self`
@@ -18,7 +19,7 @@ module Carbon
18
19
  # - `.size(self): Carbon::UInt32`
19
20
  #
20
21
  # These functions are defined on all of the integer types except boolean.
21
- # However, `.size` is also defined on boolean.
22
+ # However, `.size` and `.null` are also defined on boolean.
22
23
  #
23
24
  # @api private
24
25
  module Misc
@@ -40,6 +41,21 @@ module Carbon
40
41
  end
41
42
  end
42
43
 
44
+ # Defines the null function. This defines a function that returns a
45
+ # null (zero) integer value.
46
+ #
47
+ # @param int [Core::Int] The integer type.
48
+ # @return [void]
49
+ def define_null_function(int)
50
+ function_name = int.name.call(:null, [])
51
+ Core.define(function: function_name) do |function|
52
+ function[:return] = int.name
53
+ function[:definition].add("entry").build do |b|
54
+ b.ret(b._null(int.name).as(int.name))
55
+ end
56
+ end
57
+ end
58
+
43
59
  # Define the next functions for the given integer type. These
44
60
  # functions just return the given number incremented by one. This is
45
61
  # implemented without any calls. This function defines both the
File without changes
@@ -61,11 +61,15 @@ module Carbon
61
61
 
62
62
  def define_ship_definition_compare(params, blocks)
63
63
  blocks[0]
64
- .build { |b| b.br(b.icmp(:lt, *params).as(Carbon::Boolean),
65
- blocks[1], blocks[2]) }
64
+ .build do |b|
65
+ b.br(b.icmp(:lt, *params).as(Carbon::Boolean),
66
+ blocks[1], blocks[2])
67
+ end
66
68
  blocks[2]
67
- .build { |b| b.br(b.icmp(:eq, *params).as(Carbon::Boolean),
68
- blocks[3], blocks[4]) }
69
+ .build do |b|
70
+ b.br(b.icmp(:eq, *params).as(Carbon::Boolean),
71
+ blocks[3], blocks[4])
72
+ end
69
73
  end
70
74
  end
71
75
  end
File without changes
File without changes
File without changes
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Core
6
+ module Main
7
+ class << self
8
+ def define_main_function(mod, index, pass)
9
+ main_params = [LLVM::Int32.type, LLVM::Int8.type.pointer.pointer]
10
+ main = mod.functions.add("main", main_params, LLVM::Int32.type)
11
+ define_main_definition(mod, main, index, pass)
12
+ end
13
+
14
+ private
15
+
16
+ def define_main_definition(mod, func, index, pass)
17
+ if pass.function.parameters.size == 2
18
+ define_main_definition_params(mod, func, index, pass)
19
+ else
20
+ define_main_definition_noparams(mod, func, index, pass)
21
+ end
22
+ end
23
+
24
+ def define_main_definition_params(mod, func, index, pass)
25
+ argc, argv = func.params
26
+ argc.name, argv.name = "argc", "argv"
27
+ func.basic_blocks.append("entry").build do |b|
28
+ if index.fetch(pass).first.return != Carbon::Void
29
+ b.ret(b.call(mod.functions[pass.to_s], argc, argv))
30
+ else
31
+ b.call(mod.function[pass.to_s], argc, argv)
32
+ b.ret(LLVM::Int(0))
33
+ end
34
+ end
35
+ end
36
+
37
+ def define_main_definition_noparams(mod, func, index, pass)
38
+ func.basic_blocks.append("entry").build do |b|
39
+ if index.fetch(pass).first.return != Carbon::Void
40
+ b.ret(b.call(mod.functions[pass.to_s]))
41
+ else
42
+ b.call(mod.function[pass.to_s])
43
+ b.ret(LLVM::Int(0))
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
File without changes
@@ -84,7 +84,7 @@ module Carbon
84
84
 
85
85
  private
86
86
 
87
- def define_array_set_definition(int, definition)
87
+ def define_array_set_definition(_int, definition)
88
88
  entry = definition.add("entry").build
89
89
  this, index, value = definition.params
90
90
  this.name, index.name, value.name = %w(self index value)
File without changes
@@ -51,6 +51,15 @@ module Carbon
51
51
 
52
52
  COMP_OPERATIONS.each { |op| define_comp_pointer_function(op) }
53
53
  define_space_pointer_function
54
+ define_null_function
55
+ end
56
+
57
+ def define_null_function
58
+ function_name = PTYPE.call(:null, [])
59
+ Core.define(function: function_name) do |function|
60
+ function[:return] = PTYPE
61
+ define_null_definition(function[:definition])
62
+ end
54
63
  end
55
64
 
56
65
  # Defines the `<=>` function for two pointers. This returns `-1`,
@@ -183,6 +192,11 @@ module Carbon
183
192
 
184
193
  entry.ret(entry.gep(this, other).as(PTYPE))
185
194
  end
195
+
196
+ def define_null_definition(definition)
197
+ entry = definition.add("entry").build
198
+ entry.ret(entry.null(PTYPE).as(PTYPE))
199
+ end
186
200
  end
187
201
  end
188
202
  end
@@ -60,10 +60,8 @@ module Carbon
60
60
  size = definition.params[0]
61
61
  size.name = "size"
62
62
 
63
- element = entry.sizeof(PTYPEGEN).as(Carbon::Type("Carbon::UInt64"))
64
63
  size = convert_int_size(int, size, entry)
65
- total = entry.mul(element, size).as(size.type)
66
- entry.ret(entry.malloc(total).as(PTYPE))
64
+ entry.ret(entry.array_malloc(PTYPEGEN, size).as(PTYPE))
67
65
  end
68
66
 
69
67
  def define_free_definition(definition)
@@ -75,7 +73,7 @@ module Carbon
75
73
  end
76
74
 
77
75
  def convert_int_size(int, value, entry)
78
- size = Int.find(sign: :unsigned, size: 64)
76
+ size = Int.find(sign: :unsigned, size: 32)
79
77
  name = int.name.call(size.cast, [int.name])
80
78
  entry.call(name, value).as(size.name)
81
79
  end
File without changes
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ module Core
6
+ module Void
7
+ class << self
8
+ def define_void
9
+ define_void_type
10
+ end
11
+
12
+ def define_void_type
13
+ Core.define(internal: Carbon::Void) do |internal|
14
+ internal[:kind] = :void
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ # A mutable counter for giving out incremental ids.
6
+ #
7
+ # @api private
8
+ class Counter
9
+ # The value.
10
+ # @return [::Numeric]
11
+ attr_reader :value
12
+
13
+ # Initialize the counter.
14
+ #
15
+ # @param value [::Numeric] The initial value of the counter.
16
+ def initialize(value = 0)
17
+ @value = value
18
+ end
19
+
20
+ # Increments the counter by one, and returns the new value.
21
+ #
22
+ # @return [::Numeric]
23
+ def increment
24
+ @value += 1
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Carbon
5
+ class Error < StandardError
6
+ end
7
+
8
+ class TooManyItemsError < Error
9
+ end
10
+
11
+ class ItemNotFoundError < Error
12
+ end
13
+ end