Symtab.py 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518
  1. #
  2. # Symbol Table
  3. #
  4. from __future__ import absolute_import
  5. import re
  6. import copy
  7. import operator
  8. try:
  9. import __builtin__ as builtins
  10. except ImportError: # Py3
  11. import builtins
  12. from .Errors import warning, error, InternalError
  13. from .StringEncoding import EncodedString
  14. from . import Options, Naming
  15. from . import PyrexTypes
  16. from .PyrexTypes import py_object_type, unspecified_type
  17. from .TypeSlots import (
  18. pyfunction_signature, pymethod_signature, richcmp_special_methods,
  19. get_special_method_signature, get_property_accessor_signature)
  20. from . import Code
  21. iso_c99_keywords = set(
  22. ['auto', 'break', 'case', 'char', 'const', 'continue', 'default', 'do',
  23. 'double', 'else', 'enum', 'extern', 'float', 'for', 'goto', 'if',
  24. 'int', 'long', 'register', 'return', 'short', 'signed', 'sizeof',
  25. 'static', 'struct', 'switch', 'typedef', 'union', 'unsigned', 'void',
  26. 'volatile', 'while',
  27. '_Bool', '_Complex'', _Imaginary', 'inline', 'restrict'])
  28. def c_safe_identifier(cname):
  29. # There are some C limitations on struct entry names.
  30. if ((cname[:2] == '__' and not (cname.startswith(Naming.pyrex_prefix)
  31. or cname in ('__weakref__', '__dict__')))
  32. or cname in iso_c99_keywords):
  33. cname = Naming.pyrex_prefix + cname
  34. return cname
  35. class BufferAux(object):
  36. writable_needed = False
  37. def __init__(self, buflocal_nd_var, rcbuf_var):
  38. self.buflocal_nd_var = buflocal_nd_var
  39. self.rcbuf_var = rcbuf_var
  40. def __repr__(self):
  41. return "<BufferAux %r>" % self.__dict__
  42. class Entry(object):
  43. # A symbol table entry in a Scope or ModuleNamespace.
  44. #
  45. # name string Python name of entity
  46. # cname string C name of entity
  47. # type PyrexType Type of entity
  48. # doc string Doc string
  49. # annotation ExprNode PEP 484/526 annotation
  50. # init string Initial value
  51. # visibility 'private' or 'public' or 'extern'
  52. # is_builtin boolean Is an entry in the Python builtins dict
  53. # is_cglobal boolean Is a C global variable
  54. # is_pyglobal boolean Is a Python module-level variable
  55. # or class attribute during
  56. # class construction
  57. # is_member boolean Is an assigned class member
  58. # is_pyclass_attr boolean Is a name in a Python class namespace
  59. # is_variable boolean Is a variable
  60. # is_cfunction boolean Is a C function
  61. # is_cmethod boolean Is a C method of an extension type
  62. # is_builtin_cmethod boolean Is a C method of a builtin type (implies is_cmethod)
  63. # is_unbound_cmethod boolean Is an unbound C method of an extension type
  64. # is_final_cmethod boolean Is non-overridable C method
  65. # is_inline_cmethod boolean Is inlined C method
  66. # is_anonymous boolean Is a anonymous pyfunction entry
  67. # is_type boolean Is a type definition
  68. # is_cclass boolean Is an extension class
  69. # is_cpp_class boolean Is a C++ class
  70. # is_const boolean Is a constant
  71. # is_property boolean Is a property of an extension type:
  72. # doc_cname string or None C const holding the docstring
  73. # getter_cname string C func for getting property
  74. # setter_cname string C func for setting or deleting property
  75. # is_self_arg boolean Is the "self" arg of an exttype method
  76. # is_arg boolean Is the arg of a method
  77. # is_local boolean Is a local variable
  78. # in_closure boolean Is referenced in an inner scope
  79. # in_subscope boolean Belongs to a generator expression scope
  80. # is_readonly boolean Can't be assigned to
  81. # func_cname string C func implementing Python func
  82. # func_modifiers [string] C function modifiers ('inline')
  83. # pos position Source position where declared
  84. # namespace_cname string If is_pyglobal, the C variable
  85. # holding its home namespace
  86. # pymethdef_cname string PyMethodDef structure
  87. # signature Signature Arg & return types for Python func
  88. # as_variable Entry Alternative interpretation of extension
  89. # type name or builtin C function as a variable
  90. # xdecref_cleanup boolean Use Py_XDECREF for error cleanup
  91. # in_cinclude boolean Suppress C declaration code
  92. # enum_values [Entry] For enum types, list of values
  93. # qualified_name string "modname.funcname" or "modname.classname"
  94. # or "modname.classname.funcname"
  95. # is_declared_generic boolean Is declared as PyObject * even though its
  96. # type is an extension type
  97. # as_module None Module scope, if a cimported module
  98. # is_inherited boolean Is an inherited attribute of an extension type
  99. # pystring_cname string C name of Python version of string literal
  100. # is_interned boolean For string const entries, value is interned
  101. # is_identifier boolean For string const entries, value is an identifier
  102. # used boolean
  103. # is_special boolean Is a special method or property accessor
  104. # of an extension type
  105. # defined_in_pxd boolean Is defined in a .pxd file (not just declared)
  106. # api boolean Generate C API for C class or function
  107. # utility_code string Utility code needed when this entry is used
  108. #
  109. # buffer_aux BufferAux or None Extra information needed for buffer variables
  110. # inline_func_in_pxd boolean Hacky special case for inline function in pxd file.
  111. # Ideally this should not be necessary.
  112. # might_overflow boolean In an arithmetic expression that could cause
  113. # overflow (used for type inference).
  114. # utility_code_definition For some Cython builtins, the utility code
  115. # which contains the definition of the entry.
  116. # Currently only supported for CythonScope entries.
  117. # error_on_uninitialized Have Control Flow issue an error when this entry is
  118. # used uninitialized
  119. # cf_used boolean Entry is used
  120. # is_fused_specialized boolean Whether this entry of a cdef or def function
  121. # is a specialization
  122. # TODO: utility_code and utility_code_definition serves the same purpose...
  123. inline_func_in_pxd = False
  124. borrowed = 0
  125. init = ""
  126. annotation = None
  127. visibility = 'private'
  128. is_builtin = 0
  129. is_cglobal = 0
  130. is_pyglobal = 0
  131. is_member = 0
  132. is_pyclass_attr = 0
  133. is_variable = 0
  134. is_cfunction = 0
  135. is_cmethod = 0
  136. is_builtin_cmethod = False
  137. is_unbound_cmethod = 0
  138. is_final_cmethod = 0
  139. is_inline_cmethod = 0
  140. is_anonymous = 0
  141. is_type = 0
  142. is_cclass = 0
  143. is_cpp_class = 0
  144. is_const = 0
  145. is_property = 0
  146. doc_cname = None
  147. getter_cname = None
  148. setter_cname = None
  149. is_self_arg = 0
  150. is_arg = 0
  151. is_local = 0
  152. in_closure = 0
  153. from_closure = 0
  154. in_subscope = 0
  155. is_declared_generic = 0
  156. is_readonly = 0
  157. pyfunc_cname = None
  158. func_cname = None
  159. func_modifiers = []
  160. final_func_cname = None
  161. doc = None
  162. as_variable = None
  163. xdecref_cleanup = 0
  164. in_cinclude = 0
  165. as_module = None
  166. is_inherited = 0
  167. pystring_cname = None
  168. is_identifier = 0
  169. is_interned = 0
  170. used = 0
  171. is_special = 0
  172. defined_in_pxd = 0
  173. is_implemented = 0
  174. api = 0
  175. utility_code = None
  176. is_overridable = 0
  177. buffer_aux = None
  178. prev_entry = None
  179. might_overflow = 0
  180. fused_cfunction = None
  181. is_fused_specialized = False
  182. utility_code_definition = None
  183. needs_property = False
  184. in_with_gil_block = 0
  185. from_cython_utility_code = None
  186. error_on_uninitialized = False
  187. cf_used = True
  188. outer_entry = None
  189. def __init__(self, name, cname, type, pos = None, init = None):
  190. self.name = name
  191. self.cname = cname
  192. self.type = type
  193. self.pos = pos
  194. self.init = init
  195. self.overloaded_alternatives = []
  196. self.cf_assignments = []
  197. self.cf_references = []
  198. self.inner_entries = []
  199. self.defining_entry = self
  200. def __repr__(self):
  201. return "%s(<%x>, name=%s, type=%s)" % (type(self).__name__, id(self), self.name, self.type)
  202. def already_declared_here(self):
  203. error(self.pos, "Previous declaration is here")
  204. def redeclared(self, pos):
  205. error(pos, "'%s' does not match previous declaration" % self.name)
  206. self.already_declared_here()
  207. def all_alternatives(self):
  208. return [self] + self.overloaded_alternatives
  209. def all_entries(self):
  210. return [self] + self.inner_entries
  211. def __lt__(left, right):
  212. if isinstance(left, Entry) and isinstance(right, Entry):
  213. return (left.name, left.cname) < (right.name, right.cname)
  214. else:
  215. return NotImplemented
  216. class InnerEntry(Entry):
  217. """
  218. An entry in a closure scope that represents the real outer Entry.
  219. """
  220. from_closure = True
  221. def __init__(self, outer_entry, scope):
  222. Entry.__init__(self, outer_entry.name,
  223. outer_entry.cname,
  224. outer_entry.type,
  225. outer_entry.pos)
  226. self.outer_entry = outer_entry
  227. self.scope = scope
  228. # share state with (outermost) defining entry
  229. outermost_entry = outer_entry
  230. while outermost_entry.outer_entry:
  231. outermost_entry = outermost_entry.outer_entry
  232. self.defining_entry = outermost_entry
  233. self.inner_entries = outermost_entry.inner_entries
  234. self.cf_assignments = outermost_entry.cf_assignments
  235. self.cf_references = outermost_entry.cf_references
  236. self.overloaded_alternatives = outermost_entry.overloaded_alternatives
  237. self.inner_entries.append(self)
  238. def __getattr__(self, name):
  239. if name.startswith('__'):
  240. # we wouldn't have been called if it was there
  241. raise AttributeError(name)
  242. return getattr(self.defining_entry, name)
  243. def all_entries(self):
  244. return self.defining_entry.all_entries()
  245. class Scope(object):
  246. # name string Unqualified name
  247. # outer_scope Scope or None Enclosing scope
  248. # entries {string : Entry} Python name to entry, non-types
  249. # const_entries [Entry] Constant entries
  250. # type_entries [Entry] Struct/union/enum/typedef/exttype entries
  251. # sue_entries [Entry] Struct/union/enum entries
  252. # arg_entries [Entry] Function argument entries
  253. # var_entries [Entry] User-defined variable entries
  254. # pyfunc_entries [Entry] Python function entries
  255. # cfunc_entries [Entry] C function entries
  256. # c_class_entries [Entry] All extension type entries
  257. # cname_to_entry {string : Entry} Temp cname to entry mapping
  258. # return_type PyrexType or None Return type of function owning scope
  259. # is_builtin_scope boolean Is the builtin scope of Python/Cython
  260. # is_py_class_scope boolean Is a Python class scope
  261. # is_c_class_scope boolean Is an extension type scope
  262. # is_closure_scope boolean Is a closure scope
  263. # is_passthrough boolean Outer scope is passed directly
  264. # is_cpp_class_scope boolean Is a C++ class scope
  265. # is_property_scope boolean Is a extension type property scope
  266. # scope_prefix string Disambiguator for C names
  267. # in_cinclude boolean Suppress C declaration code
  268. # qualified_name string "modname" or "modname.classname"
  269. # Python strings in this scope
  270. # nogil boolean In a nogil section
  271. # directives dict Helper variable for the recursive
  272. # analysis, contains directive values.
  273. # is_internal boolean Is only used internally (simpler setup)
  274. is_builtin_scope = 0
  275. is_py_class_scope = 0
  276. is_c_class_scope = 0
  277. is_closure_scope = 0
  278. is_genexpr_scope = 0
  279. is_passthrough = 0
  280. is_cpp_class_scope = 0
  281. is_property_scope = 0
  282. is_module_scope = 0
  283. is_internal = 0
  284. scope_prefix = ""
  285. in_cinclude = 0
  286. nogil = 0
  287. fused_to_specific = None
  288. return_type = None
  289. def __init__(self, name, outer_scope, parent_scope):
  290. # The outer_scope is the next scope in the lookup chain.
  291. # The parent_scope is used to derive the qualified name of this scope.
  292. self.name = name
  293. self.outer_scope = outer_scope
  294. self.parent_scope = parent_scope
  295. mangled_name = "%d%s_" % (len(name), name.replace('.', '_dot_'))
  296. qual_scope = self.qualifying_scope()
  297. if qual_scope:
  298. self.qualified_name = qual_scope.qualify_name(name)
  299. self.scope_prefix = qual_scope.scope_prefix + mangled_name
  300. else:
  301. self.qualified_name = EncodedString(name)
  302. self.scope_prefix = mangled_name
  303. self.entries = {}
  304. self.subscopes = set()
  305. self.const_entries = []
  306. self.type_entries = []
  307. self.sue_entries = []
  308. self.arg_entries = []
  309. self.var_entries = []
  310. self.pyfunc_entries = []
  311. self.cfunc_entries = []
  312. self.c_class_entries = []
  313. self.defined_c_classes = []
  314. self.imported_c_classes = {}
  315. self.cname_to_entry = {}
  316. self.string_to_entry = {}
  317. self.identifier_to_entry = {}
  318. self.num_to_entry = {}
  319. self.obj_to_entry = {}
  320. self.buffer_entries = []
  321. self.lambda_defs = []
  322. self.id_counters = {}
  323. def __deepcopy__(self, memo):
  324. return self
  325. def merge_in(self, other, merge_unused=True, whitelist=None):
  326. # Use with care...
  327. entries = []
  328. for name, entry in other.entries.items():
  329. if not whitelist or name in whitelist:
  330. if entry.used or merge_unused:
  331. entries.append((name, entry))
  332. self.entries.update(entries)
  333. for attr in ('const_entries',
  334. 'type_entries',
  335. 'sue_entries',
  336. 'arg_entries',
  337. 'var_entries',
  338. 'pyfunc_entries',
  339. 'cfunc_entries',
  340. 'c_class_entries'):
  341. self_entries = getattr(self, attr)
  342. names = set(e.name for e in self_entries)
  343. for entry in getattr(other, attr):
  344. if (entry.used or merge_unused) and entry.name not in names:
  345. self_entries.append(entry)
  346. def __str__(self):
  347. return "<%s %s>" % (self.__class__.__name__, self.qualified_name)
  348. def qualifying_scope(self):
  349. return self.parent_scope
  350. def mangle(self, prefix, name = None):
  351. if name:
  352. return "%s%s%s" % (prefix, self.scope_prefix, name)
  353. else:
  354. return self.parent_scope.mangle(prefix, self.name)
  355. def mangle_internal(self, name):
  356. # Mangle an internal name so as not to clash with any
  357. # user-defined name in this scope.
  358. prefix = "%s%s_" % (Naming.pyrex_prefix, name)
  359. return self.mangle(prefix)
  360. #return self.parent_scope.mangle(prefix, self.name)
  361. def mangle_class_private_name(self, name):
  362. if self.parent_scope:
  363. return self.parent_scope.mangle_class_private_name(name)
  364. return name
  365. def next_id(self, name=None):
  366. # Return a cname fragment that is unique for this module
  367. counters = self.global_scope().id_counters
  368. try:
  369. count = counters[name] + 1
  370. except KeyError:
  371. count = 0
  372. counters[name] = count
  373. if name:
  374. if not count:
  375. # unique names don't need a suffix, reoccurrences will get one
  376. return name
  377. return '%s%d' % (name, count)
  378. else:
  379. return '%d' % count
  380. def global_scope(self):
  381. """ Return the module-level scope containing this scope. """
  382. return self.outer_scope.global_scope()
  383. def builtin_scope(self):
  384. """ Return the module-level scope containing this scope. """
  385. return self.outer_scope.builtin_scope()
  386. def iter_local_scopes(self):
  387. yield self
  388. if self.subscopes:
  389. for scope in sorted(self.subscopes, key=operator.attrgetter('scope_prefix')):
  390. yield scope
  391. def declare(self, name, cname, type, pos, visibility, shadow = 0, is_type = 0, create_wrapper = 0):
  392. # Create new entry, and add to dictionary if
  393. # name is not None. Reports a warning if already
  394. # declared.
  395. if type.is_buffer and not isinstance(self, LocalScope): # and not is_type:
  396. error(pos, 'Buffer types only allowed as function local variables')
  397. if not self.in_cinclude and cname and re.match("^_[_A-Z]+$", cname):
  398. # See http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html#Reserved-Names
  399. warning(pos, "'%s' is a reserved name in C." % cname, -1)
  400. entries = self.entries
  401. if name and name in entries and not shadow:
  402. old_entry = entries[name]
  403. # Reject redeclared C++ functions only if they have the same type signature.
  404. cpp_override_allowed = False
  405. if type.is_cfunction and old_entry.type.is_cfunction and self.is_cpp():
  406. for alt_entry in old_entry.all_alternatives():
  407. if type == alt_entry.type:
  408. if name == '<init>' and not type.args:
  409. # Cython pre-declares the no-args constructor - allow later user definitions.
  410. cpp_override_allowed = True
  411. break
  412. else:
  413. cpp_override_allowed = True
  414. if cpp_override_allowed:
  415. # C++ function/method overrides with different signatures are ok.
  416. pass
  417. elif self.is_cpp_class_scope and entries[name].is_inherited:
  418. # Likewise ignore inherited classes.
  419. pass
  420. elif visibility == 'extern':
  421. # Silenced outside of "cdef extern" blocks, until we have a safe way to
  422. # prevent pxd-defined cpdef functions from ending up here.
  423. warning(pos, "'%s' redeclared " % name, 1 if self.in_cinclude else 0)
  424. elif visibility != 'ignore':
  425. error(pos, "'%s' redeclared " % name)
  426. entries[name].already_declared_here()
  427. entry = Entry(name, cname, type, pos = pos)
  428. entry.in_cinclude = self.in_cinclude
  429. entry.create_wrapper = create_wrapper
  430. if name:
  431. entry.qualified_name = self.qualify_name(name)
  432. # if name in entries and self.is_cpp():
  433. # entries[name].overloaded_alternatives.append(entry)
  434. # else:
  435. # entries[name] = entry
  436. if not shadow:
  437. entries[name] = entry
  438. if type.is_memoryviewslice:
  439. from . import MemoryView
  440. entry.init = MemoryView.memslice_entry_init
  441. entry.scope = self
  442. entry.visibility = visibility
  443. return entry
  444. def qualify_name(self, name):
  445. return EncodedString("%s.%s" % (self.qualified_name, name))
  446. def declare_const(self, name, type, value, pos, cname = None, visibility = 'private', api = 0, create_wrapper = 0):
  447. # Add an entry for a named constant.
  448. if not cname:
  449. if self.in_cinclude or (visibility == 'public' or api):
  450. cname = name
  451. else:
  452. cname = self.mangle(Naming.enum_prefix, name)
  453. entry = self.declare(name, cname, type, pos, visibility, create_wrapper = create_wrapper)
  454. entry.is_const = 1
  455. entry.value_node = value
  456. return entry
  457. def declare_type(self, name, type, pos,
  458. cname = None, visibility = 'private', api = 0, defining = 1,
  459. shadow = 0, template = 0):
  460. # Add an entry for a type definition.
  461. if not cname:
  462. cname = name
  463. entry = self.declare(name, cname, type, pos, visibility, shadow,
  464. is_type=True)
  465. entry.is_type = 1
  466. entry.api = api
  467. if defining:
  468. self.type_entries.append(entry)
  469. if not template:
  470. type.entry = entry
  471. # here we would set as_variable to an object representing this type
  472. return entry
  473. def declare_typedef(self, name, base_type, pos, cname = None,
  474. visibility = 'private', api = 0):
  475. if not cname:
  476. if self.in_cinclude or (visibility != 'private' or api):
  477. cname = name
  478. else:
  479. cname = self.mangle(Naming.type_prefix, name)
  480. try:
  481. if self.is_cpp_class_scope:
  482. namespace = self.outer_scope.lookup(self.name).type
  483. else:
  484. namespace = None
  485. type = PyrexTypes.create_typedef_type(name, base_type, cname,
  486. (visibility == 'extern'),
  487. namespace)
  488. except ValueError as e:
  489. error(pos, e.args[0])
  490. type = PyrexTypes.error_type
  491. entry = self.declare_type(name, type, pos, cname,
  492. visibility = visibility, api = api)
  493. type.qualified_name = entry.qualified_name
  494. return entry
  495. def declare_struct_or_union(self, name, kind, scope,
  496. typedef_flag, pos, cname = None,
  497. visibility = 'private', api = 0,
  498. packed = False):
  499. # Add an entry for a struct or union definition.
  500. if not cname:
  501. if self.in_cinclude or (visibility == 'public' or api):
  502. cname = name
  503. else:
  504. cname = self.mangle(Naming.type_prefix, name)
  505. entry = self.lookup_here(name)
  506. if not entry:
  507. type = PyrexTypes.CStructOrUnionType(
  508. name, kind, scope, typedef_flag, cname, packed)
  509. entry = self.declare_type(name, type, pos, cname,
  510. visibility = visibility, api = api,
  511. defining = scope is not None)
  512. self.sue_entries.append(entry)
  513. type.entry = entry
  514. else:
  515. if not (entry.is_type and entry.type.is_struct_or_union
  516. and entry.type.kind == kind):
  517. warning(pos, "'%s' redeclared " % name, 0)
  518. elif scope and entry.type.scope:
  519. warning(pos, "'%s' already defined (ignoring second definition)" % name, 0)
  520. else:
  521. self.check_previous_typedef_flag(entry, typedef_flag, pos)
  522. self.check_previous_visibility(entry, visibility, pos)
  523. if scope:
  524. entry.type.scope = scope
  525. self.type_entries.append(entry)
  526. if self.is_cpp_class_scope:
  527. entry.type.namespace = self.outer_scope.lookup(self.name).type
  528. return entry
  529. def declare_cpp_class(self, name, scope,
  530. pos, cname = None, base_classes = (),
  531. visibility = 'extern', templates = None):
  532. if cname is None:
  533. if self.in_cinclude or (visibility != 'private'):
  534. cname = name
  535. else:
  536. cname = self.mangle(Naming.type_prefix, name)
  537. base_classes = list(base_classes)
  538. entry = self.lookup_here(name)
  539. if not entry:
  540. type = PyrexTypes.CppClassType(
  541. name, scope, cname, base_classes, templates = templates)
  542. entry = self.declare_type(name, type, pos, cname,
  543. visibility = visibility, defining = scope is not None)
  544. self.sue_entries.append(entry)
  545. else:
  546. if not (entry.is_type and entry.type.is_cpp_class):
  547. error(pos, "'%s' redeclared " % name)
  548. entry.already_declared_here()
  549. return None
  550. elif scope and entry.type.scope:
  551. warning(pos, "'%s' already defined (ignoring second definition)" % name, 0)
  552. else:
  553. if scope:
  554. entry.type.scope = scope
  555. self.type_entries.append(entry)
  556. if base_classes:
  557. if entry.type.base_classes and entry.type.base_classes != base_classes:
  558. error(pos, "Base type does not match previous declaration")
  559. entry.already_declared_here()
  560. else:
  561. entry.type.base_classes = base_classes
  562. if templates or entry.type.templates:
  563. if templates != entry.type.templates:
  564. error(pos, "Template parameters do not match previous declaration")
  565. entry.already_declared_here()
  566. def declare_inherited_attributes(entry, base_classes):
  567. for base_class in base_classes:
  568. if base_class is PyrexTypes.error_type:
  569. continue
  570. if base_class.scope is None:
  571. error(pos, "Cannot inherit from incomplete type")
  572. else:
  573. declare_inherited_attributes(entry, base_class.base_classes)
  574. entry.type.scope.declare_inherited_cpp_attributes(base_class)
  575. if scope:
  576. declare_inherited_attributes(entry, base_classes)
  577. scope.declare_var(name="this", cname="this", type=PyrexTypes.CPtrType(entry.type), pos=entry.pos)
  578. if self.is_cpp_class_scope:
  579. entry.type.namespace = self.outer_scope.lookup(self.name).type
  580. return entry
  581. def check_previous_typedef_flag(self, entry, typedef_flag, pos):
  582. if typedef_flag != entry.type.typedef_flag:
  583. error(pos, "'%s' previously declared using '%s'" % (
  584. entry.name, ("cdef", "ctypedef")[entry.type.typedef_flag]))
  585. def check_previous_visibility(self, entry, visibility, pos):
  586. if entry.visibility != visibility:
  587. error(pos, "'%s' previously declared as '%s'" % (
  588. entry.name, entry.visibility))
  589. def declare_enum(self, name, pos, cname, typedef_flag,
  590. visibility = 'private', api = 0, create_wrapper = 0):
  591. if name:
  592. if not cname:
  593. if (self.in_cinclude or visibility == 'public'
  594. or visibility == 'extern' or api):
  595. cname = name
  596. else:
  597. cname = self.mangle(Naming.type_prefix, name)
  598. if self.is_cpp_class_scope:
  599. namespace = self.outer_scope.lookup(self.name).type
  600. else:
  601. namespace = None
  602. type = PyrexTypes.CEnumType(name, cname, typedef_flag, namespace)
  603. else:
  604. type = PyrexTypes.c_anon_enum_type
  605. entry = self.declare_type(name, type, pos, cname = cname,
  606. visibility = visibility, api = api)
  607. entry.create_wrapper = create_wrapper
  608. entry.enum_values = []
  609. self.sue_entries.append(entry)
  610. return entry
  611. def declare_tuple_type(self, pos, components):
  612. return self.outer_scope.declare_tuple_type(pos, components)
  613. def declare_var(self, name, type, pos,
  614. cname = None, visibility = 'private',
  615. api = 0, in_pxd = 0, is_cdef = 0):
  616. # Add an entry for a variable.
  617. if not cname:
  618. if visibility != 'private' or api:
  619. cname = name
  620. else:
  621. cname = self.mangle(Naming.var_prefix, name)
  622. if type.is_cpp_class and visibility != 'extern':
  623. type.check_nullary_constructor(pos)
  624. entry = self.declare(name, cname, type, pos, visibility)
  625. entry.is_variable = 1
  626. if in_pxd and visibility != 'extern':
  627. entry.defined_in_pxd = 1
  628. entry.used = 1
  629. if api:
  630. entry.api = 1
  631. entry.used = 1
  632. return entry
  633. def declare_builtin(self, name, pos):
  634. return self.outer_scope.declare_builtin(name, pos)
  635. def _declare_pyfunction(self, name, pos, visibility='extern', entry=None):
  636. if entry and not entry.type.is_cfunction:
  637. error(pos, "'%s' already declared" % name)
  638. error(entry.pos, "Previous declaration is here")
  639. entry = self.declare_var(name, py_object_type, pos, visibility=visibility)
  640. entry.signature = pyfunction_signature
  641. self.pyfunc_entries.append(entry)
  642. return entry
  643. def declare_pyfunction(self, name, pos, allow_redefine=False, visibility='extern'):
  644. # Add an entry for a Python function.
  645. entry = self.lookup_here(name)
  646. if not allow_redefine:
  647. return self._declare_pyfunction(name, pos, visibility=visibility, entry=entry)
  648. if entry:
  649. if entry.type.is_unspecified:
  650. entry.type = py_object_type
  651. elif entry.type is not py_object_type:
  652. return self._declare_pyfunction(name, pos, visibility=visibility, entry=entry)
  653. else: # declare entry stub
  654. self.declare_var(name, py_object_type, pos, visibility=visibility)
  655. entry = self.declare_var(None, py_object_type, pos,
  656. cname=name, visibility='private')
  657. entry.name = EncodedString(name)
  658. entry.qualified_name = self.qualify_name(name)
  659. entry.signature = pyfunction_signature
  660. entry.is_anonymous = True
  661. return entry
  662. def declare_lambda_function(self, lambda_name, pos):
  663. # Add an entry for an anonymous Python function.
  664. func_cname = self.mangle(Naming.lambda_func_prefix + u'funcdef_', lambda_name)
  665. pymethdef_cname = self.mangle(Naming.lambda_func_prefix + u'methdef_', lambda_name)
  666. qualified_name = self.qualify_name(lambda_name)
  667. entry = self.declare(None, func_cname, py_object_type, pos, 'private')
  668. entry.name = lambda_name
  669. entry.qualified_name = qualified_name
  670. entry.pymethdef_cname = pymethdef_cname
  671. entry.func_cname = func_cname
  672. entry.signature = pyfunction_signature
  673. entry.is_anonymous = True
  674. return entry
  675. def add_lambda_def(self, def_node):
  676. self.lambda_defs.append(def_node)
  677. def register_pyfunction(self, entry):
  678. self.pyfunc_entries.append(entry)
  679. def declare_cfunction(self, name, type, pos,
  680. cname=None, visibility='private', api=0, in_pxd=0,
  681. defining=0, modifiers=(), utility_code=None, overridable=False):
  682. # Add an entry for a C function.
  683. if not cname:
  684. if visibility != 'private' or api:
  685. cname = name
  686. else:
  687. cname = self.mangle(Naming.func_prefix, name)
  688. entry = self.lookup_here(name)
  689. if entry:
  690. if not in_pxd and visibility != entry.visibility and visibility == 'extern':
  691. # Previously declared, but now extern => treat this
  692. # as implementing the function, using the new cname
  693. defining = True
  694. visibility = entry.visibility
  695. entry.cname = cname
  696. entry.func_cname = cname
  697. if visibility != 'private' and visibility != entry.visibility:
  698. warning(pos, "Function '%s' previously declared as '%s', now as '%s'" % (name, entry.visibility, visibility), 1)
  699. if overridable != entry.is_overridable:
  700. warning(pos, "Function '%s' previously declared as '%s'" % (
  701. name, 'cpdef' if overridable else 'cdef'), 1)
  702. if entry.type.same_as(type):
  703. # Fix with_gil vs nogil.
  704. entry.type = entry.type.with_with_gil(type.with_gil)
  705. else:
  706. if visibility == 'extern' and entry.visibility == 'extern':
  707. can_override = False
  708. if self.is_cpp():
  709. can_override = True
  710. elif cname:
  711. # if all alternatives have different cnames,
  712. # it's safe to allow signature overrides
  713. for alt_entry in entry.all_alternatives():
  714. if not alt_entry.cname or cname == alt_entry.cname:
  715. break # cname not unique!
  716. else:
  717. can_override = True
  718. if can_override:
  719. temp = self.add_cfunction(name, type, pos, cname, visibility, modifiers)
  720. temp.overloaded_alternatives = entry.all_alternatives()
  721. entry = temp
  722. else:
  723. warning(pos, "Function signature does not match previous declaration", 1)
  724. entry.type = type
  725. elif not in_pxd and entry.defined_in_pxd and type.compatible_signature_with(entry.type):
  726. # TODO: check that this was done by a signature optimisation and not a user error.
  727. #warning(pos, "Function signature does not match previous declaration", 1)
  728. entry.type = type
  729. else:
  730. error(pos, "Function signature does not match previous declaration")
  731. else:
  732. entry = self.add_cfunction(name, type, pos, cname, visibility, modifiers)
  733. entry.func_cname = cname
  734. entry.is_overridable = overridable
  735. if in_pxd and visibility != 'extern':
  736. entry.defined_in_pxd = 1
  737. if api:
  738. entry.api = 1
  739. if not defining and not in_pxd and visibility != 'extern':
  740. error(pos, "Non-extern C function '%s' declared but not defined" % name)
  741. if defining:
  742. entry.is_implemented = True
  743. if modifiers:
  744. entry.func_modifiers = modifiers
  745. if utility_code:
  746. assert not entry.utility_code, "duplicate utility code definition in entry %s (%s)" % (name, cname)
  747. entry.utility_code = utility_code
  748. if overridable:
  749. # names of cpdef functions can be used as variables and can be assigned to
  750. var_entry = Entry(name, cname, py_object_type) # FIXME: cname?
  751. var_entry.is_variable = 1
  752. var_entry.is_pyglobal = 1
  753. var_entry.scope = entry.scope
  754. entry.as_variable = var_entry
  755. type.entry = entry
  756. return entry
  757. def add_cfunction(self, name, type, pos, cname, visibility, modifiers, inherited=False):
  758. # Add a C function entry without giving it a func_cname.
  759. entry = self.declare(name, cname, type, pos, visibility)
  760. entry.is_cfunction = 1
  761. if modifiers:
  762. entry.func_modifiers = modifiers
  763. if inherited or type.is_fused:
  764. self.cfunc_entries.append(entry)
  765. else:
  766. # For backwards compatibility reasons, we must keep all non-fused methods
  767. # before all fused methods, but separately for each type.
  768. i = len(self.cfunc_entries)
  769. for cfunc_entry in reversed(self.cfunc_entries):
  770. if cfunc_entry.is_inherited or not cfunc_entry.type.is_fused:
  771. break
  772. i -= 1
  773. self.cfunc_entries.insert(i, entry)
  774. return entry
  775. def find(self, name, pos):
  776. # Look up name, report error if not found.
  777. entry = self.lookup(name)
  778. if entry:
  779. return entry
  780. else:
  781. error(pos, "'%s' is not declared" % name)
  782. def find_imported_module(self, path, pos):
  783. # Look up qualified name, must be a module, report error if not found.
  784. # Path is a list of names.
  785. scope = self
  786. for name in path:
  787. entry = scope.find(name, pos)
  788. if not entry:
  789. return None
  790. if entry.as_module:
  791. scope = entry.as_module
  792. else:
  793. error(pos, "'%s' is not a cimported module" % '.'.join(path))
  794. return None
  795. return scope
  796. def lookup(self, name):
  797. # Look up name in this scope or an enclosing one.
  798. # Return None if not found.
  799. return (self.lookup_here(name)
  800. or (self.outer_scope and self.outer_scope.lookup(name))
  801. or None)
  802. def lookup_here(self, name):
  803. # Look up in this scope only, return None if not found.
  804. return self.entries.get(name, None)
  805. def lookup_target(self, name):
  806. # Look up name in this scope only. Declare as Python
  807. # variable if not found.
  808. entry = self.lookup_here(name)
  809. if not entry:
  810. entry = self.declare_var(name, py_object_type, None)
  811. return entry
  812. def lookup_type(self, name):
  813. entry = self.lookup(name)
  814. if entry and entry.is_type:
  815. if entry.type.is_fused and self.fused_to_specific:
  816. return entry.type.specialize(self.fused_to_specific)
  817. return entry.type
  818. def lookup_operator(self, operator, operands):
  819. if operands[0].type.is_cpp_class:
  820. obj_type = operands[0].type
  821. method = obj_type.scope.lookup("operator%s" % operator)
  822. if method is not None:
  823. arg_types = [arg.type for arg in operands[1:]]
  824. res = PyrexTypes.best_match([arg.type for arg in operands[1:]],
  825. method.all_alternatives())
  826. if res is not None:
  827. return res
  828. function = self.lookup("operator%s" % operator)
  829. if function is None:
  830. return None
  831. return PyrexTypes.best_match([arg.type for arg in operands],
  832. function.all_alternatives())
  833. def lookup_operator_for_types(self, pos, operator, types):
  834. from .Nodes import Node
  835. class FakeOperand(Node):
  836. pass
  837. operands = [FakeOperand(pos, type=type) for type in types]
  838. return self.lookup_operator(operator, operands)
  839. def use_utility_code(self, new_code):
  840. self.global_scope().use_utility_code(new_code)
  841. def use_entry_utility_code(self, entry):
  842. self.global_scope().use_entry_utility_code(entry)
  843. def defines_any(self, names):
  844. # Test whether any of the given names are defined in this scope.
  845. for name in names:
  846. if name in self.entries:
  847. return 1
  848. return 0
  849. def defines_any_special(self, names):
  850. # Test whether any of the given names are defined as special methods in this scope.
  851. for name in names:
  852. if name in self.entries and self.entries[name].is_special:
  853. return 1
  854. return 0
  855. def infer_types(self):
  856. from .TypeInference import get_type_inferer
  857. get_type_inferer().infer_types(self)
  858. def is_cpp(self):
  859. outer = self.outer_scope
  860. if outer is None:
  861. return False
  862. else:
  863. return outer.is_cpp()
  864. def add_include_file(self, filename):
  865. self.outer_scope.add_include_file(filename)
  866. class PreImportScope(Scope):
  867. namespace_cname = Naming.preimport_cname
  868. def __init__(self):
  869. Scope.__init__(self, Options.pre_import, None, None)
  870. def declare_builtin(self, name, pos):
  871. entry = self.declare(name, name, py_object_type, pos, 'private')
  872. entry.is_variable = True
  873. entry.is_pyglobal = True
  874. return entry
  875. class BuiltinScope(Scope):
  876. # The builtin namespace.
  877. is_builtin_scope = True
  878. def __init__(self):
  879. if Options.pre_import is None:
  880. Scope.__init__(self, "__builtin__", None, None)
  881. else:
  882. Scope.__init__(self, "__builtin__", PreImportScope(), None)
  883. self.type_names = {}
  884. for name, definition in sorted(self.builtin_entries.items()):
  885. cname, type = definition
  886. self.declare_var(name, type, None, cname)
  887. def lookup(self, name, language_level=None):
  888. # 'language_level' is passed by ModuleScope
  889. if language_level == 3:
  890. if name == 'str':
  891. name = 'unicode'
  892. return Scope.lookup(self, name)
  893. def declare_builtin(self, name, pos):
  894. if not hasattr(builtins, name):
  895. if self.outer_scope is not None:
  896. return self.outer_scope.declare_builtin(name, pos)
  897. else:
  898. if Options.error_on_unknown_names:
  899. error(pos, "undeclared name not builtin: %s" % name)
  900. else:
  901. warning(pos, "undeclared name not builtin: %s" % name, 2)
  902. def declare_builtin_cfunction(self, name, type, cname, python_equiv=None, utility_code=None):
  903. # If python_equiv == "*", the Python equivalent has the same name
  904. # as the entry, otherwise it has the name specified by python_equiv.
  905. name = EncodedString(name)
  906. entry = self.declare_cfunction(name, type, None, cname, visibility='extern',
  907. utility_code=utility_code)
  908. if python_equiv:
  909. if python_equiv == "*":
  910. python_equiv = name
  911. else:
  912. python_equiv = EncodedString(python_equiv)
  913. var_entry = Entry(python_equiv, python_equiv, py_object_type)
  914. var_entry.is_variable = 1
  915. var_entry.is_builtin = 1
  916. var_entry.utility_code = utility_code
  917. var_entry.scope = entry.scope
  918. entry.as_variable = var_entry
  919. return entry
  920. def declare_builtin_type(self, name, cname, utility_code = None, objstruct_cname = None):
  921. name = EncodedString(name)
  922. type = PyrexTypes.BuiltinObjectType(name, cname, objstruct_cname)
  923. scope = CClassScope(name, outer_scope=None, visibility='extern')
  924. scope.directives = {}
  925. if name == 'bool':
  926. type.is_final_type = True
  927. type.set_scope(scope)
  928. self.type_names[name] = 1
  929. entry = self.declare_type(name, type, None, visibility='extern')
  930. entry.utility_code = utility_code
  931. var_entry = Entry(name = entry.name,
  932. type = self.lookup('type').type, # make sure "type" is the first type declared...
  933. pos = entry.pos,
  934. cname = entry.type.typeptr_cname)
  935. var_entry.is_variable = 1
  936. var_entry.is_cglobal = 1
  937. var_entry.is_readonly = 1
  938. var_entry.is_builtin = 1
  939. var_entry.utility_code = utility_code
  940. var_entry.scope = self
  941. if Options.cache_builtins:
  942. var_entry.is_const = True
  943. entry.as_variable = var_entry
  944. return type
  945. def builtin_scope(self):
  946. return self
  947. builtin_entries = {
  948. "type": ["((PyObject*)&PyType_Type)", py_object_type],
  949. "bool": ["((PyObject*)&PyBool_Type)", py_object_type],
  950. "int": ["((PyObject*)&PyInt_Type)", py_object_type],
  951. "long": ["((PyObject*)&PyLong_Type)", py_object_type],
  952. "float": ["((PyObject*)&PyFloat_Type)", py_object_type],
  953. "complex":["((PyObject*)&PyComplex_Type)", py_object_type],
  954. "bytes": ["((PyObject*)&PyBytes_Type)", py_object_type],
  955. "bytearray": ["((PyObject*)&PyByteArray_Type)", py_object_type],
  956. "str": ["((PyObject*)&PyString_Type)", py_object_type],
  957. "unicode":["((PyObject*)&PyUnicode_Type)", py_object_type],
  958. "tuple": ["((PyObject*)&PyTuple_Type)", py_object_type],
  959. "list": ["((PyObject*)&PyList_Type)", py_object_type],
  960. "dict": ["((PyObject*)&PyDict_Type)", py_object_type],
  961. "set": ["((PyObject*)&PySet_Type)", py_object_type],
  962. "frozenset": ["((PyObject*)&PyFrozenSet_Type)", py_object_type],
  963. "slice": ["((PyObject*)&PySlice_Type)", py_object_type],
  964. # "file": ["((PyObject*)&PyFile_Type)", py_object_type], # not in Py3
  965. "None": ["Py_None", py_object_type],
  966. "False": ["Py_False", py_object_type],
  967. "True": ["Py_True", py_object_type],
  968. }
  969. const_counter = 1 # As a temporary solution for compiling code in pxds
  970. class ModuleScope(Scope):
  971. # module_name string Python name of the module
  972. # module_cname string C name of Python module object
  973. # #module_dict_cname string C name of module dict object
  974. # method_table_cname string C name of method table
  975. # doc string Module doc string
  976. # doc_cname string C name of module doc string
  977. # utility_code_list [UtilityCode] Queuing utility codes for forwarding to Code.py
  978. # c_includes {key: IncludeCode} C headers or verbatim code to be generated
  979. # See process_include() for more documentation
  980. # string_to_entry {string : Entry} Map string const to entry
  981. # identifier_to_entry {string : Entry} Map identifier string const to entry
  982. # context Context
  983. # parent_module Scope Parent in the import namespace
  984. # module_entries {string : Entry} For cimport statements
  985. # type_names {string : 1} Set of type names (used during parsing)
  986. # included_files [string] Cython sources included with 'include'
  987. # pxd_file_loaded boolean Corresponding .pxd file has been processed
  988. # cimported_modules [ModuleScope] Modules imported with cimport
  989. # types_imported {PyrexType} Set of types for which import code generated
  990. # has_import_star boolean Module contains import *
  991. # cpp boolean Compiling a C++ file
  992. # is_cython_builtin boolean Is this the Cython builtin scope (or a child scope)
  993. # is_package boolean Is this a package module? (__init__)
  994. is_module_scope = 1
  995. has_import_star = 0
  996. is_cython_builtin = 0
  997. old_style_globals = 0
  998. def __init__(self, name, parent_module, context):
  999. from . import Builtin
  1000. self.parent_module = parent_module
  1001. outer_scope = Builtin.builtin_scope
  1002. Scope.__init__(self, name, outer_scope, parent_module)
  1003. if name == "__init__":
  1004. # Treat Spam/__init__.pyx specially, so that when Python loads
  1005. # Spam/__init__.so, initSpam() is defined.
  1006. self.module_name = parent_module.module_name
  1007. self.is_package = True
  1008. else:
  1009. self.module_name = name
  1010. self.is_package = False
  1011. self.module_name = EncodedString(self.module_name)
  1012. self.context = context
  1013. self.module_cname = Naming.module_cname
  1014. self.module_dict_cname = Naming.moddict_cname
  1015. self.method_table_cname = Naming.methtable_cname
  1016. self.doc = ""
  1017. self.doc_cname = Naming.moddoc_cname
  1018. self.utility_code_list = []
  1019. self.module_entries = {}
  1020. self.c_includes = {}
  1021. self.type_names = dict(outer_scope.type_names)
  1022. self.pxd_file_loaded = 0
  1023. self.cimported_modules = []
  1024. self.types_imported = set()
  1025. self.included_files = []
  1026. self.has_extern_class = 0
  1027. self.cached_builtins = []
  1028. self.undeclared_cached_builtins = []
  1029. self.namespace_cname = self.module_cname
  1030. self._cached_tuple_types = {}
  1031. for var_name in ['__builtins__', '__name__', '__file__', '__doc__', '__path__',
  1032. '__spec__', '__loader__', '__package__', '__cached__']:
  1033. self.declare_var(EncodedString(var_name), py_object_type, None)
  1034. self.process_include(Code.IncludeCode("Python.h", initial=True))
  1035. def qualifying_scope(self):
  1036. return self.parent_module
  1037. def global_scope(self):
  1038. return self
  1039. def lookup(self, name, language_level=None):
  1040. entry = self.lookup_here(name)
  1041. if entry is not None:
  1042. return entry
  1043. if language_level is None:
  1044. language_level = self.context.language_level if self.context is not None else 3
  1045. return self.outer_scope.lookup(name, language_level=language_level)
  1046. def declare_tuple_type(self, pos, components):
  1047. components = tuple(components)
  1048. try:
  1049. ttype = self._cached_tuple_types[components]
  1050. except KeyError:
  1051. ttype = self._cached_tuple_types[components] = PyrexTypes.c_tuple_type(components)
  1052. cname = ttype.cname
  1053. entry = self.lookup_here(cname)
  1054. if not entry:
  1055. scope = StructOrUnionScope(cname)
  1056. for ix, component in enumerate(components):
  1057. scope.declare_var(name="f%s" % ix, type=component, pos=pos)
  1058. struct_entry = self.declare_struct_or_union(
  1059. cname + '_struct', 'struct', scope, typedef_flag=True, pos=pos, cname=cname)
  1060. self.type_entries.remove(struct_entry)
  1061. ttype.struct_entry = struct_entry
  1062. entry = self.declare_type(cname, ttype, pos, cname)
  1063. ttype.entry = entry
  1064. return entry
  1065. def declare_builtin(self, name, pos):
  1066. if not hasattr(builtins, name) \
  1067. and name not in Code.non_portable_builtins_map \
  1068. and name not in Code.uncachable_builtins:
  1069. if self.has_import_star:
  1070. entry = self.declare_var(name, py_object_type, pos)
  1071. return entry
  1072. else:
  1073. if Options.error_on_unknown_names:
  1074. error(pos, "undeclared name not builtin: %s" % name)
  1075. else:
  1076. warning(pos, "undeclared name not builtin: %s" % name, 2)
  1077. # unknown - assume it's builtin and look it up at runtime
  1078. entry = self.declare(name, None, py_object_type, pos, 'private')
  1079. entry.is_builtin = 1
  1080. return entry
  1081. if Options.cache_builtins:
  1082. for entry in self.cached_builtins:
  1083. if entry.name == name:
  1084. return entry
  1085. if name == 'globals' and not self.old_style_globals:
  1086. return self.outer_scope.lookup('__Pyx_Globals')
  1087. else:
  1088. entry = self.declare(None, None, py_object_type, pos, 'private')
  1089. if Options.cache_builtins and name not in Code.uncachable_builtins:
  1090. entry.is_builtin = 1
  1091. entry.is_const = 1 # cached
  1092. entry.name = name
  1093. entry.cname = Naming.builtin_prefix + name
  1094. self.cached_builtins.append(entry)
  1095. self.undeclared_cached_builtins.append(entry)
  1096. else:
  1097. entry.is_builtin = 1
  1098. entry.name = name
  1099. return entry
  1100. def find_module(self, module_name, pos, relative_level=-1):
  1101. # Find a module in the import namespace, interpreting
  1102. # relative imports relative to this module's parent.
  1103. # Finds and parses the module's .pxd file if the module
  1104. # has not been referenced before.
  1105. relative_to = None
  1106. absolute_fallback = False
  1107. if relative_level is not None and relative_level > 0:
  1108. # explicit relative cimport
  1109. # error of going beyond top-level is handled in cimport node
  1110. relative_to = self
  1111. while relative_level > 0 and relative_to:
  1112. relative_to = relative_to.parent_module
  1113. relative_level -= 1
  1114. elif relative_level != 0:
  1115. # -1 or None: try relative cimport first, then absolute
  1116. relative_to = self.parent_module
  1117. absolute_fallback = True
  1118. module_scope = self.global_scope()
  1119. return module_scope.context.find_module(
  1120. module_name, relative_to=relative_to, pos=pos, absolute_fallback=absolute_fallback)
  1121. def find_submodule(self, name):
  1122. # Find and return scope for a submodule of this module,
  1123. # creating a new empty one if necessary. Doesn't parse .pxd.
  1124. if '.' in name:
  1125. name, submodule = name.split('.', 1)
  1126. else:
  1127. submodule = None
  1128. scope = self.lookup_submodule(name)
  1129. if not scope:
  1130. scope = ModuleScope(name, parent_module=self, context=self.context)
  1131. self.module_entries[name] = scope
  1132. if submodule:
  1133. scope = scope.find_submodule(submodule)
  1134. return scope
  1135. def lookup_submodule(self, name):
  1136. # Return scope for submodule of this module, or None.
  1137. if '.' in name:
  1138. name, submodule = name.split('.', 1)
  1139. else:
  1140. submodule = None
  1141. module = self.module_entries.get(name, None)
  1142. if submodule and module is not None:
  1143. module = module.lookup_submodule(submodule)
  1144. return module
  1145. def add_include_file(self, filename, verbatim_include=None, late=False):
  1146. """
  1147. Add `filename` as include file. Add `verbatim_include` as
  1148. verbatim text in the C file.
  1149. Both `filename` and `verbatim_include` can be `None` or empty.
  1150. """
  1151. inc = Code.IncludeCode(filename, verbatim_include, late=late)
  1152. self.process_include(inc)
  1153. def process_include(self, inc):
  1154. """
  1155. Add `inc`, which is an instance of `IncludeCode`, to this
  1156. `ModuleScope`. This either adds a new element to the
  1157. `c_includes` dict or it updates an existing entry.
  1158. In detail: the values of the dict `self.c_includes` are
  1159. instances of `IncludeCode` containing the code to be put in the
  1160. generated C file. The keys of the dict are needed to ensure
  1161. uniqueness in two ways: if an include file is specified in
  1162. multiple "cdef extern" blocks, only one `#include` statement is
  1163. generated. Second, the same include might occur multiple times
  1164. if we find it through multiple "cimport" paths. So we use the
  1165. generated code (of the form `#include "header.h"`) as dict key.
  1166. If verbatim code does not belong to any include file (i.e. it
  1167. was put in a `cdef extern from *` block), then we use a unique
  1168. dict key: namely, the `sortkey()`.
  1169. One `IncludeCode` object can contain multiple pieces of C code:
  1170. one optional "main piece" for the include file and several other
  1171. pieces for the verbatim code. The `IncludeCode.dict_update`
  1172. method merges the pieces of two different `IncludeCode` objects
  1173. if needed.
  1174. """
  1175. key = inc.mainpiece()
  1176. if key is None:
  1177. key = inc.sortkey()
  1178. inc.dict_update(self.c_includes, key)
  1179. inc = self.c_includes[key]
  1180. def add_imported_module(self, scope):
  1181. if scope not in self.cimported_modules:
  1182. for inc in scope.c_includes.values():
  1183. self.process_include(inc)
  1184. self.cimported_modules.append(scope)
  1185. for m in scope.cimported_modules:
  1186. self.add_imported_module(m)
  1187. def add_imported_entry(self, name, entry, pos):
  1188. if entry.is_pyglobal:
  1189. # Allow cimports to follow imports.
  1190. entry.is_variable = True
  1191. if entry not in self.entries:
  1192. self.entries[name] = entry
  1193. else:
  1194. warning(pos, "'%s' redeclared " % name, 0)
  1195. def declare_module(self, name, scope, pos):
  1196. # Declare a cimported module. This is represented as a
  1197. # Python module-level variable entry with a module
  1198. # scope attached to it. Reports an error and returns
  1199. # None if previously declared as something else.
  1200. entry = self.lookup_here(name)
  1201. if entry:
  1202. if entry.is_pyglobal and entry.as_module is scope:
  1203. return entry # Already declared as the same module
  1204. if not (entry.is_pyglobal and not entry.as_module):
  1205. # SAGE -- I put this here so Pyrex
  1206. # cimport's work across directories.
  1207. # Currently it tries to multiply define
  1208. # every module appearing in an import list.
  1209. # It shouldn't be an error for a module
  1210. # name to appear again, and indeed the generated
  1211. # code compiles fine.
  1212. return entry
  1213. else:
  1214. entry = self.declare_var(name, py_object_type, pos)
  1215. entry.is_variable = 0
  1216. entry.as_module = scope
  1217. self.add_imported_module(scope)
  1218. return entry
  1219. def declare_var(self, name, type, pos,
  1220. cname = None, visibility = 'private',
  1221. api = 0, in_pxd = 0, is_cdef = 0):
  1222. # Add an entry for a global variable. If it is a Python
  1223. # object type, and not declared with cdef, it will live
  1224. # in the module dictionary, otherwise it will be a C
  1225. # global variable.
  1226. if not visibility in ('private', 'public', 'extern'):
  1227. error(pos, "Module-level variable cannot be declared %s" % visibility)
  1228. if not is_cdef:
  1229. if type is unspecified_type:
  1230. type = py_object_type
  1231. if not (type.is_pyobject and not type.is_extension_type):
  1232. raise InternalError(
  1233. "Non-cdef global variable is not a generic Python object")
  1234. if not cname:
  1235. defining = not in_pxd
  1236. if visibility == 'extern' or (visibility == 'public' and defining):
  1237. cname = name
  1238. else:
  1239. cname = self.mangle(Naming.var_prefix, name)
  1240. entry = self.lookup_here(name)
  1241. if entry and entry.defined_in_pxd:
  1242. #if visibility != 'private' and visibility != entry.visibility:
  1243. # warning(pos, "Variable '%s' previously declared as '%s'" % (name, entry.visibility), 1)
  1244. if not entry.type.same_as(type):
  1245. if visibility == 'extern' and entry.visibility == 'extern':
  1246. warning(pos, "Variable '%s' type does not match previous declaration" % name, 1)
  1247. entry.type = type
  1248. #else:
  1249. # error(pos, "Variable '%s' type does not match previous declaration" % name)
  1250. if entry.visibility != "private":
  1251. mangled_cname = self.mangle(Naming.var_prefix, name)
  1252. if entry.cname == mangled_cname:
  1253. cname = name
  1254. entry.cname = name
  1255. if not entry.is_implemented:
  1256. entry.is_implemented = True
  1257. return entry
  1258. entry = Scope.declare_var(self, name, type, pos,
  1259. cname=cname, visibility=visibility,
  1260. api=api, in_pxd=in_pxd, is_cdef=is_cdef)
  1261. if is_cdef:
  1262. entry.is_cglobal = 1
  1263. if entry.type.declaration_value:
  1264. entry.init = entry.type.declaration_value
  1265. self.var_entries.append(entry)
  1266. else:
  1267. entry.is_pyglobal = 1
  1268. if Options.cimport_from_pyx:
  1269. entry.used = 1
  1270. return entry
  1271. def declare_cfunction(self, name, type, pos,
  1272. cname=None, visibility='private', api=0, in_pxd=0,
  1273. defining=0, modifiers=(), utility_code=None, overridable=False):
  1274. if not defining and 'inline' in modifiers:
  1275. # TODO(github/1736): Make this an error.
  1276. warning(pos, "Declarations should not be declared inline.", 1)
  1277. # Add an entry for a C function.
  1278. if not cname:
  1279. if visibility == 'extern' or (visibility == 'public' and defining):
  1280. cname = name
  1281. else:
  1282. cname = self.mangle(Naming.func_prefix, name)
  1283. if visibility == 'extern' and type.optional_arg_count:
  1284. error(pos, "Extern functions cannot have default arguments values.")
  1285. entry = self.lookup_here(name)
  1286. if entry and entry.defined_in_pxd:
  1287. if entry.visibility != "private":
  1288. mangled_cname = self.mangle(Naming.var_prefix, name)
  1289. if entry.cname == mangled_cname:
  1290. cname = name
  1291. entry.cname = cname
  1292. entry.func_cname = cname
  1293. entry = Scope.declare_cfunction(
  1294. self, name, type, pos,
  1295. cname=cname, visibility=visibility, api=api, in_pxd=in_pxd,
  1296. defining=defining, modifiers=modifiers, utility_code=utility_code,
  1297. overridable=overridable)
  1298. return entry
  1299. def declare_global(self, name, pos):
  1300. entry = self.lookup_here(name)
  1301. if not entry:
  1302. self.declare_var(name, py_object_type, pos)
  1303. def use_utility_code(self, new_code):
  1304. if new_code is not None:
  1305. self.utility_code_list.append(new_code)
  1306. def use_entry_utility_code(self, entry):
  1307. if entry is None:
  1308. return
  1309. if entry.utility_code:
  1310. self.utility_code_list.append(entry.utility_code)
  1311. if entry.utility_code_definition:
  1312. self.utility_code_list.append(entry.utility_code_definition)
  1313. def declare_c_class(self, name, pos, defining = 0, implementing = 0,
  1314. module_name = None, base_type = None, objstruct_cname = None,
  1315. typeobj_cname = None, typeptr_cname = None, visibility = 'private', typedef_flag = 0, api = 0,
  1316. buffer_defaults = None, shadow = 0):
  1317. # If this is a non-extern typedef class, expose the typedef, but use
  1318. # the non-typedef struct internally to avoid needing forward
  1319. # declarations for anonymous structs.
  1320. if typedef_flag and visibility != 'extern':
  1321. if not (visibility == 'public' or api):
  1322. warning(pos, "ctypedef only valid for 'extern' , 'public', and 'api'", 2)
  1323. objtypedef_cname = objstruct_cname
  1324. typedef_flag = 0
  1325. else:
  1326. objtypedef_cname = None
  1327. #
  1328. # Look for previous declaration as a type
  1329. #
  1330. entry = self.lookup_here(name)
  1331. if entry and not shadow:
  1332. type = entry.type
  1333. if not (entry.is_type and type.is_extension_type):
  1334. entry = None # Will cause redeclaration and produce an error
  1335. else:
  1336. scope = type.scope
  1337. if typedef_flag and (not scope or scope.defined):
  1338. self.check_previous_typedef_flag(entry, typedef_flag, pos)
  1339. if (scope and scope.defined) or (base_type and type.base_type):
  1340. if base_type and base_type is not type.base_type:
  1341. error(pos, "Base type does not match previous declaration")
  1342. if base_type and not type.base_type:
  1343. type.base_type = base_type
  1344. #
  1345. # Make a new entry if needed
  1346. #
  1347. if not entry or shadow:
  1348. type = PyrexTypes.PyExtensionType(name, typedef_flag, base_type, visibility == 'extern')
  1349. type.pos = pos
  1350. type.buffer_defaults = buffer_defaults
  1351. if objtypedef_cname is not None:
  1352. type.objtypedef_cname = objtypedef_cname
  1353. if visibility == 'extern':
  1354. type.module_name = module_name
  1355. else:
  1356. type.module_name = self.qualified_name
  1357. if typeptr_cname:
  1358. type.typeptr_cname = typeptr_cname
  1359. else:
  1360. type.typeptr_cname = self.mangle(Naming.typeptr_prefix, name)
  1361. entry = self.declare_type(name, type, pos, visibility = visibility,
  1362. defining = 0, shadow = shadow)
  1363. entry.is_cclass = True
  1364. if objstruct_cname:
  1365. type.objstruct_cname = objstruct_cname
  1366. elif not entry.in_cinclude:
  1367. type.objstruct_cname = self.mangle(Naming.objstruct_prefix, name)
  1368. else:
  1369. error(entry.pos,
  1370. "Object name required for 'public' or 'extern' C class")
  1371. self.attach_var_entry_to_c_class(entry)
  1372. self.c_class_entries.append(entry)
  1373. #
  1374. # Check for re-definition and create scope if needed
  1375. #
  1376. if not type.scope:
  1377. if defining or implementing:
  1378. scope = CClassScope(name = name, outer_scope = self,
  1379. visibility = visibility)
  1380. scope.directives = self.directives.copy()
  1381. if base_type and base_type.scope:
  1382. scope.declare_inherited_c_attributes(base_type.scope)
  1383. type.set_scope(scope)
  1384. self.type_entries.append(entry)
  1385. else:
  1386. if defining and type.scope.defined:
  1387. error(pos, "C class '%s' already defined" % name)
  1388. elif implementing and type.scope.implemented:
  1389. error(pos, "C class '%s' already implemented" % name)
  1390. #
  1391. # Fill in options, checking for compatibility with any previous declaration
  1392. #
  1393. if defining:
  1394. entry.defined_in_pxd = 1
  1395. if implementing: # So that filenames in runtime exceptions refer to
  1396. entry.pos = pos # the .pyx file and not the .pxd file
  1397. if visibility != 'private' and entry.visibility != visibility:
  1398. error(pos, "Class '%s' previously declared as '%s'"
  1399. % (name, entry.visibility))
  1400. if api:
  1401. entry.api = 1
  1402. if objstruct_cname:
  1403. if type.objstruct_cname and type.objstruct_cname != objstruct_cname:
  1404. error(pos, "Object struct name differs from previous declaration")
  1405. type.objstruct_cname = objstruct_cname
  1406. if typeobj_cname:
  1407. if type.typeobj_cname and type.typeobj_cname != typeobj_cname:
  1408. error(pos, "Type object name differs from previous declaration")
  1409. type.typeobj_cname = typeobj_cname
  1410. if self.directives.get('final'):
  1411. entry.type.is_final_type = True
  1412. # cdef classes are always exported, but we need to set it to
  1413. # distinguish between unused Cython utility code extension classes
  1414. entry.used = True
  1415. #
  1416. # Return new or existing entry
  1417. #
  1418. return entry
  1419. def allocate_vtable_names(self, entry):
  1420. # If extension type has a vtable, allocate vtable struct and
  1421. # slot names for it.
  1422. type = entry.type
  1423. if type.base_type and type.base_type.vtabslot_cname:
  1424. #print "...allocating vtabslot_cname because base type has one" ###
  1425. type.vtabslot_cname = "%s.%s" % (
  1426. Naming.obj_base_cname, type.base_type.vtabslot_cname)
  1427. elif type.scope and type.scope.cfunc_entries:
  1428. # one special case here: when inheriting from builtin
  1429. # types, the methods may also be built-in, in which
  1430. # case they won't need a vtable
  1431. entry_count = len(type.scope.cfunc_entries)
  1432. base_type = type.base_type
  1433. while base_type:
  1434. # FIXME: this will break if we ever get non-inherited C methods
  1435. if not base_type.scope or entry_count > len(base_type.scope.cfunc_entries):
  1436. break
  1437. if base_type.is_builtin_type:
  1438. # builtin base type defines all methods => no vtable needed
  1439. return
  1440. base_type = base_type.base_type
  1441. #print "...allocating vtabslot_cname because there are C methods" ###
  1442. type.vtabslot_cname = Naming.vtabslot_cname
  1443. if type.vtabslot_cname:
  1444. #print "...allocating other vtable related cnames" ###
  1445. type.vtabstruct_cname = self.mangle(Naming.vtabstruct_prefix, entry.name)
  1446. type.vtabptr_cname = self.mangle(Naming.vtabptr_prefix, entry.name)
  1447. def check_c_classes_pxd(self):
  1448. # Performs post-analysis checking and finishing up of extension types
  1449. # being implemented in this module. This is called only for the .pxd.
  1450. #
  1451. # Checks all extension types declared in this scope to
  1452. # make sure that:
  1453. #
  1454. # * The extension type is fully declared
  1455. #
  1456. # Also allocates a name for the vtable if needed.
  1457. #
  1458. for entry in self.c_class_entries:
  1459. # Check defined
  1460. if not entry.type.scope:
  1461. error(entry.pos, "C class '%s' is declared but not defined" % entry.name)
  1462. def check_c_class(self, entry):
  1463. type = entry.type
  1464. name = entry.name
  1465. visibility = entry.visibility
  1466. # Check defined
  1467. if not type.scope:
  1468. error(entry.pos, "C class '%s' is declared but not defined" % name)
  1469. # Generate typeobj_cname
  1470. if visibility != 'extern' and not type.typeobj_cname:
  1471. type.typeobj_cname = self.mangle(Naming.typeobj_prefix, name)
  1472. ## Generate typeptr_cname
  1473. #type.typeptr_cname = self.mangle(Naming.typeptr_prefix, name)
  1474. # Check C methods defined
  1475. if type.scope:
  1476. for method_entry in type.scope.cfunc_entries:
  1477. if not method_entry.is_inherited and not method_entry.func_cname:
  1478. error(method_entry.pos, "C method '%s' is declared but not defined" %
  1479. method_entry.name)
  1480. # Allocate vtable name if necessary
  1481. if type.vtabslot_cname:
  1482. #print "ModuleScope.check_c_classes: allocating vtable cname for", self ###
  1483. type.vtable_cname = self.mangle(Naming.vtable_prefix, entry.name)
  1484. def check_c_classes(self):
  1485. # Performs post-analysis checking and finishing up of extension types
  1486. # being implemented in this module. This is called only for the main
  1487. # .pyx file scope, not for cimported .pxd scopes.
  1488. #
  1489. # Checks all extension types declared in this scope to
  1490. # make sure that:
  1491. #
  1492. # * The extension type is implemented
  1493. # * All required object and type names have been specified or generated
  1494. # * All non-inherited C methods are implemented
  1495. #
  1496. # Also allocates a name for the vtable if needed.
  1497. #
  1498. debug_check_c_classes = 0
  1499. if debug_check_c_classes:
  1500. print("Scope.check_c_classes: checking scope " + self.qualified_name)
  1501. for entry in self.c_class_entries:
  1502. if debug_check_c_classes:
  1503. print("...entry %s %s" % (entry.name, entry))
  1504. print("......type = ", entry.type)
  1505. print("......visibility = ", entry.visibility)
  1506. self.check_c_class(entry)
  1507. def check_c_functions(self):
  1508. # Performs post-analysis checking making sure all
  1509. # defined c functions are actually implemented.
  1510. for name, entry in self.entries.items():
  1511. if entry.is_cfunction:
  1512. if (entry.defined_in_pxd
  1513. and entry.scope is self
  1514. and entry.visibility != 'extern'
  1515. and not entry.in_cinclude
  1516. and not entry.is_implemented):
  1517. error(entry.pos, "Non-extern C function '%s' declared but not defined" % name)
  1518. def attach_var_entry_to_c_class(self, entry):
  1519. # The name of an extension class has to serve as both a type
  1520. # name and a variable name holding the type object. It is
  1521. # represented in the symbol table by a type entry with a
  1522. # variable entry attached to it. For the variable entry,
  1523. # we use a read-only C global variable whose name is an
  1524. # expression that refers to the type object.
  1525. from . import Builtin
  1526. var_entry = Entry(name = entry.name,
  1527. type = Builtin.type_type,
  1528. pos = entry.pos,
  1529. cname = entry.type.typeptr_cname)
  1530. var_entry.is_variable = 1
  1531. var_entry.is_cglobal = 1
  1532. var_entry.is_readonly = 1
  1533. var_entry.scope = entry.scope
  1534. entry.as_variable = var_entry
  1535. def is_cpp(self):
  1536. return self.cpp
  1537. def infer_types(self):
  1538. from .TypeInference import PyObjectTypeInferer
  1539. PyObjectTypeInferer().infer_types(self)
  1540. class LocalScope(Scope):
  1541. # Does the function have a 'with gil:' block?
  1542. has_with_gil_block = False
  1543. # Transient attribute, used for symbol table variable declarations
  1544. _in_with_gil_block = False
  1545. def __init__(self, name, outer_scope, parent_scope = None):
  1546. if parent_scope is None:
  1547. parent_scope = outer_scope
  1548. Scope.__init__(self, name, outer_scope, parent_scope)
  1549. def mangle(self, prefix, name):
  1550. return prefix + name
  1551. def declare_arg(self, name, type, pos):
  1552. # Add an entry for an argument of a function.
  1553. cname = self.mangle(Naming.var_prefix, name)
  1554. entry = self.declare(name, cname, type, pos, 'private')
  1555. entry.is_variable = 1
  1556. if type.is_pyobject:
  1557. entry.init = "0"
  1558. entry.is_arg = 1
  1559. #entry.borrowed = 1 # Not using borrowed arg refs for now
  1560. self.arg_entries.append(entry)
  1561. return entry
  1562. def declare_var(self, name, type, pos,
  1563. cname = None, visibility = 'private',
  1564. api = 0, in_pxd = 0, is_cdef = 0):
  1565. # Add an entry for a local variable.
  1566. if visibility in ('public', 'readonly'):
  1567. error(pos, "Local variable cannot be declared %s" % visibility)
  1568. entry = Scope.declare_var(self, name, type, pos,
  1569. cname=cname, visibility=visibility,
  1570. api=api, in_pxd=in_pxd, is_cdef=is_cdef)
  1571. if entry.type.declaration_value:
  1572. entry.init = entry.type.declaration_value
  1573. entry.is_local = 1
  1574. entry.in_with_gil_block = self._in_with_gil_block
  1575. self.var_entries.append(entry)
  1576. return entry
  1577. def declare_global(self, name, pos):
  1578. # Pull entry from global scope into local scope.
  1579. if self.lookup_here(name):
  1580. warning(pos, "'%s' redeclared ", 0)
  1581. else:
  1582. entry = self.global_scope().lookup_target(name)
  1583. self.entries[name] = entry
  1584. def declare_nonlocal(self, name, pos):
  1585. # Pull entry from outer scope into local scope
  1586. orig_entry = self.lookup_here(name)
  1587. if orig_entry and orig_entry.scope is self and not orig_entry.from_closure:
  1588. error(pos, "'%s' redeclared as nonlocal" % name)
  1589. orig_entry.already_declared_here()
  1590. else:
  1591. entry = self.lookup(name)
  1592. if entry is None or not entry.from_closure:
  1593. error(pos, "no binding for nonlocal '%s' found" % name)
  1594. def lookup(self, name):
  1595. # Look up name in this scope or an enclosing one.
  1596. # Return None if not found.
  1597. entry = Scope.lookup(self, name)
  1598. if entry is not None:
  1599. entry_scope = entry.scope
  1600. while entry_scope.is_genexpr_scope:
  1601. entry_scope = entry_scope.outer_scope
  1602. if entry_scope is not self and entry_scope.is_closure_scope:
  1603. if hasattr(entry.scope, "scope_class"):
  1604. raise InternalError("lookup() after scope class created.")
  1605. # The actual c fragment for the different scopes differs
  1606. # on the outside and inside, so we make a new entry
  1607. entry.in_closure = True
  1608. inner_entry = InnerEntry(entry, self)
  1609. inner_entry.is_variable = True
  1610. self.entries[name] = inner_entry
  1611. return inner_entry
  1612. return entry
  1613. def mangle_closure_cnames(self, outer_scope_cname):
  1614. for scope in self.iter_local_scopes():
  1615. for entry in scope.entries.values():
  1616. if entry.from_closure:
  1617. cname = entry.outer_entry.cname
  1618. if self.is_passthrough:
  1619. entry.cname = cname
  1620. else:
  1621. if cname.startswith(Naming.cur_scope_cname):
  1622. cname = cname[len(Naming.cur_scope_cname)+2:]
  1623. entry.cname = "%s->%s" % (outer_scope_cname, cname)
  1624. elif entry.in_closure:
  1625. entry.original_cname = entry.cname
  1626. entry.cname = "%s->%s" % (Naming.cur_scope_cname, entry.cname)
  1627. class GeneratorExpressionScope(Scope):
  1628. """Scope for generator expressions and comprehensions. As opposed
  1629. to generators, these can be easily inlined in some cases, so all
  1630. we really need is a scope that holds the loop variable(s).
  1631. """
  1632. is_genexpr_scope = True
  1633. def __init__(self, outer_scope):
  1634. parent_scope = outer_scope
  1635. # TODO: also ignore class scopes?
  1636. while parent_scope.is_genexpr_scope:
  1637. parent_scope = parent_scope.parent_scope
  1638. name = parent_scope.global_scope().next_id(Naming.genexpr_id_ref)
  1639. Scope.__init__(self, name, outer_scope, parent_scope)
  1640. self.directives = outer_scope.directives
  1641. self.genexp_prefix = "%s%d%s" % (Naming.pyrex_prefix, len(name), name)
  1642. # Class/ExtType scopes are filled at class creation time, i.e. from the
  1643. # module init function or surrounding function.
  1644. while outer_scope.is_genexpr_scope or outer_scope.is_c_class_scope or outer_scope.is_py_class_scope:
  1645. outer_scope = outer_scope.outer_scope
  1646. self.var_entries = outer_scope.var_entries # keep declarations outside
  1647. outer_scope.subscopes.add(self)
  1648. def mangle(self, prefix, name):
  1649. return '%s%s' % (self.genexp_prefix, self.parent_scope.mangle(prefix, name))
  1650. def declare_var(self, name, type, pos,
  1651. cname = None, visibility = 'private',
  1652. api = 0, in_pxd = 0, is_cdef = True):
  1653. if type is unspecified_type:
  1654. # if the outer scope defines a type for this variable, inherit it
  1655. outer_entry = self.outer_scope.lookup(name)
  1656. if outer_entry and outer_entry.is_variable:
  1657. type = outer_entry.type # may still be 'unspecified_type' !
  1658. # the parent scope needs to generate code for the variable, but
  1659. # this scope must hold its name exclusively
  1660. cname = '%s%s' % (self.genexp_prefix, self.parent_scope.mangle(Naming.var_prefix, name or self.next_id()))
  1661. entry = self.declare(name, cname, type, pos, visibility)
  1662. entry.is_variable = True
  1663. if self.parent_scope.is_module_scope:
  1664. entry.is_cglobal = True
  1665. else:
  1666. entry.is_local = True
  1667. entry.in_subscope = True
  1668. self.var_entries.append(entry)
  1669. self.entries[name] = entry
  1670. return entry
  1671. def declare_pyfunction(self, name, pos, allow_redefine=False):
  1672. return self.outer_scope.declare_pyfunction(
  1673. name, pos, allow_redefine)
  1674. def declare_lambda_function(self, func_cname, pos):
  1675. return self.outer_scope.declare_lambda_function(func_cname, pos)
  1676. def add_lambda_def(self, def_node):
  1677. return self.outer_scope.add_lambda_def(def_node)
  1678. class ClosureScope(LocalScope):
  1679. is_closure_scope = True
  1680. def __init__(self, name, scope_name, outer_scope, parent_scope=None):
  1681. LocalScope.__init__(self, name, outer_scope, parent_scope)
  1682. self.closure_cname = "%s%s" % (Naming.closure_scope_prefix, scope_name)
  1683. # def mangle_closure_cnames(self, scope_var):
  1684. # for entry in self.entries.values() + self.temp_entries:
  1685. # entry.in_closure = 1
  1686. # LocalScope.mangle_closure_cnames(self, scope_var)
  1687. # def mangle(self, prefix, name):
  1688. # return "%s->%s" % (self.cur_scope_cname, name)
  1689. # return "%s->%s" % (self.closure_cname, name)
  1690. def declare_pyfunction(self, name, pos, allow_redefine=False):
  1691. return LocalScope.declare_pyfunction(self, name, pos, allow_redefine, visibility='private')
  1692. class StructOrUnionScope(Scope):
  1693. # Namespace of a C struct or union.
  1694. def __init__(self, name="?"):
  1695. Scope.__init__(self, name, None, None)
  1696. def declare_var(self, name, type, pos,
  1697. cname = None, visibility = 'private',
  1698. api = 0, in_pxd = 0, is_cdef = 0,
  1699. allow_pyobject=False, allow_memoryview=False):
  1700. # Add an entry for an attribute.
  1701. if not cname:
  1702. cname = name
  1703. if visibility == 'private':
  1704. cname = c_safe_identifier(cname)
  1705. if type.is_cfunction:
  1706. type = PyrexTypes.CPtrType(type)
  1707. entry = self.declare(name, cname, type, pos, visibility)
  1708. entry.is_variable = 1
  1709. self.var_entries.append(entry)
  1710. if type.is_pyobject and not allow_pyobject:
  1711. error(pos, "C struct/union member cannot be a Python object")
  1712. elif type.is_memoryviewslice and not allow_memoryview:
  1713. # Memory views wrap their buffer owner as a Python object.
  1714. error(pos, "C struct/union member cannot be a memory view")
  1715. if visibility != 'private':
  1716. error(pos, "C struct/union member cannot be declared %s" % visibility)
  1717. return entry
  1718. def declare_cfunction(self, name, type, pos,
  1719. cname=None, visibility='private', api=0, in_pxd=0,
  1720. defining=0, modifiers=(), overridable=False): # currently no utility code ...
  1721. if overridable:
  1722. error(pos, "C struct/union member cannot be declared 'cpdef'")
  1723. return self.declare_var(name, type, pos,
  1724. cname=cname, visibility=visibility)
  1725. class ClassScope(Scope):
  1726. # Abstract base class for namespace of
  1727. # Python class or extension type.
  1728. #
  1729. # class_name string Python name of the class
  1730. # scope_prefix string Additional prefix for names
  1731. # declared in the class
  1732. # doc string or None Doc string
  1733. def __init__(self, name, outer_scope):
  1734. Scope.__init__(self, name, outer_scope, outer_scope)
  1735. self.class_name = name
  1736. self.doc = None
  1737. def lookup(self, name):
  1738. entry = Scope.lookup(self, name)
  1739. if entry:
  1740. return entry
  1741. if name == "classmethod":
  1742. # We don't want to use the builtin classmethod here 'cause it won't do the
  1743. # right thing in this scope (as the class members aren't still functions).
  1744. # Don't want to add a cfunction to this scope 'cause that would mess with
  1745. # the type definition, so we just return the right entry.
  1746. entry = Entry(
  1747. "classmethod",
  1748. "__Pyx_Method_ClassMethod",
  1749. PyrexTypes.CFuncType(
  1750. py_object_type,
  1751. [PyrexTypes.CFuncTypeArg("", py_object_type, None)], 0, 0))
  1752. entry.utility_code_definition = Code.UtilityCode.load_cached("ClassMethod", "CythonFunction.c")
  1753. self.use_entry_utility_code(entry)
  1754. entry.is_cfunction = 1
  1755. return entry
  1756. class PyClassScope(ClassScope):
  1757. # Namespace of a Python class.
  1758. #
  1759. # class_obj_cname string C variable holding class object
  1760. is_py_class_scope = 1
  1761. def mangle_class_private_name(self, name):
  1762. return self.mangle_special_name(name)
  1763. def mangle_special_name(self, name):
  1764. if name and name.startswith('__') and not name.endswith('__'):
  1765. name = EncodedString('_%s%s' % (self.class_name.lstrip('_'), name))
  1766. return name
  1767. def lookup_here(self, name):
  1768. name = self.mangle_special_name(name)
  1769. return ClassScope.lookup_here(self, name)
  1770. def declare_var(self, name, type, pos,
  1771. cname = None, visibility = 'private',
  1772. api = 0, in_pxd = 0, is_cdef = 0):
  1773. name = self.mangle_special_name(name)
  1774. if type is unspecified_type:
  1775. type = py_object_type
  1776. # Add an entry for a class attribute.
  1777. entry = Scope.declare_var(self, name, type, pos,
  1778. cname=cname, visibility=visibility,
  1779. api=api, in_pxd=in_pxd, is_cdef=is_cdef)
  1780. entry.is_pyglobal = 1
  1781. entry.is_pyclass_attr = 1
  1782. return entry
  1783. def declare_nonlocal(self, name, pos):
  1784. # Pull entry from outer scope into local scope
  1785. orig_entry = self.lookup_here(name)
  1786. if orig_entry and orig_entry.scope is self and not orig_entry.from_closure:
  1787. error(pos, "'%s' redeclared as nonlocal" % name)
  1788. orig_entry.already_declared_here()
  1789. else:
  1790. entry = self.lookup(name)
  1791. if entry is None:
  1792. error(pos, "no binding for nonlocal '%s' found" % name)
  1793. else:
  1794. # FIXME: this works, but it's unclear if it's the
  1795. # right thing to do
  1796. self.entries[name] = entry
  1797. def declare_global(self, name, pos):
  1798. # Pull entry from global scope into local scope.
  1799. if self.lookup_here(name):
  1800. warning(pos, "'%s' redeclared ", 0)
  1801. else:
  1802. entry = self.global_scope().lookup_target(name)
  1803. self.entries[name] = entry
  1804. def add_default_value(self, type):
  1805. return self.outer_scope.add_default_value(type)
  1806. class CClassScope(ClassScope):
  1807. # Namespace of an extension type.
  1808. #
  1809. # parent_type CClassType
  1810. # #typeobj_cname string or None
  1811. # #objstruct_cname string
  1812. # method_table_cname string
  1813. # getset_table_cname string
  1814. # has_pyobject_attrs boolean Any PyObject attributes?
  1815. # has_memoryview_attrs boolean Any memory view attributes?
  1816. # has_cpp_class_attrs boolean Any (non-pointer) C++ attributes?
  1817. # has_cyclic_pyobject_attrs boolean Any PyObject attributes that may need GC?
  1818. # property_entries [Entry]
  1819. # defined boolean Defined in .pxd file
  1820. # implemented boolean Defined in .pyx file
  1821. # inherited_var_entries [Entry] Adapted var entries from base class
  1822. is_c_class_scope = 1
  1823. is_closure_class_scope = False
  1824. has_pyobject_attrs = False
  1825. has_memoryview_attrs = False
  1826. has_cpp_class_attrs = False
  1827. has_cyclic_pyobject_attrs = False
  1828. defined = False
  1829. implemented = False
  1830. def __init__(self, name, outer_scope, visibility):
  1831. ClassScope.__init__(self, name, outer_scope)
  1832. if visibility != 'extern':
  1833. self.method_table_cname = outer_scope.mangle(Naming.methtab_prefix, name)
  1834. self.getset_table_cname = outer_scope.mangle(Naming.gstab_prefix, name)
  1835. self.property_entries = []
  1836. self.inherited_var_entries = []
  1837. def needs_gc(self):
  1838. # If the type or any of its base types have Python-valued
  1839. # C attributes, then it needs to participate in GC.
  1840. if self.has_cyclic_pyobject_attrs and not self.directives.get('no_gc', False):
  1841. return True
  1842. base_type = self.parent_type.base_type
  1843. if base_type and base_type.scope is not None:
  1844. return base_type.scope.needs_gc()
  1845. elif self.parent_type.is_builtin_type:
  1846. return not self.parent_type.is_gc_simple
  1847. return False
  1848. def needs_tp_clear(self):
  1849. """
  1850. Do we need to generate an implementation for the tp_clear slot? Can
  1851. be disabled to keep references for the __dealloc__ cleanup function.
  1852. """
  1853. return self.needs_gc() and not self.directives.get('no_gc_clear', False)
  1854. def get_refcounted_entries(self, include_weakref=False,
  1855. include_gc_simple=True):
  1856. py_attrs = []
  1857. py_buffers = []
  1858. memoryview_slices = []
  1859. for entry in self.var_entries:
  1860. if entry.type.is_pyobject:
  1861. if include_weakref or (self.is_closure_class_scope or entry.name != "__weakref__"):
  1862. if include_gc_simple or not entry.type.is_gc_simple:
  1863. py_attrs.append(entry)
  1864. elif entry.type == PyrexTypes.c_py_buffer_type:
  1865. py_buffers.append(entry)
  1866. elif entry.type.is_memoryviewslice:
  1867. memoryview_slices.append(entry)
  1868. have_entries = py_attrs or py_buffers or memoryview_slices
  1869. return have_entries, (py_attrs, py_buffers, memoryview_slices)
  1870. def declare_var(self, name, type, pos,
  1871. cname = None, visibility = 'private',
  1872. api = 0, in_pxd = 0, is_cdef = 0):
  1873. if is_cdef:
  1874. # Add an entry for an attribute.
  1875. if self.defined:
  1876. error(pos,
  1877. "C attributes cannot be added in implementation part of"
  1878. " extension type defined in a pxd")
  1879. if not self.is_closure_class_scope and get_special_method_signature(name):
  1880. error(pos,
  1881. "The name '%s' is reserved for a special method."
  1882. % name)
  1883. if not cname:
  1884. cname = name
  1885. if visibility == 'private':
  1886. cname = c_safe_identifier(cname)
  1887. if type.is_cpp_class and visibility != 'extern':
  1888. type.check_nullary_constructor(pos)
  1889. self.use_utility_code(Code.UtilityCode("#include <new>"))
  1890. entry = self.declare(name, cname, type, pos, visibility)
  1891. entry.is_variable = 1
  1892. self.var_entries.append(entry)
  1893. if type.is_memoryviewslice:
  1894. self.has_memoryview_attrs = True
  1895. elif type.is_cpp_class:
  1896. self.has_cpp_class_attrs = True
  1897. elif type.is_pyobject and (self.is_closure_class_scope or name != '__weakref__'):
  1898. self.has_pyobject_attrs = True
  1899. if (not type.is_builtin_type
  1900. or not type.scope or type.scope.needs_gc()):
  1901. self.has_cyclic_pyobject_attrs = True
  1902. if visibility not in ('private', 'public', 'readonly'):
  1903. error(pos,
  1904. "Attribute of extension type cannot be declared %s" % visibility)
  1905. if visibility in ('public', 'readonly'):
  1906. # If the field is an external typedef, we cannot be sure about the type,
  1907. # so do conversion ourself rather than rely on the CPython mechanism (through
  1908. # a property; made in AnalyseDeclarationsTransform).
  1909. entry.needs_property = True
  1910. if not self.is_closure_class_scope and name == "__weakref__":
  1911. error(pos, "Special attribute __weakref__ cannot be exposed to Python")
  1912. if not (type.is_pyobject or type.can_coerce_to_pyobject(self)):
  1913. # we're not testing for coercion *from* Python here - that would fail later
  1914. error(pos, "C attribute of type '%s' cannot be accessed from Python" % type)
  1915. else:
  1916. entry.needs_property = False
  1917. return entry
  1918. else:
  1919. if type is unspecified_type:
  1920. type = py_object_type
  1921. # Add an entry for a class attribute.
  1922. entry = Scope.declare_var(self, name, type, pos,
  1923. cname=cname, visibility=visibility,
  1924. api=api, in_pxd=in_pxd, is_cdef=is_cdef)
  1925. entry.is_member = 1
  1926. entry.is_pyglobal = 1 # xxx: is_pyglobal changes behaviour in so many places that
  1927. # I keep it in for now. is_member should be enough
  1928. # later on
  1929. self.namespace_cname = "(PyObject *)%s" % self.parent_type.typeptr_cname
  1930. return entry
  1931. def declare_pyfunction(self, name, pos, allow_redefine=False):
  1932. # Add an entry for a method.
  1933. if name in richcmp_special_methods:
  1934. if self.lookup_here('__richcmp__'):
  1935. error(pos, "Cannot define both % and __richcmp__" % name)
  1936. elif name == '__richcmp__':
  1937. for n in richcmp_special_methods:
  1938. if self.lookup_here(n):
  1939. error(pos, "Cannot define both % and __richcmp__" % n)
  1940. if name == "__new__":
  1941. error(pos, "__new__ method of extension type will change semantics "
  1942. "in a future version of Pyrex and Cython. Use __cinit__ instead.")
  1943. entry = self.declare_var(name, py_object_type, pos,
  1944. visibility='extern')
  1945. special_sig = get_special_method_signature(name)
  1946. if special_sig:
  1947. # Special methods get put in the method table with a particular
  1948. # signature declared in advance.
  1949. entry.signature = special_sig
  1950. entry.is_special = 1
  1951. else:
  1952. entry.signature = pymethod_signature
  1953. entry.is_special = 0
  1954. self.pyfunc_entries.append(entry)
  1955. return entry
  1956. def lookup_here(self, name):
  1957. if not self.is_closure_class_scope and name == "__new__":
  1958. name = EncodedString("__cinit__")
  1959. entry = ClassScope.lookup_here(self, name)
  1960. if entry and entry.is_builtin_cmethod:
  1961. if not self.parent_type.is_builtin_type:
  1962. # For subtypes of builtin types, we can only return
  1963. # optimised C methods if the type if final.
  1964. # Otherwise, subtypes may choose to override the
  1965. # method, but the optimisation would prevent the
  1966. # subtype method from being called.
  1967. if not self.parent_type.is_final_type:
  1968. return None
  1969. return entry
  1970. def declare_cfunction(self, name, type, pos,
  1971. cname=None, visibility='private', api=0, in_pxd=0,
  1972. defining=0, modifiers=(), utility_code=None, overridable=False):
  1973. if get_special_method_signature(name) and not self.parent_type.is_builtin_type:
  1974. error(pos, "Special methods must be declared with 'def', not 'cdef'")
  1975. args = type.args
  1976. if not type.is_static_method:
  1977. if not args:
  1978. error(pos, "C method has no self argument")
  1979. elif not self.parent_type.assignable_from(args[0].type):
  1980. error(pos, "Self argument (%s) of C method '%s' does not match parent type (%s)" %
  1981. (args[0].type, name, self.parent_type))
  1982. entry = self.lookup_here(name)
  1983. if cname is None:
  1984. cname = c_safe_identifier(name)
  1985. if entry:
  1986. if not entry.is_cfunction:
  1987. warning(pos, "'%s' redeclared " % name, 0)
  1988. else:
  1989. if defining and entry.func_cname:
  1990. error(pos, "'%s' already defined" % name)
  1991. #print "CClassScope.declare_cfunction: checking signature" ###
  1992. if entry.is_final_cmethod and entry.is_inherited:
  1993. error(pos, "Overriding final methods is not allowed")
  1994. elif type.same_c_signature_as(entry.type, as_cmethod = 1) and type.nogil == entry.type.nogil:
  1995. # Fix with_gil vs nogil.
  1996. entry.type = entry.type.with_with_gil(type.with_gil)
  1997. elif type.compatible_signature_with(entry.type, as_cmethod = 1) and type.nogil == entry.type.nogil:
  1998. if (self.defined and not in_pxd
  1999. and not type.same_c_signature_as_resolved_type(entry.type, as_cmethod = 1, as_pxd_definition = 1)):
  2000. # TODO(robertwb): Make this an error.
  2001. warning(pos,
  2002. "Compatible but non-identical C method '%s' not redeclared "
  2003. "in definition part of extension type '%s'. This may cause incorrect vtables to be generated." % (name, self.class_name), 2)
  2004. warning(entry.pos, "Previous declaration is here", 2)
  2005. entry = self.add_cfunction(name, type, pos, cname, visibility='ignore', modifiers=modifiers)
  2006. else:
  2007. error(pos, "Signature not compatible with previous declaration")
  2008. error(entry.pos, "Previous declaration is here")
  2009. else:
  2010. if self.defined:
  2011. error(pos,
  2012. "C method '%s' not previously declared in definition part of"
  2013. " extension type '%s'" % (name, self.class_name))
  2014. entry = self.add_cfunction(name, type, pos, cname, visibility, modifiers)
  2015. if defining:
  2016. entry.func_cname = self.mangle(Naming.func_prefix, name)
  2017. entry.utility_code = utility_code
  2018. type.entry = entry
  2019. if u'inline' in modifiers:
  2020. entry.is_inline_cmethod = True
  2021. if (self.parent_type.is_final_type or entry.is_inline_cmethod or
  2022. self.directives.get('final')):
  2023. entry.is_final_cmethod = True
  2024. entry.final_func_cname = entry.func_cname
  2025. return entry
  2026. def add_cfunction(self, name, type, pos, cname, visibility, modifiers, inherited=False):
  2027. # Add a cfunction entry without giving it a func_cname.
  2028. prev_entry = self.lookup_here(name)
  2029. entry = ClassScope.add_cfunction(self, name, type, pos, cname,
  2030. visibility, modifiers, inherited=inherited)
  2031. entry.is_cmethod = 1
  2032. entry.prev_entry = prev_entry
  2033. return entry
  2034. def declare_builtin_cfunction(self, name, type, cname, utility_code = None):
  2035. # overridden methods of builtin types still have their Python
  2036. # equivalent that must be accessible to support bound methods
  2037. name = EncodedString(name)
  2038. entry = self.declare_cfunction(name, type, None, cname, visibility='extern',
  2039. utility_code=utility_code)
  2040. var_entry = Entry(name, name, py_object_type)
  2041. var_entry.is_variable = 1
  2042. var_entry.is_builtin = 1
  2043. var_entry.utility_code = utility_code
  2044. var_entry.scope = entry.scope
  2045. entry.as_variable = var_entry
  2046. return entry
  2047. def declare_property(self, name, doc, pos):
  2048. entry = self.lookup_here(name)
  2049. if entry is None:
  2050. entry = self.declare(name, name, py_object_type, pos, 'private')
  2051. entry.is_property = 1
  2052. entry.doc = doc
  2053. entry.scope = PropertyScope(name,
  2054. outer_scope = self.global_scope(), parent_scope = self)
  2055. entry.scope.parent_type = self.parent_type
  2056. self.property_entries.append(entry)
  2057. return entry
  2058. def declare_inherited_c_attributes(self, base_scope):
  2059. # Declare entries for all the C attributes of an
  2060. # inherited type, with cnames modified appropriately
  2061. # to work with this type.
  2062. def adapt(cname):
  2063. return "%s.%s" % (Naming.obj_base_cname, base_entry.cname)
  2064. entries = base_scope.inherited_var_entries + base_scope.var_entries
  2065. for base_entry in entries:
  2066. entry = self.declare(
  2067. base_entry.name, adapt(base_entry.cname),
  2068. base_entry.type, None, 'private')
  2069. entry.is_variable = 1
  2070. self.inherited_var_entries.append(entry)
  2071. # If the class defined in a pxd, specific entries have not been added.
  2072. # Ensure now that the parent (base) scope has specific entries
  2073. # Iterate over a copy as get_all_specialized_function_types() will mutate
  2074. for base_entry in base_scope.cfunc_entries[:]:
  2075. if base_entry.type.is_fused:
  2076. base_entry.type.get_all_specialized_function_types()
  2077. for base_entry in base_scope.cfunc_entries:
  2078. cname = base_entry.cname
  2079. var_entry = base_entry.as_variable
  2080. is_builtin = var_entry and var_entry.is_builtin
  2081. if not is_builtin:
  2082. cname = adapt(cname)
  2083. entry = self.add_cfunction(base_entry.name, base_entry.type,
  2084. base_entry.pos, cname,
  2085. base_entry.visibility, base_entry.func_modifiers, inherited=True)
  2086. entry.is_inherited = 1
  2087. if base_entry.is_final_cmethod:
  2088. entry.is_final_cmethod = True
  2089. entry.is_inline_cmethod = base_entry.is_inline_cmethod
  2090. if (self.parent_scope == base_scope.parent_scope or
  2091. entry.is_inline_cmethod):
  2092. entry.final_func_cname = base_entry.final_func_cname
  2093. if is_builtin:
  2094. entry.is_builtin_cmethod = True
  2095. entry.as_variable = var_entry
  2096. if base_entry.utility_code:
  2097. entry.utility_code = base_entry.utility_code
  2098. class CppClassScope(Scope):
  2099. # Namespace of a C++ class.
  2100. is_cpp_class_scope = 1
  2101. default_constructor = None
  2102. type = None
  2103. def __init__(self, name, outer_scope, templates=None):
  2104. Scope.__init__(self, name, outer_scope, None)
  2105. self.directives = outer_scope.directives
  2106. self.inherited_var_entries = []
  2107. if templates is not None:
  2108. for T in templates:
  2109. template_entry = self.declare(
  2110. T, T, PyrexTypes.TemplatePlaceholderType(T), None, 'extern')
  2111. template_entry.is_type = 1
  2112. def declare_var(self, name, type, pos,
  2113. cname = None, visibility = 'extern',
  2114. api = 0, in_pxd = 0, is_cdef = 0, defining = 0):
  2115. # Add an entry for an attribute.
  2116. if not cname:
  2117. cname = name
  2118. entry = self.lookup_here(name)
  2119. if defining and entry is not None:
  2120. if entry.type.same_as(type):
  2121. # Fix with_gil vs nogil.
  2122. entry.type = entry.type.with_with_gil(type.with_gil)
  2123. elif type.is_cfunction and type.compatible_signature_with(entry.type):
  2124. entry.type = type
  2125. else:
  2126. error(pos, "Function signature does not match previous declaration")
  2127. else:
  2128. entry = self.declare(name, cname, type, pos, visibility)
  2129. entry.is_variable = 1
  2130. if type.is_cfunction and self.type:
  2131. if not self.type.get_fused_types():
  2132. entry.func_cname = "%s::%s" % (self.type.empty_declaration_code(), cname)
  2133. if name != "this" and (defining or name != "<init>"):
  2134. self.var_entries.append(entry)
  2135. return entry
  2136. def declare_cfunction(self, name, type, pos,
  2137. cname=None, visibility='extern', api=0, in_pxd=0,
  2138. defining=0, modifiers=(), utility_code=None, overridable=False):
  2139. class_name = self.name.split('::')[-1]
  2140. if name in (class_name, '__init__') and cname is None:
  2141. cname = "%s__init__%s" % (Naming.func_prefix, class_name)
  2142. name = '<init>'
  2143. type.return_type = PyrexTypes.CVoidType()
  2144. # This is called by the actual constructor, but need to support
  2145. # arguments that cannot by called by value.
  2146. type.original_args = type.args
  2147. def maybe_ref(arg):
  2148. if arg.type.is_cpp_class and not arg.type.is_reference:
  2149. return PyrexTypes.CFuncTypeArg(
  2150. arg.name, PyrexTypes.c_ref_type(arg.type), arg.pos)
  2151. else:
  2152. return arg
  2153. type.args = [maybe_ref(arg) for arg in type.args]
  2154. elif name == '__dealloc__' and cname is None:
  2155. cname = "%s__dealloc__%s" % (Naming.func_prefix, class_name)
  2156. name = '<del>'
  2157. type.return_type = PyrexTypes.CVoidType()
  2158. if name in ('<init>', '<del>') and type.nogil:
  2159. for base in self.type.base_classes:
  2160. base_entry = base.scope.lookup(name)
  2161. if base_entry and not base_entry.type.nogil:
  2162. error(pos, "Constructor cannot be called without GIL unless all base constructors can also be called without GIL")
  2163. error(base_entry.pos, "Base constructor defined here.")
  2164. prev_entry = self.lookup_here(name)
  2165. entry = self.declare_var(name, type, pos,
  2166. defining=defining,
  2167. cname=cname, visibility=visibility)
  2168. if prev_entry and not defining:
  2169. entry.overloaded_alternatives = prev_entry.all_alternatives()
  2170. entry.utility_code = utility_code
  2171. type.entry = entry
  2172. return entry
  2173. def declare_inherited_cpp_attributes(self, base_class):
  2174. base_scope = base_class.scope
  2175. template_type = base_class
  2176. while getattr(template_type, 'template_type', None):
  2177. template_type = template_type.template_type
  2178. if getattr(template_type, 'templates', None):
  2179. base_templates = [T.name for T in template_type.templates]
  2180. else:
  2181. base_templates = ()
  2182. # Declare entries for all the C++ attributes of an
  2183. # inherited type, with cnames modified appropriately
  2184. # to work with this type.
  2185. for base_entry in \
  2186. base_scope.inherited_var_entries + base_scope.var_entries:
  2187. #constructor/destructor is not inherited
  2188. if base_entry.name in ("<init>", "<del>"):
  2189. continue
  2190. #print base_entry.name, self.entries
  2191. if base_entry.name in self.entries:
  2192. base_entry.name # FIXME: is there anything to do in this case?
  2193. entry = self.declare(base_entry.name, base_entry.cname,
  2194. base_entry.type, None, 'extern')
  2195. entry.is_variable = 1
  2196. entry.is_inherited = 1
  2197. self.inherited_var_entries.append(entry)
  2198. for base_entry in base_scope.cfunc_entries:
  2199. entry = self.declare_cfunction(base_entry.name, base_entry.type,
  2200. base_entry.pos, base_entry.cname,
  2201. base_entry.visibility, api=0,
  2202. modifiers=base_entry.func_modifiers,
  2203. utility_code=base_entry.utility_code)
  2204. entry.is_inherited = 1
  2205. for base_entry in base_scope.type_entries:
  2206. if base_entry.name not in base_templates:
  2207. entry = self.declare_type(base_entry.name, base_entry.type,
  2208. base_entry.pos, base_entry.cname,
  2209. base_entry.visibility)
  2210. entry.is_inherited = 1
  2211. def specialize(self, values, type_entry):
  2212. scope = CppClassScope(self.name, self.outer_scope)
  2213. scope.type = type_entry
  2214. for entry in self.entries.values():
  2215. if entry.is_type:
  2216. scope.declare_type(entry.name,
  2217. entry.type.specialize(values),
  2218. entry.pos,
  2219. entry.cname,
  2220. template=1)
  2221. elif entry.type.is_cfunction:
  2222. for e in entry.all_alternatives():
  2223. scope.declare_cfunction(e.name,
  2224. e.type.specialize(values),
  2225. e.pos,
  2226. e.cname,
  2227. utility_code=e.utility_code)
  2228. else:
  2229. scope.declare_var(entry.name,
  2230. entry.type.specialize(values),
  2231. entry.pos,
  2232. entry.cname,
  2233. entry.visibility)
  2234. return scope
  2235. class PropertyScope(Scope):
  2236. # Scope holding the __get__, __set__ and __del__ methods for
  2237. # a property of an extension type.
  2238. #
  2239. # parent_type PyExtensionType The type to which the property belongs
  2240. is_property_scope = 1
  2241. def declare_pyfunction(self, name, pos, allow_redefine=False):
  2242. # Add an entry for a method.
  2243. signature = get_property_accessor_signature(name)
  2244. if signature:
  2245. entry = self.declare(name, name, py_object_type, pos, 'private')
  2246. entry.is_special = 1
  2247. entry.signature = signature
  2248. return entry
  2249. else:
  2250. error(pos, "Only __get__, __set__ and __del__ methods allowed "
  2251. "in a property declaration")
  2252. return None
  2253. class CConstScope(Scope):
  2254. def __init__(self, const_base_type_scope):
  2255. Scope.__init__(
  2256. self,
  2257. 'const_' + const_base_type_scope.name,
  2258. const_base_type_scope.outer_scope,
  2259. const_base_type_scope.parent_scope)
  2260. self.const_base_type_scope = const_base_type_scope
  2261. def lookup_here(self, name):
  2262. entry = self.const_base_type_scope.lookup_here(name)
  2263. if entry is not None:
  2264. entry = copy.copy(entry)
  2265. entry.type = PyrexTypes.c_const_type(entry.type)
  2266. return entry
  2267. class TemplateScope(Scope):
  2268. def __init__(self, name, outer_scope):
  2269. Scope.__init__(self, name, outer_scope, None)
  2270. self.directives = outer_scope.directives