dictbuilder.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: sjis -*-
3 # -*- python -*-
4 #
5 # @file omniidl_be/doil/dictbuilder
6 # @brief Dictionary Builder class for doil backend
7 # @date $Date$
8 # @author Noriaki Ando <n-ando@aist.go.jp>
9 #
10 # Copyright (C) 2008
11 # Task-intelligence Research Group,
12 # Intelligent Systems Research Institute,
13 # National Institute of
14 # Advanced Industrial Science and Technology (AIST), Japan
15 # All rights reserved.
16 #
17 # $Id$
18 #
19 
20 """Produce Dictionary from IDL AST"""
21 
22 import string
23 
24 # module from omniidl
25 from omniidl import idlast, idlvisitor, idltype
26 
27 # module from omniidl_be/cxx
28 from omniidl_be.cxx import ast, id, types, output
29 import omniidl_be.cxx.id as corba_cxx_id
30 
31 # module from omniidl_be/doil
32 from omniidl_be.doil import util
33 
34 import omniidl_be.doil.yat as yat
35 
36 #import dictbuilder
37 
38 #self = dictbuilder
39 
40 def impl_fullname(name):
41  bits = name.suffix("Servant").fullName()
42  return string.join(bits, "_")
43 
44 # Main code entrypoint
45 def run(tree, config):
46  # first thing is to build the interface implementations
47  bsi = BuildDictionaryFromAST(tree, config)
48  tree.accept(bsi)
49  dict = bsi.get_dict()
50 # import yaml
51 # print yaml.dump(dict['tree'], default_flow_style=False)
52  return dict
53 
54 
55 
56 
57 # Build the interface implementations
58 #
59 class BuildDictionaryFromAST(idlvisitor.AstVisitor):
60  def get_dict(self):
61  return self.dict
62 
63  def __init__(self, tree, config):
64  # configuration parameters from command options
65  self.config = config
66 
67  # module's namespace stack
68  self.module = []
69 
70  # main dictionary
71  self.dict = {}
72  self.dict['tree'] = []
73 
74  # idl file name
75  idl_fname = ast.mainFile()
76  self.dict['idl_fname'] = idl_fname
77 
78  # included idl files
79  incs = []
80  idl_incs = ast.includes()
81  for inc in idl_incs:
82  d = self.createHeaderInfo(inc)
83  if inc == idl_fname:
84  self.dict.update(d)
85  elif self.config['ImplicitInclude']:
86  # -Wbimplicit option makes process included IDLs
87  d = self.createHeaderInfo(inc)
88  incs.append(d)
89  self.dict['idl_includes'] = incs
90 
91  # other includes
92  self.dict['include_h'] = self.config["IncludeHeaders"]
93 
94  # type mapping
95  self.typemap = self.config['TypeMapping']
96  # now configurations can be accessed by self.config["key"]
97 
98  # keep track of all interfaces for later use
99  self.__allInterfaces = []
100  self.tk_map = {
101  idltype.tk_null : "tk_null",
102  idltype.tk_void : "tk_void",
103  idltype.tk_short : "tk_short",
104  idltype.tk_long : "tk_long",
105  idltype.tk_ushort : "tk_ushort",
106  idltype.tk_ulong : "tk_ulong",
107  idltype.tk_float : "tk_float",
108  idltype.tk_double : "tk_double",
109  idltype.tk_boolean : "tk_boolean",
110  idltype.tk_char : "tk_char",
111  idltype.tk_octet : "tk_octet",
112  idltype.tk_any : "tk_any",
113  idltype.tk_TypeCode : "tk_TypeCode",
114  idltype.tk_Principal : "tk_Principal",
115  idltype.tk_objref : "tk_objref",
116  idltype.tk_struct : "tk_struct",
117  idltype.tk_union : "tk_union",
118  idltype.tk_enum : "tk_enum",
119  idltype.tk_string : "tk_string",
120  idltype.tk_sequence : "tk_sequence",
121  idltype.tk_array : "tk_array",
122  idltype.tk_alias : "tk_alias",
123  idltype.tk_except : "tk_except",
124  idltype.tk_longlong : "tk_longlong",
125  idltype.tk_ulonglong : "tk_ulonglong",
126  idltype.tk_longdouble : "tk_longdouble",
127  idltype.tk_wchar : "tk_wchar",
128  idltype.tk_wstring : "tk_wstring",
129  idltype.tk_fixed : "tk_fixed",
130  idltype.tk_value : "tk_value",
131  idltype.tk_value_box : "tk_value_box",
132  idltype.tk_native : "tk_native",
133  idltype.tk_abstract_interface : "tk_abstract_interface",
134  idltype.tk_local_interface : "tk_local_interface"
135  }
136 
138  "CORBA::Short" : "short int",
139  "CORBA::UShort" : "unsigned short int",
140  "CORBA::Long" : "int",
141  "CORBA::ULong" : "unsigned int",
142  "CORBA::Float" : "float",
143  "CORBA::Double" : "double",
144  "CORBA::Char" : "char",
145  "CORBA::Boolean" : "bool",
146  "char*" : "::std::string",
147  "CORBA::Any" : "::std::string",
148  "CORBA::TypeCode_ptr" : "::std::string"
149  }
150 
151  def createHeaderInfo(self, idl_path):
152  dict = {}
153  idl_path_list = idl_path.split('/')
154  idl_fname = idl_path_list[-1]
155 
156  dict['idl_fname'] = idl_fname
157  dict['idl_fname_path'] = idl_path
158 
159  # types.h
160  base_name = idl_fname.split('.')[0]
161  inc_guard = base_name.upper() + 'TYPES_H'
162  dict['types_h'] = types_h = base_name + 'Types.h'
163  dict['typeconv_h'] = base_name + 'TypeConversion.h'
164  dict['typeconv_cpp'] = base_name + 'TypeConversion.cpp'
165  dict['types_include_guard'] = inc_guard
166  if self.config['IfaceDir'] != "":
167  inc_types_h_path = self.config['IfaceDir'] \
168  + '/' + types_h
169  else:
170  inc_types_h_path = types_h
171  dict['types_h_path'] = inc_types_h_path
172 
173  # typeconv.h
174  dict['typeconv_h'] = typeconv_h = base_name + 'TypeConversion.h'
175  dict['typeconv_cpp'] = base_name + 'TypeConversion.cpp'
176  tc_inc_guard = base_name.upper() + 'TYPECONVERSION_H'
177  dict['typeconv_include_guard'] = tc_inc_guard
178  if self.config['ServantDir'] != "":
179  inc_typeconv_h_path = self.config['ServantDir'] \
180  + '/' + typeconv_h
181  else:
182  inc_typeconv_h_path = typeconv_h
183  dict['typeconv_h_path'] = inc_typeconv_h_path
184  return dict
185 
186 
187 
188  def createDecl(self, decl_type):
189  """
190  宣言情報の基本ディクショナリの生成
191 
192  decl_type: 宣言のタイプ, struct, interface, union など corba: decl_type: 宣言のタイプ, struct, interface, union など corba_ns: [] ネームスペースのリスト local: decl_type: 宣言のタイプ, struct, interface, union など local_ns: [] ローカルインターフェースのネームスペース adapter_ns: [] アダプタのネームスペース servant_ns: [] サーバントのネームスペース
193  corba:
194  decl_type: 宣言のタイプ, struct, interface, union など corba_ns: [] ネームスペースのリスト local: decl_type: 宣言のタイプ, struct, interface, union など local_ns: [] ローカルインターフェースのネームスペース adapter_ns: [] アダプタのネームスペース servant_ns: [] サーバントのネームスペース
195  corba_ns: [] ネームスペースのリスト
196  local:
197  decl_type: 宣言のタイプ, struct, interface, union など local_ns: [] ローカルインターフェースのネームスペース adapter_ns: [] アダプタのネームスペース servant_ns: [] サーバントのネームスペース
198  local_ns: [] ローカルインターフェースのネームスペース
199  adapter_ns: [] アダプタのネームスペース
200  servant_ns: [] サーバントのネームスペース
201  """
202  cdict = {'decl_type': decl_type}
203  ldict = {'decl_type': decl_type}
204  cdict['corba_ns'] = self.module
205  ldict['iface_ns'] = self.module + self.config['IfaceNs']
206  ldict['adapter_ns'] = self.module + self.config['AdapterNs']
207  ldict['proxy_ns'] = self.module + self.config['ProxyNs']
208  ldict['servant_ns'] = self.module + self.config['ServantNs']
209  return {'decl_type': decl_type, 'corba': cdict, 'local': ldict}
210 
211 
212  def getType(self, typeobj):
213  """
214  CORBA と Local の型名を取得する
215  """
216  corba_type = typeobj.base()
217  # for omniidl4 4.1.1-2
218  if corba_type[:2] == "::":
219  corba_type = corba_type[2:]
220  is_primitive = None
221  # if CORBA to Local mapping is specified explicitly
222  if self.typemap.has_key(corba_type):
223  local_type = self.typemap[corba_type]
224 
225  # if CORBA type is primitive, string or Any
226  elif self.corba_primitive.has_key(corba_type):
227  local_type = self.corba_primitive[corba_type]
228  if corba_type[:5] == 'CORBA':
229  corba_type = '::' + corba_type
230  tk = self.tk_map[typeobj.kind()]
231  primitive = ["tk_short", "tk_long", "tk_ushort",
232  "tk_ulong", "tk_float", "tk_double",
233  "tk_boolean", "tk_char", "tk_octet"]
234  if primitive.count(tk) > 0:
235  is_primitive = 'YES'
236 
237 
238  # other case
239  else:
240  corba_scoped_type = corba_type.split('::')
241  corba_ns = corba_scoped_type[:-1]
242  corba_base = corba_scoped_type[-1]
243  local_ns = corba_ns + self.config['IfaceNs']
244  local_scope = string.join(local_ns, '::')
245  if typeobj.objref():
246  corba_base = corba_base[:corba_base.rfind('_ptr')]
247  local_type = local_scope + '::' + \
248  self.config['IfacePrefix'] + \
249  corba_base + \
250  self.config['IfaceSuffix']
251  elif typeobj.sequence():
252  seqType = types.Type(typeobj.type().seqType())
253  # get type of element of sequence
254  (corba_etype, local_etype, eis_primitive) = self.getType(seqType)
255  if seqType.objref():
256  local_etype = local_etype + '*'
257  local_type = "std::vector< " + local_etype + " >"
258 
259  else:
260  local_type = local_scope + '::' + corba_base
261  corba_type = '::' + corba_type
262  local_type = '::' + local_type
263  return (corba_type, local_type, is_primitive)
264 
265 
266  def createIdent(self, dict, node):
267  """
268  宣言の識別子に関するディクショナリの生成
269  主に、識別子のIDL、C++、Local名を生成する createDeclで生成したディクショナリとnodeを引数に取る corba: idl_name: 宣言のidl上の識別子 name: C++にマッピングされた識別子 name_fq: C++識別子の完全修飾名 scoped_name: [] リスト形式の完全修飾名 local: idl_name: 宣言のidl上の識別子 name: C++にマッピングされた識別子 name_fq: C++識別子の完全修飾名 scoped_name: [] リスト形式の完全修飾名
270  createDeclで生成したディクショナリとnodeを引数に取る corba: idl_name: 宣言のidl上の識別子 name: C++にマッピングされた識別子 name_fq: C++識別子の完全修飾名 scoped_name: [] リスト形式の完全修飾名 local: idl_name: 宣言のidl上の識別子 name: C++にマッピングされた識別子 name_fq: C++識別子の完全修飾名 scoped_name: [] リスト形式の完全修飾名
271 
272  corba:
273  idl_name: 宣言のidl上の識別子
274  name: C++にマッピングされた識別子
275  name_fq: C++識別子の完全修飾名
276  scoped_name: [] リスト形式の完全修飾名
277  local:
278  idl_name: 宣言のidl上の識別子
279  name: C++にマッピングされた識別子
280  name_fq: C++識別子の完全修飾名
281  scoped_name: [] リスト形式の完全修飾名
282 
283  """
284  cdict = dict['corba']
285  ldict = dict['local']
286 
287  cdict['idl_name'] = idl_name = node.identifier()
288  cdict['name'] = cxx_name = id.mapID(idl_name)
289  ns = node.scopedName()[:-1]
290  cdict['corba_ns'] = ns
291  ldict['iface_ns'] = ns + self.config['IfaceNs']
292  ldict['adapter_ns'] = ns + self.config['AdapterNs']
293  ldict['proxy_ns'] = ns + self.config['ProxyNs']
294  ldict['servant_ns'] = ns + self.config['ServantNs']
295  cxx_fq_name = id.Name(node.scopedName()).fullyQualify()
296  cdict['name_fq'] = '::' + cxx_fq_name
297  cdict['scoped_name'] = node.scopedName()
298 
299  iface_ns = '::' + string.join(ldict['iface_ns'], '::')
300 
301  if self.typemap.has_key(cxx_fq_name):
302  local_fq_name = self.typemap[cxx_fq_name]
303  local_name = local_fq_name.split('::')[-1]
304  elif self.corba_primitive.has_key(cxx_fq_name):
305  local_fq_name = self.corba_primitive[cxx_fq_name]
306  local_name = local_fq_name
307  else:
308  local_name = cxx_name
309  local_fq_name = iface_ns + '::' + local_name
310 
311  ldict['name'] = local_name
312  ldict['name_fq'] = local_fq_name
313  ldict['scoped_name'] = ldict['iface_ns'] + [local_name]
314  return dict
315 
316 
317  def createInterfaceIdent(self, dict, node):
318  """
319  インターフェース宣言の識別子に関するディクショナリの生成
320  interface/servant/adapter 名を作成しディクショナリに追加する corba: name_poa: CORBA POAクラス名 local: iface_name: Interfaceの識別子 iface_name_fq: Interfaceの完全修飾名 iface_scoped_name: Interfaceのリスト形式完全修飾名 servant_name: Servantの識別子 servant_name_fq: Servantの完全修飾名 servant_scoped_name: Servantのリスト形式完全修飾名 adapter_name: Adapterの識別子 adapter_name_fq: Adapterの完全修飾名 adapter_scoped_name: Adapterのリスト形式完全修飾名
321 
322  corba:
323  name_poa: CORBA POAクラス名
324  local:
325  iface_name: Interfaceの識別子
326  iface_name_fq: Interfaceの完全修飾名
327  iface_scoped_name: Interfaceのリスト形式完全修飾名
328  servant_name: Servantの識別子
329  servant_name_fq: Servantの完全修飾名
330  servant_scoped_name: Servantのリスト形式完全修飾名
331  adapter_name: Adapterの識別子
332  adapter_name_fq: Adapterの完全修飾名
333  adapter_scoped_name: Adapterのリスト形式完全修飾名
334 
335  """
336  self.createIdent(dict, node)
337  cdict = dict['corba']
338  ldict = dict['local']
339  cdict['name_poa'] = '::POA_' + cdict['name_fq'].strip(':')
340 
341  # set iface_name, servant_name adapter_name
342  name = ldict['name']
343  p = "Prefix"
344  s = "Suffix"
345  n = "Ns"
346  for t in ['Iface', 'Servant', 'Adapter', 'Proxy']:
347  #for t in ['Iface', 'Servant', 'Adapter']:
348  key = t.lower() + '_name'
349  local_name = self.config[t + p] + name + self.config[t + s]
350  scoped_name = cdict['corba_ns'] + self.config[t + n] + [local_name]
351  local_fq_name = '::' + string.join(scoped_name, '::')
352  ldict[key] = local_name
353  ldict[key + '_fq'] = local_fq_name
354  ldict[key + '_scoped_name'] = scoped_name
355  return dict
356 
357 
358  def createInterfaceFileInfo(self, dict, node):
359  """
360  インターフェース関連ファイル名のディクショナリの生成
361 
362  local:
363  iface_h: Interfaceヘッダファイル名
364  iface_cpp: Interface実装ファイル名
365  iface_h_path: Interfaceヘッダのインクルードパス
366  iface_include_guard: Interfaceヘッダののインクルードガード
367  servant_h: Servantヘッダファイル名
368  servant_cpp: Servant実装ファイル名
369  servant_h_path: Servantヘッダのインクルードパス
370  servant_include_guard: Servantヘッダののインクルードガード
371  adapter_h: Adapterヘッダファイル名
372  adapter_cpp: Adapter実装ファイル名
373  adapter_h_path: Adapterヘッダのインクルードパス
374  adapter_include_guard: Adapterヘッダののインクルードガード
375 
376  """
377  cdict = dict['corba']
378  ldict = dict['local']
379  ldict['include_h'] = self.config["IncludeHeaders"]
380 
381  # set [iface|servant|adapter]_[h|cpp|h_path|include_guard]
382 
383  for t in ['Iface', 'Servant', 'Adapter', 'Proxy']:
384  #for t in ['Iface', 'Servant', 'Adapter']:
385  k = t.lower()
386  ldict[k + '_h'] = ldict[k + '_name'] + ".h"
387  ldict[k + '_cpp'] = ldict[k + '_name'] + ".cpp"
388  if self.config[t + 'Dir'] == '':
389  ldict[k + '_h_path'] = ldict[k + '_h']
390  else:
391  ldict[k + '_h_path'] = \
392  self.config[t + 'Dir'] + '/' + ldict[k + '_h']
393  ns = string.join(map(lambda x: x + '_',
394  ldict[k + '_ns']), '')
395  ns = ns.upper()
396  name = ldict[k + '_name'].upper()
397  ldict[k + '_include_guard'] = ns + name + "_H"
398  return dict
399 
400 
401  def createUnionIdent(self, dict, node):
402  """
403  共用体宣言のの識別子に関するディクショナリの生成
404 
405  corba:
406  idl_name: 宣言のidl上の識別子
407  name: C++にマッピングされた識別子
408  name_fq: C++識別子の完全修飾名
409  scoped_name: [] リスト形式の完全修飾名
410  switch_type: switchの型
411  switch_fq_type: switchの完全修飾型
412  deref_switch_type: switchの非参照型
413  local:
414  idl_name: 宣言のidl上の識別子
415  name: C++にマッピングされた識別子
416  name_fq: C++識別子の完全修飾名
417  scoped_name: [] リスト形式の完全修飾名
418 
419  """
420  self.createIdent(dict, node)
421  cdict = dict['corba']
422  ldict = dict['local']
423 
424  switchType = types.Type(node.switchType())
425  ast.markDefaultCase(node)
426  hasDefault = ast.defaultCase(node) != None
427 
428  (ctype, ltype, is_primitive) = self.getType(switchType)
429  cdict['switch_fq_type'] = ctype
430  cdict['switch_type'] = ctype.split('::')[-1]
431  ldict['switch_fq_type'] = ltype
432  ldict['switch_type'] = ltype.split('::')[-1]
433  return
434 
435 
436  def createStructIdent(self, dict, node):
437  return self.createIdent(dict, node)
438 
439 
440  def createEnumIdent(self, dict, node):
441  return self.createIdent(dict, node)
442 
443 
444  def createExceptionIdent(self, dict, node):
445  return self.createIdent(dict, node)
446 
447 
448  def createMembers(self, dict, node):
449  corba_name = dict['corba']['name']
450  outer_environment = id.lookup(node)
451  environment = outer_environment.enter(id.mapID(corba_name))
452  scope = environment.scope()
453 
454  members = []
455  for member in node.members():
456  #------------------------------------------------------------
457  # member
458  # - type_d: type of a member
459  # - decl_d: decralation list
460  memberType = types.Type(member.memberType())
461 
462 # self.createType(memberType, environment)
463  memtype = memberType.member(environment)
464  for decl in member.declarators():
465  m = self.createMember(decl, member, environment)
466  if m != None:
467  members.append(m)
468  dict['members'] = members
469  return dict
470 
471 
472  def createMember(self, decl, member, env):
473  dict = self.createDecl('member')
474  cdict = dict['corba']
475  ldict = dict['local']
476 
477  memberType = types.Type(member.memberType())
478  memtype = memberType.member(env)
479 
480  (ctype, ltype, is_primitive) = self.getType(memberType)
481  cdict['base_type'] = ctype
482  cdict['tk'] = self.tk_map[memberType.kind()]
483  ldict['base_type'] = ltype
484  cdict['member_name'] = id.mapID(decl.identifier())
485  cdict['member_dims'] = decl.sizes()
486  ldict['member_name'] = cdict['member_name']
487  ldict['member_dims'] = cdict['member_dims']
488 
489  if memberType.objref():
490  corba_mtype = ctype
491  local_mtype = ltype + '*'
492  else:
493  corba_mtype = ctype
494  local_mtype = ltype
495 
496  cdict['member_type'] = corba_mtype
497  ldict['member_type'] = local_mtype
498  return dict
499 
500 
501  def createUnionCases(self, dict, node):
502  cases = []
503  switchType = types.Type(node.switchType())
504  ast.markDefaultCase(node)
505  outer_environment = id.lookup(node)
506  environment = outer_environment.enter(node.identifier())
507 
508  for case in node.cases():
509  c = self.createUnionCase(case, node, switchType, environment)
510  if c != None:
511  cases.append(c)
512  dict['cases'] = cases
513  return dict
514 
515 
516  def createUnionCase(self, case, node, switchtype, environment):
517  dict = self.createDecl('union_case')
518  cdict = dict['corba']
519  ldict = dict['local']
520 
521  caseType = types.Type(case.caseType())
522  d_caseType = caseType.deref()
523  (corba_ctype, local_ctype, is_primitive) = self.getType(caseType)
524  cdict['case_type'] = corba_ctype
525  ldict['case_type'] = local_ctype
526 
527  decl = case.declarator()
528  case_member = id.mapID(decl.identifier())
529  cdict['case_member'] = case_member
530  ldict['case_member'] = case_member
531 
532  decl_dims = decl.sizes()
533  full_dims = decl_dims + caseType.dims()
534  is_array = full_dims != []
535  if is_array: raise "array union case type is not supported."
536 
537  # ------------------------------------------------------------
538  # generate default discriminator
539  def choose(switchType = switchtype,
540  values = ast.allCaseLabelValues(node),
541  environment = environment):
542  switchType = switchType.deref()
543  def min_unused(start, used = values):
544  x = start
545  while x in used:
546  x = x + 1
547  return x
548  kind = switchType.type().kind()
549  if switchType.integer():
550  (low, high) = ast.integer_type_ranges[kind]
551  s = switchType.literal(min_unused(low+1))
552  return s
553  elif kind == idltype.tk_char:
554  all = map(chr, range(0, 255))
555  elif kind == idltype.tk_boolean:
556  all = [0, 1]
557  elif kind == idltype.tk_enum:
558  all = switchType.type().decl().enumerators()
559  else:
560  util.fatalError("Failed to generate a default union " +\
561  "discriminator value")
562  possibles = util.minus(all, values)
563  return switchType.literal(possibles[0], environment)
564  # ------------------------------------------------------------
565 
566  labels = case.labels()
567  if labels != []:
568  non_default_labels = filter(lambda x:not x.default(), labels)
569  if non_default_labels == []:
570  # only one label and it's the default
571  label = labels[0]
572  discrimvalue = choose()
573  elif len(non_default_labels) > 1:
574  # oooh, we have a choice. Let's pick the second one.
575  # no-one will be expecting that
576  label = non_default_labels[1]
577  else:
578  # just the one interesting label
579  label = non_default_labels[0]
580 
581  if label.default():
582  discrimvalue = choose()
583  else:
584  discrimvalue = switchtype.literal(label.value(),
585  environment)
586  cdict['discriminator'] = discrimvalue
587  ldict['discriminator'] = discrimvalue
588 
589  if switchtype.enum():
590  corba_ns = '::' + string.join(cdict['corba_ns'], '::')
591  local_ns = '::' + string.join(ldict['iface_ns'], '::')
592  cdict['discriminator_fq'] = corba_ns + '::' + discrimvalue
593  ldict['discriminator_fq'] = local_ns + '::' + discrimvalue
594  else:
595  cdict['discriminator_fq'] = discrimvalue
596  ldict['discriminator_fq'] = discrimvalue
597  non_default_labels = filter(lambda x:not x.default(), labels)
598  return dict
599 
600 
601  def createTypedef(self, aliasType, decl, env):
602  """
603  typedef宣言に関するディクショナリの生成
604 
605  corba:
606  derived_type: 導出型名
607  derived_fq_type: 完全修飾導出型名
608  deref_type: 非参照型名
609  deref_fq_type: 完全修飾非参型名
610  tk: TypeCode
611  local:
612  derived_type: 導出型名
613  derived_fq_type: 完全修飾導出型名
614  deref_type: 非参照型名
615  deref_fq_type: 完全修飾非参型名
616  """
617 
618  dict = self.createDecl('typedef')
619  cdict = dict['corba']
620  ldict = dict['local']
621 
622  (cdict['base_type'], ldict['base_type'], is_primitive) = self.getType(aliasType)
623  derivedName = id.mapID(decl.identifier())
624  alias_dims = aliasType.dims()
625  cdict['derived_type'] = derivedName
626  ldict['derived_type'] = derivedName
627 
628  corba_ns = '::' + string.join(cdict['corba_ns'], '::')
629  local_ns = '::' + string.join(ldict['iface_ns'], '::')
630  cdict['derived_type_fq'] = corba_ns + '::' + derivedName
631  ldict['derived_type_fq'] = local_ns + '::' + derivedName
632  cdict['tk'] = tk = self.tk_map[aliasType.kind()]
633  primitive = ["tk_short", "tk_long", "tk_ushort",
634  "tk_ulong", "tk_float", "tk_double",
635  "tk_boolean", "tk_char", "tk_octet"]
636  if primitive.count(tk) > 0:
637  cdict['is_primitive'] = 'YES'
638 
639  if aliasType.sequence():
640  seqType = types.Type(aliasType.type().seqType())
641  # get type of element of sequence
642  (corba_etype, local_etype, is_primitive) = self.getType(seqType)
643  cdict['element_tk'] = self.tk_map[seqType.kind()]
644  if seqType.objref():
645  cdict['element_type_fq'] = corba_etype
646  ldict['element_type_fq'] = local_etype + '*'
647  else:
648  cdict['element_type_fq'] = corba_etype
649  ldict['element_type_fq'] = local_etype
650  return dict
651 
652 
653  # ------------------------------------------------------------
654  # オペレーションに関する辞書を作成する # # name: オペレーションの名称 # def createOperation(self, operation): dict = {} dict['name'] = id.mapID(operation.identifier()) dict['raises'] = [] for r in operation.raises(): edict = self.createDecl('exception') self.createExceptionIdent(edict, r) dict['raises'].append(edict) return dict def createAttribute(self, ident): dict = {} dict['name'] = id.mapID(ident) return dict # ------------------------------------------------------------ # オペレーションの戻り値に関する辞書を作成する # # type_r: remote戻り値の型名 # type_l: local戻り値の型名 # var_r: remote戻り値の変数名 # var_l: local戻り値の変数名 # to_doil: to_remote変換関数 # to_local: to_local変換関数 # def createReturn(self, operation): """ corba: base_type: ret_type: decl_type: tk: local: base_type: ret_type: decl_type: tk: """ dict = self.createDecl('return') cdict = dict['corba'] ldict = dict['local'] retType = types.Type(operation.returnType()) (corba_type, local_type, is_primitive) = self.getType(retType) cdict['base_type'] = corba_type ldict['base_type'] = local_type if is_primitive != None: cdict['is_primitive'] = is_primitive retn_type = types.Type(operation.returnType()).op(types.RET) retn_type = retn_type.replace('CORBA', '::CORBA') retn_type = retn_type.replace('RTC', '::RTC') retn_type = retn_type.replace('SDOPackage', '::SDOPackage') retn_type = retn_type.replace('::::', '::') cdict['retn_type'] = retn_type if retType.objref(): local_rtype = local_type + '*' else: local_rtype = local_type ldict['retn_type'] = local_rtype cdict['tk'] = ldict['tk'] = self.tk_map[retType.kind()] if retType.deref().sequence(): retType = retType.deref() if retType.sequence(): seqType = types.Type(retType.type().seqType()) # get type of element of sequence (corba_etype, local_etype, is_primitive) = self.getType(seqType) cdict['deref_tk'] = self.tk_map[seqType.kind()] else: derefType = retType.deref() (corba_dtype, local_dtype, is_primitive) = self.getType(derefType) cdict['deref_tk'] = self.tk_map[derefType.kind()] return dict # ------------------------------------------------------------ # オペレーションの引数に関する辞書を作成する # # type_r: remoteの引数の型 # type_l: localの引数の型 # var_r: remoteの変数名 # var_l: localの変数名 # to_doil: to_remote変換関数 # to_local: to_local変換関数 # def createArgs(self, operation, env): """ corba: base_type: arg_type: arg_name: var_name: decl_type: direction: tk: local: base_type: arg_type: arg_name: var_name: decl_type: direction: tk: """ args = [] direction = ['in', 'out', 'inout','return'] for arg in operation.parameters(): # corba args information dict = self.createDecl('arg') cdict = dict['corba'] ldict = dict['local'] paramType = types.Type(arg.paramType()) (corba_type, local_type, is_primitive) = self.getType(paramType) cdict['base_type'] = corba_type ldict['base_type'] = local_type if is_primitive != None: cdict['is_primitive'] = is_primitive arg_name = id.mapID(arg.identifier()) cdict['arg_name'] = arg_name ldict['arg_name'] = arg_name cdict['var_name'] = '_' + arg_name ldict['var_name'] = '_' + arg_name direction_val = direction[arg.direction()] cdict['direction'] = direction_val ldict['direction'] = direction_val cdict['tk'] = ldict['tk'] = self.tk_map[paramType.kind()] arg_type = paramType.op(types.direction(arg), use_out = 0) arg_type = arg_type.replace('CORBA', '::CORBA') arg_type = arg_type.replace('RTC', '::RTC') arg_type = arg_type.replace('SDOPackage', '::SDOPackage') arg_type = arg_type.replace('::::', '::') cdict['arg_type'] = arg_type out = arg.is_out() self.createArg(dict, paramType, out) args.append(dict) return args def createArg(self, dict, typeobj, out): (corba_type, local_type, is_primitive) = self.getType(typeobj) cdict = dict['corba'] ldict = dict['local'] if is_primitive != None: cdict['is_primitive'] = is_primitive paramType = typeobj if paramType.typedef(): paramType = paramType.deref() # primitive type if paramType.is_basic_data_types(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = local_type ldict['var_type'] = local_type cdict['var_type'] = corba_type # Enum type elif paramType.enum(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = local_type ldict['var_type'] = local_type cdict['var_type'] = corba_type # Struct type # Sequence type # Union type elif paramType.struct() or paramType.sequence() or \ paramType.union(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = 'const '+local_type+'&' ldict['var_type'] = local_type cdict['var_type'] = corba_type + '*' # Object type elif paramType.objref(): if out: ldict['arg_type'] = local_type + '*' else: ldict['arg_type'] = 'const '+local_type+'*' ldict['var_type'] = local_type + '*' cdict['var_type'] = corba_type # String type # Any type elif paramType.any() or paramType.string(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = 'const ' + local_type + '&' ldict['var_type'] = local_type cdict['var_type'] = corba_type else: raise "UNKNOWN TYPE", ( self.tk_map[paramType.kind()], paramType.typedef()) #------------------------------------------------------------ # AST visitor functions # # Tree walking code def visitAST(self, node): for n in node.declarations(): if ast.shouldGenerateCodeForDecl(n): n.accept(self) # modules can contain interfaces def visitModule(self, node): module = id.mapID(node.identifier()) self.module.append(module) for n in node.definitions(): # enter tree inside this module n.accept(self) self.module.pop() def visitDeclarator(self, node): print "Declarator", id.mapID(node.identifier()) def visitStructForward(self, node): dict = self.createDecl('struct_forward') # set corba/local struct identifier self.createStructIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitUnionForward(self, node): dict = self.createDecl('union_forward') # set corba/local union identifier self.getUnionIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitForward(self, node): dict = self.createDecl('interface_forward') # set corba/local interface identifier self.createInterfaceIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitException(self, node): dict = self.createDecl('exception') # set corba/local exception identifier self.createExceptionIdent(dict, node) # create members self.createMembers(dict, node) # add to dict self.dict['tree'].append(dict) return def visitUnion(self, node): dict = self.createDecl('union') # set corba/local union identifier self.createUnionIdent(dict, node) # create union cases self.createUnionCases(dict, node) # add to dict self.dict['tree'].append(dict) return def visitEnum(self, node): dict = self.createDecl('enum') cdict = dict['corba'] ldict = dict['local'] self.createEnumIdent(dict, node) enumerators = node.enumerators() memberlist = map(lambda x: id.Name(x.scopedName()).simple(), enumerators) cdict['members'] = memberlist ldict['members'] = memberlist self.dict['tree'].append(dict) return def visitTypedef(self, node): environment = id.lookup(node) scope = environment.scope() aliasType = types.Type(node.aliasType()) aliasTypeID = aliasType.member(environment) if node.constrType(): node.aliasType().decl().accept(self) for decl in node.declarators(): dict = self.createTypedef(aliasType, decl, environment) self.dict['tree'].append(dict) return def visitStruct(self, node): dict = self.createDecl('struct') self.createStructIdent(dict, node) self.createMembers(dict, node) self.dict['tree'].append(dict) return # interfaces cannot be further nested def visitInterface(self, node): self.__allInterfaces.append(node) # listed scope and interface name dict = self.createDecl('interface') self.createInterfaceIdent(dict, node) self.createInterfaceFileInfo(dict, node) dict['inherits'] = [] for ihnode in ast.allInherits(node): idict = self.createDecl('inherit') self.createInterfaceIdent(idict, ihnode) self.createInterfaceFileInfo(idict, ihnode) dict['inherits'].append(idict) env = id.lookup(node) allInterfaces = [node]# + ast.allInherits(node) allCallables = util.fold( map(lambda x:x.callables(), allInterfaces), [], lambda x, y: x + y ) dict['operations'] = [] dict['attributes'] = [] for c in allCallables: if isinstance(c, idlast.Attribute): attrType = types.Type(c.attrType()) d_attrType = attrType.deref() (corba_atype, local_atype, is_primitive) = self.getType(attrType) for i in c.identifiers(): ident = id.mapID(i) returnType = attrType.op(types.RET) inType = attrType.op(types.IN) adict = createDecl('attribute') cdict = adict['corba'] ldict = adict['local'] cdict['base_type'] = corba_atype; ldict['base_type'] = local_atype cdict['name'] = ident adict['return'] = self.createReturn(c) adict['arg'] = {} self.createArg(adict['arg'], attrType, False) dict['attributes'].append(adict) if c.readonly(): dict['readonly'] = 'yes' dict['attributes'].append(gdict) elif isinstance(c, idlast.Operation): # operations op_dict = self.createOperation(c) op_dict['return'] = self.createReturn(c) op_dict['args'] = self.createArgs(c, env) dict['operations'].append(op_dict) else: util.fatalError("Internal error generating interface member") raise "No code for interface member: " + repr(c) # self.dict['interfaces'].append(dict) self.dict['tree'].append(dict) return
655  #
656  # name: オペレーションの名称 # def createOperation(self, operation): dict = {} dict['name'] = id.mapID(operation.identifier()) dict['raises'] = [] for r in operation.raises(): edict = self.createDecl('exception') self.createExceptionIdent(edict, r) dict['raises'].append(edict) return dict def createAttribute(self, ident): dict = {} dict['name'] = id.mapID(ident) return dict # ------------------------------------------------------------ # オペレーションの戻り値に関する辞書を作成する # # type_r: remote戻り値の型名 # type_l: local戻り値の型名 # var_r: remote戻り値の変数名 # var_l: local戻り値の変数名 # to_doil: to_remote変換関数 # to_local: to_local変換関数 # def createReturn(self, operation): """ corba: base_type: ret_type: decl_type: tk: local: base_type: ret_type: decl_type: tk: """ dict = self.createDecl('return') cdict = dict['corba'] ldict = dict['local'] retType = types.Type(operation.returnType()) (corba_type, local_type, is_primitive) = self.getType(retType) cdict['base_type'] = corba_type ldict['base_type'] = local_type if is_primitive != None: cdict['is_primitive'] = is_primitive retn_type = types.Type(operation.returnType()).op(types.RET) retn_type = retn_type.replace('CORBA', '::CORBA') retn_type = retn_type.replace('RTC', '::RTC') retn_type = retn_type.replace('SDOPackage', '::SDOPackage') retn_type = retn_type.replace('::::', '::') cdict['retn_type'] = retn_type if retType.objref(): local_rtype = local_type + '*' else: local_rtype = local_type ldict['retn_type'] = local_rtype cdict['tk'] = ldict['tk'] = self.tk_map[retType.kind()] if retType.deref().sequence(): retType = retType.deref() if retType.sequence(): seqType = types.Type(retType.type().seqType()) # get type of element of sequence (corba_etype, local_etype, is_primitive) = self.getType(seqType) cdict['deref_tk'] = self.tk_map[seqType.kind()] else: derefType = retType.deref() (corba_dtype, local_dtype, is_primitive) = self.getType(derefType) cdict['deref_tk'] = self.tk_map[derefType.kind()] return dict # ------------------------------------------------------------ # オペレーションの引数に関する辞書を作成する # # type_r: remoteの引数の型 # type_l: localの引数の型 # var_r: remoteの変数名 # var_l: localの変数名 # to_doil: to_remote変換関数 # to_local: to_local変換関数 # def createArgs(self, operation, env): """ corba: base_type: arg_type: arg_name: var_name: decl_type: direction: tk: local: base_type: arg_type: arg_name: var_name: decl_type: direction: tk: """ args = [] direction = ['in', 'out', 'inout','return'] for arg in operation.parameters(): # corba args information dict = self.createDecl('arg') cdict = dict['corba'] ldict = dict['local'] paramType = types.Type(arg.paramType()) (corba_type, local_type, is_primitive) = self.getType(paramType) cdict['base_type'] = corba_type ldict['base_type'] = local_type if is_primitive != None: cdict['is_primitive'] = is_primitive arg_name = id.mapID(arg.identifier()) cdict['arg_name'] = arg_name ldict['arg_name'] = arg_name cdict['var_name'] = '_' + arg_name ldict['var_name'] = '_' + arg_name direction_val = direction[arg.direction()] cdict['direction'] = direction_val ldict['direction'] = direction_val cdict['tk'] = ldict['tk'] = self.tk_map[paramType.kind()] arg_type = paramType.op(types.direction(arg), use_out = 0) arg_type = arg_type.replace('CORBA', '::CORBA') arg_type = arg_type.replace('RTC', '::RTC') arg_type = arg_type.replace('SDOPackage', '::SDOPackage') arg_type = arg_type.replace('::::', '::') cdict['arg_type'] = arg_type out = arg.is_out() self.createArg(dict, paramType, out) args.append(dict) return args def createArg(self, dict, typeobj, out): (corba_type, local_type, is_primitive) = self.getType(typeobj) cdict = dict['corba'] ldict = dict['local'] if is_primitive != None: cdict['is_primitive'] = is_primitive paramType = typeobj if paramType.typedef(): paramType = paramType.deref() # primitive type if paramType.is_basic_data_types(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = local_type ldict['var_type'] = local_type cdict['var_type'] = corba_type # Enum type elif paramType.enum(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = local_type ldict['var_type'] = local_type cdict['var_type'] = corba_type # Struct type # Sequence type # Union type elif paramType.struct() or paramType.sequence() or \ paramType.union(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = 'const '+local_type+'&' ldict['var_type'] = local_type cdict['var_type'] = corba_type + '*' # Object type elif paramType.objref(): if out: ldict['arg_type'] = local_type + '*' else: ldict['arg_type'] = 'const '+local_type+'*' ldict['var_type'] = local_type + '*' cdict['var_type'] = corba_type # String type # Any type elif paramType.any() or paramType.string(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = 'const ' + local_type + '&' ldict['var_type'] = local_type cdict['var_type'] = corba_type else: raise "UNKNOWN TYPE", ( self.tk_map[paramType.kind()], paramType.typedef()) #------------------------------------------------------------ # AST visitor functions # # Tree walking code def visitAST(self, node): for n in node.declarations(): if ast.shouldGenerateCodeForDecl(n): n.accept(self) # modules can contain interfaces def visitModule(self, node): module = id.mapID(node.identifier()) self.module.append(module) for n in node.definitions(): # enter tree inside this module n.accept(self) self.module.pop() def visitDeclarator(self, node): print "Declarator", id.mapID(node.identifier()) def visitStructForward(self, node): dict = self.createDecl('struct_forward') # set corba/local struct identifier self.createStructIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitUnionForward(self, node): dict = self.createDecl('union_forward') # set corba/local union identifier self.getUnionIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitForward(self, node): dict = self.createDecl('interface_forward') # set corba/local interface identifier self.createInterfaceIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitException(self, node): dict = self.createDecl('exception') # set corba/local exception identifier self.createExceptionIdent(dict, node) # create members self.createMembers(dict, node) # add to dict self.dict['tree'].append(dict) return def visitUnion(self, node): dict = self.createDecl('union') # set corba/local union identifier self.createUnionIdent(dict, node) # create union cases self.createUnionCases(dict, node) # add to dict self.dict['tree'].append(dict) return def visitEnum(self, node): dict = self.createDecl('enum') cdict = dict['corba'] ldict = dict['local'] self.createEnumIdent(dict, node) enumerators = node.enumerators() memberlist = map(lambda x: id.Name(x.scopedName()).simple(), enumerators) cdict['members'] = memberlist ldict['members'] = memberlist self.dict['tree'].append(dict) return def visitTypedef(self, node): environment = id.lookup(node) scope = environment.scope() aliasType = types.Type(node.aliasType()) aliasTypeID = aliasType.member(environment) if node.constrType(): node.aliasType().decl().accept(self) for decl in node.declarators(): dict = self.createTypedef(aliasType, decl, environment) self.dict['tree'].append(dict) return def visitStruct(self, node): dict = self.createDecl('struct') self.createStructIdent(dict, node) self.createMembers(dict, node) self.dict['tree'].append(dict) return # interfaces cannot be further nested def visitInterface(self, node): self.__allInterfaces.append(node) # listed scope and interface name dict = self.createDecl('interface') self.createInterfaceIdent(dict, node) self.createInterfaceFileInfo(dict, node) dict['inherits'] = [] for ihnode in ast.allInherits(node): idict = self.createDecl('inherit') self.createInterfaceIdent(idict, ihnode) self.createInterfaceFileInfo(idict, ihnode) dict['inherits'].append(idict) env = id.lookup(node) allInterfaces = [node]# + ast.allInherits(node) allCallables = util.fold( map(lambda x:x.callables(), allInterfaces), [], lambda x, y: x + y ) dict['operations'] = [] dict['attributes'] = [] for c in allCallables: if isinstance(c, idlast.Attribute): attrType = types.Type(c.attrType()) d_attrType = attrType.deref() (corba_atype, local_atype, is_primitive) = self.getType(attrType) for i in c.identifiers(): ident = id.mapID(i) returnType = attrType.op(types.RET) inType = attrType.op(types.IN) adict = createDecl('attribute') cdict = adict['corba'] ldict = adict['local'] cdict['base_type'] = corba_atype; ldict['base_type'] = local_atype cdict['name'] = ident adict['return'] = self.createReturn(c) adict['arg'] = {} self.createArg(adict['arg'], attrType, False) dict['attributes'].append(adict) if c.readonly(): dict['readonly'] = 'yes' dict['attributes'].append(gdict) elif isinstance(c, idlast.Operation): # operations op_dict = self.createOperation(c) op_dict['return'] = self.createReturn(c) op_dict['args'] = self.createArgs(c, env) dict['operations'].append(op_dict) else: util.fatalError("Internal error generating interface member") raise "No code for interface member: " + repr(c) # self.dict['interfaces'].append(dict) self.dict['tree'].append(dict) return
657  #
658  def createOperation(self, operation):
659  dict = {}
660  dict['name'] = id.mapID(operation.identifier())
661  dict['raises'] = []
662  for r in operation.raises():
663  edict = self.createDecl('exception')
664  self.createExceptionIdent(edict, r)
665  dict['raises'].append(edict)
666  return dict
667 
668  def createAttribute(self, ident):
669  dict = {}
670  dict['name'] = id.mapID(ident)
671  return dict
672 
673  # ------------------------------------------------------------
674  # オペレーションの戻り値に関する辞書を作成する # # type_r: remote戻り値の型名 # type_l: local戻り値の型名 # var_r: remote戻り値の変数名 # var_l: local戻り値の変数名 # to_doil: to_remote変換関数 # to_local: to_local変換関数 # def createReturn(self, operation): """ corba: base_type: ret_type: decl_type: tk: local: base_type: ret_type: decl_type: tk: """ dict = self.createDecl('return') cdict = dict['corba'] ldict = dict['local'] retType = types.Type(operation.returnType()) (corba_type, local_type, is_primitive) = self.getType(retType) cdict['base_type'] = corba_type ldict['base_type'] = local_type if is_primitive != None: cdict['is_primitive'] = is_primitive retn_type = types.Type(operation.returnType()).op(types.RET) retn_type = retn_type.replace('CORBA', '::CORBA') retn_type = retn_type.replace('RTC', '::RTC') retn_type = retn_type.replace('SDOPackage', '::SDOPackage') retn_type = retn_type.replace('::::', '::') cdict['retn_type'] = retn_type if retType.objref(): local_rtype = local_type + '*' else: local_rtype = local_type ldict['retn_type'] = local_rtype cdict['tk'] = ldict['tk'] = self.tk_map[retType.kind()] if retType.deref().sequence(): retType = retType.deref() if retType.sequence(): seqType = types.Type(retType.type().seqType()) # get type of element of sequence (corba_etype, local_etype, is_primitive) = self.getType(seqType) cdict['deref_tk'] = self.tk_map[seqType.kind()] else: derefType = retType.deref() (corba_dtype, local_dtype, is_primitive) = self.getType(derefType) cdict['deref_tk'] = self.tk_map[derefType.kind()] return dict # ------------------------------------------------------------ # オペレーションの引数に関する辞書を作成する # # type_r: remoteの引数の型 # type_l: localの引数の型 # var_r: remoteの変数名 # var_l: localの変数名 # to_doil: to_remote変換関数 # to_local: to_local変換関数 # def createArgs(self, operation, env): """ corba: base_type: arg_type: arg_name: var_name: decl_type: direction: tk: local: base_type: arg_type: arg_name: var_name: decl_type: direction: tk: """ args = [] direction = ['in', 'out', 'inout','return'] for arg in operation.parameters(): # corba args information dict = self.createDecl('arg') cdict = dict['corba'] ldict = dict['local'] paramType = types.Type(arg.paramType()) (corba_type, local_type, is_primitive) = self.getType(paramType) cdict['base_type'] = corba_type ldict['base_type'] = local_type if is_primitive != None: cdict['is_primitive'] = is_primitive arg_name = id.mapID(arg.identifier()) cdict['arg_name'] = arg_name ldict['arg_name'] = arg_name cdict['var_name'] = '_' + arg_name ldict['var_name'] = '_' + arg_name direction_val = direction[arg.direction()] cdict['direction'] = direction_val ldict['direction'] = direction_val cdict['tk'] = ldict['tk'] = self.tk_map[paramType.kind()] arg_type = paramType.op(types.direction(arg), use_out = 0) arg_type = arg_type.replace('CORBA', '::CORBA') arg_type = arg_type.replace('RTC', '::RTC') arg_type = arg_type.replace('SDOPackage', '::SDOPackage') arg_type = arg_type.replace('::::', '::') cdict['arg_type'] = arg_type out = arg.is_out() self.createArg(dict, paramType, out) args.append(dict) return args def createArg(self, dict, typeobj, out): (corba_type, local_type, is_primitive) = self.getType(typeobj) cdict = dict['corba'] ldict = dict['local'] if is_primitive != None: cdict['is_primitive'] = is_primitive paramType = typeobj if paramType.typedef(): paramType = paramType.deref() # primitive type if paramType.is_basic_data_types(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = local_type ldict['var_type'] = local_type cdict['var_type'] = corba_type # Enum type elif paramType.enum(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = local_type ldict['var_type'] = local_type cdict['var_type'] = corba_type # Struct type # Sequence type # Union type elif paramType.struct() or paramType.sequence() or \ paramType.union(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = 'const '+local_type+'&' ldict['var_type'] = local_type cdict['var_type'] = corba_type + '*' # Object type elif paramType.objref(): if out: ldict['arg_type'] = local_type + '*' else: ldict['arg_type'] = 'const '+local_type+'*' ldict['var_type'] = local_type + '*' cdict['var_type'] = corba_type # String type # Any type elif paramType.any() or paramType.string(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = 'const ' + local_type + '&' ldict['var_type'] = local_type cdict['var_type'] = corba_type else: raise "UNKNOWN TYPE", ( self.tk_map[paramType.kind()], paramType.typedef()) #------------------------------------------------------------ # AST visitor functions # # Tree walking code def visitAST(self, node): for n in node.declarations(): if ast.shouldGenerateCodeForDecl(n): n.accept(self) # modules can contain interfaces def visitModule(self, node): module = id.mapID(node.identifier()) self.module.append(module) for n in node.definitions(): # enter tree inside this module n.accept(self) self.module.pop() def visitDeclarator(self, node): print "Declarator", id.mapID(node.identifier()) def visitStructForward(self, node): dict = self.createDecl('struct_forward') # set corba/local struct identifier self.createStructIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitUnionForward(self, node): dict = self.createDecl('union_forward') # set corba/local union identifier self.getUnionIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitForward(self, node): dict = self.createDecl('interface_forward') # set corba/local interface identifier self.createInterfaceIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitException(self, node): dict = self.createDecl('exception') # set corba/local exception identifier self.createExceptionIdent(dict, node) # create members self.createMembers(dict, node) # add to dict self.dict['tree'].append(dict) return def visitUnion(self, node): dict = self.createDecl('union') # set corba/local union identifier self.createUnionIdent(dict, node) # create union cases self.createUnionCases(dict, node) # add to dict self.dict['tree'].append(dict) return def visitEnum(self, node): dict = self.createDecl('enum') cdict = dict['corba'] ldict = dict['local'] self.createEnumIdent(dict, node) enumerators = node.enumerators() memberlist = map(lambda x: id.Name(x.scopedName()).simple(), enumerators) cdict['members'] = memberlist ldict['members'] = memberlist self.dict['tree'].append(dict) return def visitTypedef(self, node): environment = id.lookup(node) scope = environment.scope() aliasType = types.Type(node.aliasType()) aliasTypeID = aliasType.member(environment) if node.constrType(): node.aliasType().decl().accept(self) for decl in node.declarators(): dict = self.createTypedef(aliasType, decl, environment) self.dict['tree'].append(dict) return def visitStruct(self, node): dict = self.createDecl('struct') self.createStructIdent(dict, node) self.createMembers(dict, node) self.dict['tree'].append(dict) return # interfaces cannot be further nested def visitInterface(self, node): self.__allInterfaces.append(node) # listed scope and interface name dict = self.createDecl('interface') self.createInterfaceIdent(dict, node) self.createInterfaceFileInfo(dict, node) dict['inherits'] = [] for ihnode in ast.allInherits(node): idict = self.createDecl('inherit') self.createInterfaceIdent(idict, ihnode) self.createInterfaceFileInfo(idict, ihnode) dict['inherits'].append(idict) env = id.lookup(node) allInterfaces = [node]# + ast.allInherits(node) allCallables = util.fold( map(lambda x:x.callables(), allInterfaces), [], lambda x, y: x + y ) dict['operations'] = [] dict['attributes'] = [] for c in allCallables: if isinstance(c, idlast.Attribute): attrType = types.Type(c.attrType()) d_attrType = attrType.deref() (corba_atype, local_atype, is_primitive) = self.getType(attrType) for i in c.identifiers(): ident = id.mapID(i) returnType = attrType.op(types.RET) inType = attrType.op(types.IN) adict = createDecl('attribute') cdict = adict['corba'] ldict = adict['local'] cdict['base_type'] = corba_atype; ldict['base_type'] = local_atype cdict['name'] = ident adict['return'] = self.createReturn(c) adict['arg'] = {} self.createArg(adict['arg'], attrType, False) dict['attributes'].append(adict) if c.readonly(): dict['readonly'] = 'yes' dict['attributes'].append(gdict) elif isinstance(c, idlast.Operation): # operations op_dict = self.createOperation(c) op_dict['return'] = self.createReturn(c) op_dict['args'] = self.createArgs(c, env) dict['operations'].append(op_dict) else: util.fatalError("Internal error generating interface member") raise "No code for interface member: " + repr(c) # self.dict['interfaces'].append(dict) self.dict['tree'].append(dict) return
675  #
676  # type_r: remote戻り値の型名
677  # type_l: local戻り値の型名
678  # var_r: remote戻り値の変数名
679  # var_l: local戻り値の変数名
680  # to_doil: to_remote変換関数
681  # to_local: to_local変換関数
682  #
683  def createReturn(self, operation):
684  """
685  corba:
686  base_type:
687  ret_type:
688  decl_type:
689  tk:
690  local:
691  base_type:
692  ret_type:
693  decl_type:
694  tk:
695  """
696  dict = self.createDecl('return')
697  cdict = dict['corba']
698  ldict = dict['local']
699 
700  retType = types.Type(operation.returnType())
701  (corba_type, local_type, is_primitive) = self.getType(retType)
702  cdict['base_type'] = corba_type
703  ldict['base_type'] = local_type
704  if is_primitive != None:
705  cdict['is_primitive'] = is_primitive
706  retn_type = types.Type(operation.returnType()).op(types.RET)
707  retn_type = retn_type.replace('CORBA', '::CORBA')
708  retn_type = retn_type.replace('RTC', '::RTC')
709  retn_type = retn_type.replace('SDOPackage', '::SDOPackage')
710  retn_type = retn_type.replace('::::', '::')
711  cdict['retn_type'] = retn_type
712 
713  if retType.objref(): local_rtype = local_type + '*'
714  else: local_rtype = local_type
715 
716  ldict['retn_type'] = local_rtype
717  cdict['tk'] = ldict['tk'] = self.tk_map[retType.kind()]
718 
719  if retType.deref().sequence():
720  retType = retType.deref()
721 
722  if retType.sequence():
723  seqType = types.Type(retType.type().seqType())
724  # get type of element of sequence
725  (corba_etype, local_etype, is_primitive) = self.getType(seqType)
726  cdict['deref_tk'] = self.tk_map[seqType.kind()]
727  else:
728  derefType = retType.deref()
729  (corba_dtype, local_dtype, is_primitive) = self.getType(derefType)
730  cdict['deref_tk'] = self.tk_map[derefType.kind()]
731  return dict
732 
733 
734  # ------------------------------------------------------------
735  # オペレーションの引数に関する辞書を作成する # # type_r: remoteの引数の型 # type_l: localの引数の型 # var_r: remoteの変数名 # var_l: localの変数名 # to_doil: to_remote変換関数 # to_local: to_local変換関数 # def createArgs(self, operation, env): """ corba: base_type: arg_type: arg_name: var_name: decl_type: direction: tk: local: base_type: arg_type: arg_name: var_name: decl_type: direction: tk: """ args = [] direction = ['in', 'out', 'inout','return'] for arg in operation.parameters(): # corba args information dict = self.createDecl('arg') cdict = dict['corba'] ldict = dict['local'] paramType = types.Type(arg.paramType()) (corba_type, local_type, is_primitive) = self.getType(paramType) cdict['base_type'] = corba_type ldict['base_type'] = local_type if is_primitive != None: cdict['is_primitive'] = is_primitive arg_name = id.mapID(arg.identifier()) cdict['arg_name'] = arg_name ldict['arg_name'] = arg_name cdict['var_name'] = '_' + arg_name ldict['var_name'] = '_' + arg_name direction_val = direction[arg.direction()] cdict['direction'] = direction_val ldict['direction'] = direction_val cdict['tk'] = ldict['tk'] = self.tk_map[paramType.kind()] arg_type = paramType.op(types.direction(arg), use_out = 0) arg_type = arg_type.replace('CORBA', '::CORBA') arg_type = arg_type.replace('RTC', '::RTC') arg_type = arg_type.replace('SDOPackage', '::SDOPackage') arg_type = arg_type.replace('::::', '::') cdict['arg_type'] = arg_type out = arg.is_out() self.createArg(dict, paramType, out) args.append(dict) return args def createArg(self, dict, typeobj, out): (corba_type, local_type, is_primitive) = self.getType(typeobj) cdict = dict['corba'] ldict = dict['local'] if is_primitive != None: cdict['is_primitive'] = is_primitive paramType = typeobj if paramType.typedef(): paramType = paramType.deref() # primitive type if paramType.is_basic_data_types(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = local_type ldict['var_type'] = local_type cdict['var_type'] = corba_type # Enum type elif paramType.enum(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = local_type ldict['var_type'] = local_type cdict['var_type'] = corba_type # Struct type # Sequence type # Union type elif paramType.struct() or paramType.sequence() or \ paramType.union(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = 'const '+local_type+'&' ldict['var_type'] = local_type cdict['var_type'] = corba_type + '*' # Object type elif paramType.objref(): if out: ldict['arg_type'] = local_type + '*' else: ldict['arg_type'] = 'const '+local_type+'*' ldict['var_type'] = local_type + '*' cdict['var_type'] = corba_type # String type # Any type elif paramType.any() or paramType.string(): if out: ldict['arg_type'] = local_type + '&' else: ldict['arg_type'] = 'const ' + local_type + '&' ldict['var_type'] = local_type cdict['var_type'] = corba_type else: raise "UNKNOWN TYPE", ( self.tk_map[paramType.kind()], paramType.typedef()) #------------------------------------------------------------ # AST visitor functions # # Tree walking code def visitAST(self, node): for n in node.declarations(): if ast.shouldGenerateCodeForDecl(n): n.accept(self) # modules can contain interfaces def visitModule(self, node): module = id.mapID(node.identifier()) self.module.append(module) for n in node.definitions(): # enter tree inside this module n.accept(self) self.module.pop() def visitDeclarator(self, node): print "Declarator", id.mapID(node.identifier()) def visitStructForward(self, node): dict = self.createDecl('struct_forward') # set corba/local struct identifier self.createStructIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitUnionForward(self, node): dict = self.createDecl('union_forward') # set corba/local union identifier self.getUnionIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitForward(self, node): dict = self.createDecl('interface_forward') # set corba/local interface identifier self.createInterfaceIdent(dict, node) # append dicts to tree self.dict['tree'].append(dict) return def visitException(self, node): dict = self.createDecl('exception') # set corba/local exception identifier self.createExceptionIdent(dict, node) # create members self.createMembers(dict, node) # add to dict self.dict['tree'].append(dict) return def visitUnion(self, node): dict = self.createDecl('union') # set corba/local union identifier self.createUnionIdent(dict, node) # create union cases self.createUnionCases(dict, node) # add to dict self.dict['tree'].append(dict) return def visitEnum(self, node): dict = self.createDecl('enum') cdict = dict['corba'] ldict = dict['local'] self.createEnumIdent(dict, node) enumerators = node.enumerators() memberlist = map(lambda x: id.Name(x.scopedName()).simple(), enumerators) cdict['members'] = memberlist ldict['members'] = memberlist self.dict['tree'].append(dict) return def visitTypedef(self, node): environment = id.lookup(node) scope = environment.scope() aliasType = types.Type(node.aliasType()) aliasTypeID = aliasType.member(environment) if node.constrType(): node.aliasType().decl().accept(self) for decl in node.declarators(): dict = self.createTypedef(aliasType, decl, environment) self.dict['tree'].append(dict) return def visitStruct(self, node): dict = self.createDecl('struct') self.createStructIdent(dict, node) self.createMembers(dict, node) self.dict['tree'].append(dict) return # interfaces cannot be further nested def visitInterface(self, node): self.__allInterfaces.append(node) # listed scope and interface name dict = self.createDecl('interface') self.createInterfaceIdent(dict, node) self.createInterfaceFileInfo(dict, node) dict['inherits'] = [] for ihnode in ast.allInherits(node): idict = self.createDecl('inherit') self.createInterfaceIdent(idict, ihnode) self.createInterfaceFileInfo(idict, ihnode) dict['inherits'].append(idict) env = id.lookup(node) allInterfaces = [node]# + ast.allInherits(node) allCallables = util.fold( map(lambda x:x.callables(), allInterfaces), [], lambda x, y: x + y ) dict['operations'] = [] dict['attributes'] = [] for c in allCallables: if isinstance(c, idlast.Attribute): attrType = types.Type(c.attrType()) d_attrType = attrType.deref() (corba_atype, local_atype, is_primitive) = self.getType(attrType) for i in c.identifiers(): ident = id.mapID(i) returnType = attrType.op(types.RET) inType = attrType.op(types.IN) adict = createDecl('attribute') cdict = adict['corba'] ldict = adict['local'] cdict['base_type'] = corba_atype; ldict['base_type'] = local_atype cdict['name'] = ident adict['return'] = self.createReturn(c) adict['arg'] = {} self.createArg(adict['arg'], attrType, False) dict['attributes'].append(adict) if c.readonly(): dict['readonly'] = 'yes' dict['attributes'].append(gdict) elif isinstance(c, idlast.Operation): # operations op_dict = self.createOperation(c) op_dict['return'] = self.createReturn(c) op_dict['args'] = self.createArgs(c, env) dict['operations'].append(op_dict) else: util.fatalError("Internal error generating interface member") raise "No code for interface member: " + repr(c) # self.dict['interfaces'].append(dict) self.dict['tree'].append(dict) return
736  #
737  # type_r: remoteの引数の型
738  # type_l: localの引数の型
739  # var_r: remoteの変数名
740  # var_l: localの変数名
741  # to_doil: to_remote変換関数
742  # to_local: to_local変換関数
743  #
744  def createArgs(self, operation, env):
745  """
746 
747  corba:
748  base_type:
749  arg_type:
750  arg_name:
751  var_name:
752  decl_type:
753  direction:
754  tk:
755  local:
756  base_type:
757  arg_type:
758  arg_name:
759  var_name:
760  decl_type:
761  direction:
762  tk:
763 
764 
765  """
766  args = []
767  direction = ['in', 'out', 'inout','return']
768  for arg in operation.parameters():
769  # corba args information
770  dict = self.createDecl('arg')
771  cdict = dict['corba']
772  ldict = dict['local']
773 
774  paramType = types.Type(arg.paramType())
775  (corba_type, local_type, is_primitive) = self.getType(paramType)
776 
777  cdict['base_type'] = corba_type
778  ldict['base_type'] = local_type
779  if is_primitive != None:
780  cdict['is_primitive'] = is_primitive
781 
782  arg_name = id.mapID(arg.identifier())
783  cdict['arg_name'] = arg_name
784  ldict['arg_name'] = arg_name
785  cdict['var_name'] = '_' + arg_name
786  ldict['var_name'] = '_' + arg_name
787 
788  direction_val = direction[arg.direction()]
789  cdict['direction'] = direction_val
790  ldict['direction'] = direction_val
791 
792  cdict['tk'] = ldict['tk'] = self.tk_map[paramType.kind()]
793  arg_type = paramType.op(types.direction(arg), use_out = 0)
794  arg_type = arg_type.replace('CORBA', '::CORBA')
795  arg_type = arg_type.replace('RTC', '::RTC')
796  arg_type = arg_type.replace('SDOPackage', '::SDOPackage')
797  arg_type = arg_type.replace('::::', '::')
798  cdict['arg_type'] = arg_type
799  out = arg.is_out()
800  self.createArg(dict, paramType, out)
801  args.append(dict)
802  return args
803 
804  def createArg(self, dict, typeobj, out):
805  (corba_type, local_type, is_primitive) = self.getType(typeobj)
806  cdict = dict['corba']
807  ldict = dict['local']
808  if is_primitive != None:
809  cdict['is_primitive'] = is_primitive
810 
811  paramType = typeobj
812  if paramType.typedef():
813  paramType = paramType.deref()
814  # primitive type
815  if paramType.is_basic_data_types():
816  if out: ldict['arg_type'] = local_type + '&'
817  else: ldict['arg_type'] = local_type
818  ldict['var_type'] = local_type
819  cdict['var_type'] = corba_type
820  # Enum type
821  elif paramType.enum():
822  if out: ldict['arg_type'] = local_type + '&'
823  else: ldict['arg_type'] = local_type
824  ldict['var_type'] = local_type
825  cdict['var_type'] = corba_type
826  # Struct type
827  # Sequence type
828  # Union type
829  elif paramType.struct() or paramType.sequence() or \
830  paramType.union():
831  if out: ldict['arg_type'] = local_type + '&'
832  else: ldict['arg_type'] = 'const '+local_type+'&'
833  ldict['var_type'] = local_type
834  cdict['var_type'] = corba_type + '*'
835  # Object type
836  elif paramType.objref():
837  if out: ldict['arg_type'] = local_type + '*'
838  else: ldict['arg_type'] = 'const '+local_type+'*'
839  ldict['var_type'] = local_type + '*'
840  cdict['var_type'] = corba_type
841  # String type
842  # Any type
843  elif paramType.any() or paramType.string():
844  if out: ldict['arg_type'] = local_type + '&'
845  else: ldict['arg_type'] = 'const ' + local_type + '&'
846  ldict['var_type'] = local_type
847  cdict['var_type'] = corba_type
848  else:
849  raise "UNKNOWN TYPE", (
850  self.tk_map[paramType.kind()],
851  paramType.typedef())
852 
853 
854  #------------------------------------------------------------
855  # AST visitor functions
856  #
857  # Tree walking code
858  def visitAST(self, node):
859  for n in node.declarations():
860  if ast.shouldGenerateCodeForDecl(n):
861  n.accept(self)
862 
863  # modules can contain interfaces
864  def visitModule(self, node):
865  module = id.mapID(node.identifier())
866  self.module.append(module)
867  for n in node.definitions():
868  # enter tree inside this module
869  n.accept(self)
870  self.module.pop()
871 
872 
873  def visitDeclarator(self, node):
874  print "Declarator", id.mapID(node.identifier())
875 
876 
877  def visitStructForward(self, node):
878  dict = self.createDecl('struct_forward')
879  # set corba/local struct identifier
880  self.createStructIdent(dict, node)
881  # append dicts to tree
882  self.dict['tree'].append(dict)
883  return
884 
885 
886  def visitUnionForward(self, node):
887  dict = self.createDecl('union_forward')
888  # set corba/local union identifier
889  self.getUnionIdent(dict, node)
890  # append dicts to tree
891  self.dict['tree'].append(dict)
892  return
893 
894 
895  def visitForward(self, node):
896  dict = self.createDecl('interface_forward')
897  # set corba/local interface identifier
898  self.createInterfaceIdent(dict, node)
899  # append dicts to tree
900  self.dict['tree'].append(dict)
901  return
902 
903  def visitException(self, node):
904  dict = self.createDecl('exception')
905  # set corba/local exception identifier
906  self.createExceptionIdent(dict, node)
907  # create members
908  self.createMembers(dict, node)
909  # add to dict
910  self.dict['tree'].append(dict)
911  return
912 
913 
914  def visitUnion(self, node):
915  dict = self.createDecl('union')
916  # set corba/local union identifier
917  self.createUnionIdent(dict, node)
918  # create union cases
919  self.createUnionCases(dict, node)
920  # add to dict
921  self.dict['tree'].append(dict)
922  return
923 
924 
925  def visitEnum(self, node):
926  dict = self.createDecl('enum')
927  cdict = dict['corba']
928  ldict = dict['local']
929  self.createEnumIdent(dict, node)
930 
931  enumerators = node.enumerators()
932  memberlist = map(lambda x: id.Name(x.scopedName()).simple(),
933  enumerators)
934  cdict['members'] = memberlist
935  ldict['members'] = memberlist
936  self.dict['tree'].append(dict)
937  return
938 
939 
940  def visitTypedef(self, node):
941  environment = id.lookup(node)
942  scope = environment.scope()
943  aliasType = types.Type(node.aliasType())
944  aliasTypeID = aliasType.member(environment)
945 
946  if node.constrType():
947  node.aliasType().decl().accept(self)
948 
949 
950  for decl in node.declarators():
951  dict = self.createTypedef(aliasType, decl, environment)
952  self.dict['tree'].append(dict)
953  return
954 
955 
956  def visitStruct(self, node):
957  dict = self.createDecl('struct')
958  self.createStructIdent(dict, node)
959  self.createMembers(dict, node)
960  self.dict['tree'].append(dict)
961  return
962 
963 
964  # interfaces cannot be further nested
965  def visitInterface(self, node):
966  self.__allInterfaces.append(node)
967  # listed scope and interface name
968  dict = self.createDecl('interface')
969  self.createInterfaceIdent(dict, node)
970  self.createInterfaceFileInfo(dict, node)
971 
972  dict['inherits'] = []
973  for ihnode in ast.allInherits(node):
974  idict = self.createDecl('inherit')
975  self.createInterfaceIdent(idict, ihnode)
976  self.createInterfaceFileInfo(idict, ihnode)
977  dict['inherits'].append(idict)
978 
979  env = id.lookup(node)
980 
981  allInterfaces = [node]# + ast.allInherits(node)
982  allCallables = util.fold( map(lambda x:x.callables(), allInterfaces),
983  [], lambda x, y: x + y )
984 
985  dict['operations'] = []
986  dict['attributes'] = []
987  for c in allCallables:
988  if isinstance(c, idlast.Attribute):
989  attrType = types.Type(c.attrType())
990  d_attrType = attrType.deref()
991  (corba_atype, local_atype, is_primitive) = self.getType(attrType)
992 
993  for i in c.identifiers():
994  ident = id.mapID(i)
995  returnType = attrType.op(types.RET)
996  inType = attrType.op(types.IN)
997 
998  adict = createDecl('attribute')
999  cdict = adict['corba']
1000  ldict = adict['local']
1001  cdict['base_type'] = corba_atype;
1002  ldict['base_type'] = local_atype
1003  cdict['name'] = ident
1004  adict['return'] = self.createReturn(c)
1005  adict['arg'] = {}
1006  self.createArg(adict['arg'], attrType, False)
1007 
1008  dict['attributes'].append(adict)
1009  if c.readonly():
1010  dict['readonly'] = 'yes'
1011  dict['attributes'].append(gdict)
1012 
1013  elif isinstance(c, idlast.Operation):
1014  # operations
1015  op_dict = self.createOperation(c)
1016  op_dict['return'] = self.createReturn(c)
1017  op_dict['args'] = self.createArgs(c, env)
1018  dict['operations'].append(op_dict)
1019  else:
1020  util.fatalError("Internal error generating interface member")
1021  raise "No code for interface member: " + repr(c)
1022 
1023 # self.dict['interfaces'].append(dict)
1024  self.dict['tree'].append(dict)
1025  return
1026 
1027 
def createUnionCase(self, case, node, switchtype, environment)
Definition: dictbuilder.py:516
def createArgs(self, operation, env)
Definition: dictbuilder.py:744
def createUnionIdent(self, dict, node)
Definition: dictbuilder.py:401
def createTypedef(self, aliasType, decl, env)
Definition: dictbuilder.py:601
def createMember(self, decl, member, env)
Definition: dictbuilder.py:472
def impl_fullname(name)
Definition: dictbuilder.py:40
def createStructIdent(self, dict, node)
Definition: dictbuilder.py:436
def run(tree, config)
Definition: dictbuilder.py:45
def createEnumIdent(self, dict, node)
Definition: dictbuilder.py:440
def createArg(self, dict, typeobj, out)
Definition: dictbuilder.py:804
void append(SDOPackage::NVList &dest, const SDOPackage::NVList &src)
Append an element to NVList.
Definition: NVUtil.cpp:354
def createExceptionIdent(self, dict, node)
Definition: dictbuilder.py:444
def createInterfaceFileInfo(self, dict, node)
Definition: dictbuilder.py:358
def __init__(self, tree, config)
Definition: dictbuilder.py:63
def createUnionCases(self, dict, node)
Definition: dictbuilder.py:501
def createInterfaceIdent(self, dict, node)
Definition: dictbuilder.py:317


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:58