blob: 471bdcb1f2f340a774c14d8b0d6d698ea1a49abb [file] [log] [blame]
Brian Silverman8efe23e2013-07-07 23:31:37 -07001begin
2 require "sha1"
3rescue LoadError
4 require "digest/sha1"
5end
6
Austin Schuh7e958392014-10-21 22:16:23 -07007$i = 0
8def getForLoopVar()
9 $i = $i + 1
10 return "_autogen_index_#{$i}"
11end
12
brians343bc112013-02-10 01:53:46 +000013module Target
14end
15class Target::Node
16 attr_accessor :created_by
Parker Schuh343481e2014-02-09 18:28:43 -080017 def get_name()
18 if(@parent)
19 return "#{@parent.get_name}.#{@name}"
20 else
21 return "#{@name}"
22 end
23 end
brians343bc112013-02-10 01:53:46 +000024end
25class Target::QFile < Target::Node
26 def initialize() #needs to know repo_path,
27 @class_types = []
28 end
29 def add_type(type)
30 @class_types << type
31 end
32 def extern=(value)
33 @class_types.each do |type|
34 type.extern=value
35 end
36 end
37 def make_cpp_tree(rel_path)
38 cpp_tree = DepFilePair.new(rel_path)
Austin Schuh7e958392014-10-21 22:16:23 -070039 cpp_tree.add_header_include("<array>")
brians343bc112013-02-10 01:53:46 +000040 cpp_tree.add_header_include("\"aos/common/macros.h\"")
41 cpp_tree.add_header_include("\"aos/common/queue.h\"")
42 @class_types.each do |type|
43 cpp_tree.get(type)
44 end
45 return cpp_tree
46 end
47end
48class Target::QInclude < Target::Node
49 def initialize(path)
50 @path = path
51 end
52 def create(cpp_tree)
53# inc = cpp_tree.header.add_include("\"#{@path}\"")
54 inc = cpp_tree.add_header_include("\"#{@path}\"")
55 cpp_tree.set(self,inc)
56 end
57end
58class Target::QueueGroupDec < Target::Node
59 attr_accessor :name,:loc,:parent,:queues
60 def initialize(name)
61 @name = name
62 @queues = []
63 @subs = {}
64 end
65 def root()
66 @parent.root()
67 end
68 def []=(key,val)
69 #puts "#{key}= #{val}"
70 @subs[key] = val
71 end
72 def extern=(value)
73 @extern=value
74 end
75 def get_name()
76 if(@parent)
77 return "#{@parent.get_name}.#{@name}"
78 else
79 return "#{@name}"
80 end
81 end
82 def to_cpp_id(id)
83 name = "#{@name}::#{id}"
84 return @parent.to_cpp_id(name) if(@parent)
85 return name
86 end
87 def [](key)
88 #puts "#{key}"
89 @subs[key]
90 end
91 def add_queue(queue)
92 @queues.push(queue)
93 end
94 def hash_with_name(name)
95 ts = (@queues.collect { |queue|
96 queue.msg_hash()
97 }).join("") + name
Brian Silverman8efe23e2013-07-07 23:31:37 -070098 return "0x#{Digest::SHA1.hexdigest(ts)[-8..-1]}"
brians343bc112013-02-10 01:53:46 +000099 end
100 def create(cpp_tree)
101 return if(@extern)
102 namespace = cpp_tree.get(@loc)
103 type_class = Types::Class.new(namespace,@name)
104 cpp_tree.set(self,type_class) #breaks infinite recursion
105 type_class.set_parent("public ::aos::QueueGroup")
106 @queues.each do |queue|
107 type_class.add_member(:public,queue.create_usage(cpp_tree))
brians343bc112013-02-10 01:53:46 +0000108 end
109 create_Constructor(type_class,cpp_tree)
110 namespace.add(type_class)
111 end
112 def create_Constructor(type_class,cpp_tree)
113 member_func = CPP::Constructor.new(type_class)
114 type_class.add_member(:public,member_func)
115 #cpp_tree.cc_file.add_funct(member_func)
116 member_func.args << "const char *name";
117 member_func.args << "uint32_t hash";
118 member_func.add_cons("::aos::QueueGroup","name", "hash")
119 @queues.each do |queue|
120 member_func.args << "const char *#{queue.name}_name";
121 member_func.add_cons(queue.name,"#{queue.name}_name")
122 end
123 end
124end
125class Target::QueueGroup < Target::Node
126 attr_accessor :name,:loc
127 def initialize(type,name)
128 @type,@name = type,name
129 end
130 def type_name()
131 if(@type.loc == @loc) #use relative name
132 return @type.name
133 else #use full name
134 return @type.loc.to_cpp_id(@type.name)
135 end
136 end
137 def create(cpp_tree)
138 namespace = cpp_tree.get(@loc)
139 type = cpp_tree.get(@type)
140 cpp_tree.set(self,self)
141 namespace.add_cc("static #{type_name} *#{@name}_ptr")
142 counter = "#{@name}_counter"
143 namespace.add_cc("static int #{counter}")
144
145 str = <<COMMENT_END
146Schwarz counter to construct and destruct the #{@name} object.
147Must be constructed before &#{@name} is constructed so that #{@name}_ptr
148is valid so we can store a reference to the object.
149COMMENT_END
150 str.split(/\n/).each do |str_sec|
151 namespace.class_comment(str_sec)
152 end
153 init_class = namespace.add_class("InitializerFor_" + @name).add_dep(type)
154
155
156 init_class.add_cc_comment(
157 "The counter is initialized at load-time, i.e., before any of the static",
158 "objects are initialized.")
159
160
161 cons = CPP::Constructor.new(init_class)
162 init_class.add_member(:public,cons)
163 cons.suite << if_stmt = CPP::If.new("0 == #{counter}++")
brians343bc112013-02-10 01:53:46 +0000164
165 cons_call = CPP::FuncCall.new("new #{type_name}")
166 cons_call.args.push(@loc.queue_name(@name).inspect)
167
168 cons_call.args.push(@type.hash_with_name(@loc.queue_name(@name).inspect))
169 @type.queues.collect do |queue|
170 cons_call.args.push(@loc.queue_name(@name + "." + queue.name).inspect)
171 end
172 if_stmt.suite << CPP::Assign.new("#{@name}_ptr",cons_call)
brians343bc112013-02-10 01:53:46 +0000173
174
175 destruct = CPP::Destructor.new(init_class)
176 destruct.suite << if_stmt = CPP::If.new("0 == --#{counter}")
brians343bc112013-02-10 01:53:46 +0000177 if_stmt.suite << "delete #{@name}_ptr"
178 if_stmt.suite << CPP::Assign.new("#{@name}_ptr","NULL")
179
180 init_class.add_member(:public,destruct)
181 init_class.static = true
182 init_class.dec = @name + "_initializer";
183
184 str = <<COMMENT_END
185Create a reference to the new object in the pointer. Since we have already
186created the initializer
187COMMENT_END
Brian Silverman04fdc232014-02-12 14:51:11 -0800188 str.split(/\n/).map{|str_sec| CPP::Comment.new(str_sec)}.each do |comment|
189 namespace.add(comment)
190 end
191 namespace.add("static UNUSED_VARIABLE #{type_name} &#{@name}" +
192 " = #{@name}_initializer.get()")
brians343bc112013-02-10 01:53:46 +0000193
194 get = init_class.def_func(type_name,"get") #.add_dep(type)
195 get.pre_func_types = "&"
196 get.suite << CPP::Return.new("*#{@name}_ptr")
197 end
198end