1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
#
# Copyright (C) 2000 Stefan Seefeld
# Copyright (C) 2000 Stephen Davies
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#
import string
from Synopsis.Processor import Composite, Parameter
from Synopsis import AST, Type
class Linker(Composite, AST.Visitor, Type.Visitor):
"""Visitor that removes duplicate declarations"""
remove_empty_modules = Parameter(True, 'Remove empty modules.')
def process(self, ast, **kwds):
self.set_parameters(kwds)
self.ast = self.merge_input(ast)
root = AST.MetaModule("", "",[])
self.__scopes = [root]
global_dict = {}
self.__dict_map = {id(root) : global_dict}
self.__dicts = [global_dict]
self.types = self.ast.types()
declarations = self.ast.declarations()
try:
for decl in declarations: decl.accept(self)
declarations[:] = root.declarations()
except TypeError, e:
print 'linker error :', e
for file in self.ast.files().values():
self.visitSourceFile(file)
if self.remove_empty_modules:
import EmptyModuleRemover
self.ast = EmptyModuleRemover.EmptyModuleRemover().process(self.ast)
# now deal with the sub-processors, if any
output = self.output
self.ast = Composite.process(self, self.ast, input=[], output='')
self.output = output
return self.output_and_return_ast()
def lookup(self, name):
"""look whether the current scope already contains
a declaration with the given name"""
if self.__dicts[-1].has_key(name):
return self.__dicts[-1][name]
return None
def append(self, declaration):
"""append declaration to the current scope"""
self.__scopes[-1].declarations().append(declaration)
self.__dicts[-1][declaration.name()] = declaration
def push(self, scope):
"""push new scope on the stack"""
self.__scopes.append(scope)
dict = self.__dict_map.setdefault(id(scope), {})
self.__dicts.append(dict)
def pop(self):
"""restore the previous scope"""
del self.__scopes[-1]
del self.__dicts[-1]
def top(self):
return self.__scopes[-1]
def top_dict(self):
return self.__dicts[-1]
def link_type(self, type):
"Returns the same or new proxy type"
self.__type = type
if type is not None: type.accept(self)
return self.__type
#################### Type Visitor ###########################################
def visitBaseType(self, type):
if self.types.has_key(type.name()):
self.__type = self.types[type.name()]
def visitUnknown(self, type):
if self.types.has_key(type.name()):
self.__type = self.types[type.name()]
def visitDeclared(self, type):
if self.types.has_key(type.name()):
self.__type = self.types[type.name()]
else:
print "Couldn't find declared type:",type.name()
def visitTemplate(self, type):
# Should be a Declared with the same name
if not self.types.has_key(type.name()):
return
declared = self.types[type.name()]
if isinstance(declared, Type.Unknown):
#the type was declared in a file for which no AST is retained
return
elif not isinstance(declared, Type.Declared):
print "Warning: template declaration was not a declaration:",type.name(),declared.__class__.__name__
return
decl = declared.declaration()
if not hasattr(decl, 'template'):
#print "Warning: non-class/function template",type.name(), decl.__class__.__name__
return
if decl.template():
self.__type = decl.template()
else:
print "Warning: template type disappeared:",type.name()
def visitModifier(self, type):
alias = self.link_type(type.alias())
if alias is not type.alias():
type.set_alias(alias)
self.__type = type
def visitArray(self, type):
alias = self.link_type(type.alias())
if alias is not type.alias():
type.set_alias(alias)
self.__type = type
def visitParametrized(self, type):
templ = self.link_type(type.template())
if templ is not type.template():
type.set_template(templ)
params = tuple(type.parameters())
del type.parameters()[:]
for param in params:
type.parameters().append(self.link_type(param))
self.__type = type
def visitFunctionType(self, type):
ret = self.link_type(type.returnType())
if ret is not type.returnType():
type.set_returnType(ret)
params = tuple(type.parameters())
del type.parameters()[:]
for param in params:
type.parameters().append(self.link_type(param))
self.__type = type
#################### AST Visitor ############################################
def visitSourceFile(self, file):
"""Resolves any duplicates in the list of declarations from this
file"""
types = self.types
# Clear the list and refill it
old_decls = list(file.declarations())
new_decls = file.declarations()
new_decls[:] = []
for decl in old_decls:
# Try to find a replacement declaration
if types.has_key(decl.name()):
declared = types[decl.name()]
if isinstance(type, Type.Declared):
decl = declared.declaration()
new_decls.append(decl)
# TODO: includes.
def visitModule(self, module):
#hmm, we assume that the parent node is a MetaModule. Can that ever fail ?
metamodule = self.lookup(module.name())
if metamodule is None:
metamodule = AST.MetaModule(module.language(), module.type(),module.name())
self.append(metamodule)
elif not isinstance(metamodule, AST.MetaModule):
raise TypeError, 'symbol type mismatch: Synopsis.AST.Module and %s both match "%s"'%(metamodule.__class__, '::'.join(module.name()))
metamodule.module_declarations().append(module)
self.merge_comments(metamodule.comments(), module.comments())
self.push(metamodule)
decls = tuple(module.declarations())
del module.declarations()[:]
for decl in decls: decl.accept(self)
self.pop()
def merge_comments(self, dest, src):
"""Merges the src comments into dest. Merge is just an append, unless
src already exists inside dest!"""
texter = lambda x: x.text()
dest_str = map(texter, dest)
src_str = map(texter, src)
if dest_str[-len(src):] == src_str: return
dest.extend(src)
def visitMetaModule(self, module):
#hmm, we assume that the parent node is a MetaModule. Can that ever fail ?
metamodule = self.lookup(module.name())
if metamodule is None:
metamodule = AST.MetaModule(module.language(), module.type(),module.name())
self.append(metamodule)
elif not isinstance(metamodule, AST.MetaModule):
raise TypeError, 'symbol type mismatch: Synopsis.AST.MetaModule and %s both match "%s"'%(metamodule.__class__, '::'.join(module.name()))
metamodule.module_declarations().extend(module.module_declarations())
metamodule.comments().extend(module.comments())
self.push(metamodule)
decls = tuple(module.declarations())
del module.declarations()[:]
for decl in decls: decl.accept(self)
self.pop()
def add_declaration(self, decl):
"""Adds a declaration to the current (top) scope.
If there is already a Forward declaration, then this replaces it
unless this is also a Forward.
"""
name = decl.name()
dict = self.__dicts[-1]
decls = self.top().declarations()
if dict.has_key(name):
prev = dict[name]
if not isinstance(prev, AST.Forward):
return
if not isinstance(decl, AST.Forward):
decls.remove(prev)
decls.append(decl)
dict[name] = decl # overwrite prev
return
decls.append(decl)
dict[name] = decl
def visitBuiltin(self, builtin):
"""preserve builtins unconditionally"""
decls = self.top().declarations()
decls.append(builtin)
def visitNamed(self, decl):
name = decl.name()
if self.lookup(decl.name()): return
self.add_declaration(decl)
visitDeclaration = add_declaration
visitForward = add_declaration
visitEnum = add_declaration
def visitFunction(self, func):
if not isinstance(self.top(), AST.Class):
for decl in self.top().declarations():
if not isinstance(decl, AST.Function): continue
if func.name() == decl.name():
return
ret = self.link_type(func.returnType())
if ret is not func.returnType():
func.set_returnType(ret)
for param in func.parameters():
self.visitParameter(param)
self.top().declarations().append(func)
visitOperation = visitFunction
def visitVariable(self, var):
#if not scopedNameOkay(var.name()): return
vt = self.link_type(var.vtype())
if vt is not var.vtype():
var.set_vtype(vt)
self.add_declaration(var)
def visitTypedef(self, tdef):
alias = self.link_type(tdef.alias())
if alias is not tdef.alias():
tdef.set_alias(alias)
self.add_declaration(tdef)
def visitClass(self, clas):
name = clas.name()
prev = self.lookup(name)
if prev:
if isinstance(prev, AST.Forward):
# Forward declaration, replace it
self.top().declarations().remove(prev)
del self.top_dict()[name]
elif isinstance(prev, AST.Class):
# Previous class. Would ignore duplicate but clas may have
# class declarations that prev doesn't. (forward declared
# nested -- see ThreadData.hh for example)
self.push(prev)
for decl in clas.declarations():
if isinstance(decl, AST.Class):
decl.accept(self)
self.pop()
return
else:
raise TypeError, 'symbol type mismatch: Synopsis.AST.Class and %s both match "%s"'%(prev.__class__, '::'.join(name))
self.add_declaration(clas)
for parent in clas.parents():
parent.accept(self)
self.push(clas)
decls = tuple(clas.declarations())
del clas.declarations()[:]
for decl in decls: decl.accept(self)
self.pop()
def visitInheritance(self, parent):
type = parent.parent()
if isinstance(type, Type.Declared) or isinstance(type, Type.Unknown):
ltype = self.link_type(type)
if ltype is not type:
parent.set_parent(ltype)
elif isinstance(type, Type.Parametrized):
ltype = self.link_type(type.template())
if ltype is not type.template():
# Must find a Type.Template from it
if not isinstance(ltype, Type.Declared):
# Error
return
decl = ltype.declaration()
if isinstance(decl, AST.Class):
type.set_template(decl.template())
else:
# Unknown type in class inheritance
pass
def visitParameter(self, param):
type = self.link_type(param.type())
if type is not param.type():
param.set_type(type)
def visitConst(self, const):
ct = self.link_type(const.ctype())
if ct is not const.ctype():
const.set_ctype(ct)
self.add_declaration(const)
|