pdecsub.pas 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used implementation section }
  26. pd_interface, { directive can be used interface section }
  27. pd_object, { directive can be used object declaration }
  28. pd_procvar, { directive can be used procvar declaration }
  29. pd_notobject, { directive can not be used object declaration }
  30. pd_notobjintf, { directive can not be used interface declaration }
  31. pd_notprocvar, { directive can not be used procvar declaration }
  32. pd_dispinterface,{ directive can be used with dispinterface methods }
  33. pd_cppobject, { directive can be used with cppclass }
  34. pd_objcclass, { directive can be used with objcclass }
  35. pd_objcprot { directive can be used with objcprotocol }
  36. );
  37. tpdflags=set of tpdflag;
  38. function check_proc_directive(isprocvar:boolean):boolean;
  39. procedure insert_funcret_local(pd:tprocdef);
  40. function proc_add_definition(var currpd:tprocdef):boolean;
  41. function proc_get_importname(pd:tprocdef):string;
  42. procedure proc_set_mangledname(pd:tprocdef);
  43. procedure handle_calling_convention(pd:tabstractprocdef);
  44. procedure parse_parameter_dec(pd:tabstractprocdef);
  45. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  46. procedure parse_var_proc_directives(sym:tsym);
  47. procedure parse_object_proc_directives(pd:tabstractprocdef);
  48. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  49. function parse_proc_dec(isclassmethod:boolean; aclass:tobjectdef):tprocdef;
  50. { helper functions - they insert nested objects hierarcy to the symtablestack
  51. with object hierarchy
  52. }
  53. function push_child_hierarcy(obj:tobjectdef):integer;
  54. function pop_child_hierarchy(obj:tobjectdef):integer;
  55. function push_nested_hierarchy(obj:tobjectdef):integer;
  56. function pop_nested_hierarchy(obj:tobjectdef):integer;
  57. implementation
  58. uses
  59. SysUtils,
  60. { common }
  61. cutils,cclasses,
  62. { global }
  63. globtype,globals,verbose,constexp,
  64. systems,
  65. cpuinfo,
  66. { symtable }
  67. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  68. { pass 1 }
  69. fmodule,node,htypechk,
  70. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  71. objcutil,
  72. { parser }
  73. scanner,
  74. pbase,pexpr,ptype,pdecl
  75. ;
  76. const
  77. { Please leave this here, this module should NOT use
  78. these variables.
  79. Declaring it as string here results in an error when compiling (PFV) }
  80. current_procinfo = 'error';
  81. function push_child_hierarcy(obj:tobjectdef):integer;
  82. var
  83. _class,hp : tobjectdef;
  84. begin
  85. result:=0;
  86. { insert class hierarchy in the reverse order }
  87. hp:=nil;
  88. repeat
  89. _class:=obj;
  90. while _class.childof<>hp do
  91. _class:=_class.childof;
  92. hp:=_class;
  93. symtablestack.push(_class.symtable);
  94. inc(result);
  95. until hp=obj;
  96. end;
  97. function push_nested_hierarchy(obj:tobjectdef):integer;
  98. begin
  99. result:=0;
  100. if obj.owner.symtabletype=ObjectSymtable then
  101. inc(result,push_nested_hierarchy(tobjectdef(obj.owner.defowner)));
  102. inc(result,push_child_hierarcy(obj));
  103. end;
  104. function pop_child_hierarchy(obj:tobjectdef):integer;
  105. var
  106. _class : tobjectdef;
  107. begin
  108. result:=0;
  109. _class:=obj;
  110. while assigned(_class) do
  111. begin
  112. symtablestack.pop(_class.symtable);
  113. _class:=_class.childof;
  114. inc(result);
  115. end;
  116. end;
  117. function pop_nested_hierarchy(obj:tobjectdef):integer;
  118. begin
  119. result:=pop_child_hierarchy(obj);
  120. if obj.owner.symtabletype=ObjectSymtable then
  121. inc(result,pop_nested_hierarchy(tobjectdef(obj.owner.defowner)));
  122. end;
  123. procedure insert_funcret_para(pd:tabstractprocdef);
  124. var
  125. storepos : tfileposinfo;
  126. vs : tparavarsym;
  127. paranr : word;
  128. begin
  129. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  130. not is_void(pd.returndef) and
  131. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  132. begin
  133. storepos:=current_tokenpos;
  134. if pd.typ=procdef then
  135. current_tokenpos:=tprocdef(pd).fileinfo;
  136. {$if defined(i386)}
  137. { For left to right add it at the end to be delphi compatible.
  138. In the case of safecalls with safecal-exceptions support the
  139. funcret-para is (from the 'c'-point of view) a normal parameter
  140. which has to be added to the end of the parameter-list }
  141. if (pd.proccalloption in (pushleftright_pocalls)) or
  142. ((tf_safecall_exceptions in target_info.flags) and
  143. (pd.proccalloption=pocall_safecall)) then
  144. paranr:=paranr_result_leftright
  145. else
  146. {$elseif defined(x86) or defined(arm)}
  147. if (target_info.system in systems_all_windows) and
  148. (pd.proccalloption = pocall_safecall) then
  149. paranr:=paranr_result_leftright
  150. else
  151. {$endif}
  152. paranr:=paranr_result;
  153. { Generate result variable accessing function result }
  154. vs:=tparavarsym.create('$result',paranr,vs_var,pd.returndef,[vo_is_funcret,vo_is_hidden_para]);
  155. pd.parast.insert(vs);
  156. { Store the this symbol as funcretsym for procedures }
  157. if pd.typ=procdef then
  158. tprocdef(pd).funcretsym:=vs;
  159. current_tokenpos:=storepos;
  160. end;
  161. end;
  162. procedure insert_parentfp_para(pd:tabstractprocdef);
  163. var
  164. storepos : tfileposinfo;
  165. vs : tparavarsym;
  166. paranr : longint;
  167. begin
  168. if pd.parast.symtablelevel>normal_function_level then
  169. begin
  170. storepos:=current_tokenpos;
  171. if pd.typ=procdef then
  172. current_tokenpos:=tprocdef(pd).fileinfo;
  173. { if no support for nested procvars is activated, use the old
  174. calling convention to pass the parent frame pointer for backwards
  175. compatibility }
  176. if not(m_nested_procvars in current_settings.modeswitches) then
  177. paranr:=paranr_parentfp
  178. { nested procvars require Delphi-style parentfp passing, see
  179. po_delphi_nested_cc declaration for more info }
  180. {$ifdef i386}
  181. else if (pd.proccalloption in pushleftright_pocalls) then
  182. paranr:=paranr_parentfp_delphi_cc_leftright
  183. {$endif i386}
  184. else
  185. paranr:=paranr_parentfp_delphi_cc;
  186. { Generate result variable accessing function result, it
  187. can't be put in a register since it must be accessable
  188. from the framepointer }
  189. vs:=tparavarsym.create('$parentfp',paranr,vs_value
  190. ,voidpointertype,[vo_is_parentfp,vo_is_hidden_para]);
  191. vs.varregable:=vr_none;
  192. pd.parast.insert(vs);
  193. current_tokenpos:=storepos;
  194. end;
  195. end;
  196. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  197. var
  198. storepos : tfileposinfo;
  199. vs : tparavarsym;
  200. hdef : tdef;
  201. vsp : tvarspez;
  202. aliasvs : tabsolutevarsym;
  203. sl : tpropaccesslist;
  204. begin
  205. if (pd.typ=procdef) and
  206. is_objc_class_or_protocol(tprocdef(pd)._class) and
  207. (pd.parast.symtablelevel=normal_function_level) then
  208. begin
  209. { insert Objective-C self and selector parameters }
  210. vs:=tparavarsym.create('$_cmd',paranr_objc_cmd,vs_value,objc_seltype,[vo_is_msgsel,vo_is_hidden_para]);
  211. pd.parast.insert(vs);
  212. { make accessible to code }
  213. sl:=tpropaccesslist.create;
  214. sl.addsym(sl_load,vs);
  215. aliasvs:=tabsolutevarsym.create_ref('_CMD',objc_seltype,sl);
  216. include(aliasvs.varoptions,vo_is_msgsel);
  217. tlocalsymtable(tprocdef(pd).localst).insert(aliasvs);
  218. if (po_classmethod in pd.procoptions) then
  219. { compatible with what gcc does }
  220. hdef:=objc_idtype
  221. else
  222. hdef:=tprocdef(pd)._class;
  223. vs:=tparavarsym.create('$self',paranr_objc_self,vs_value,hdef,[vo_is_self,vo_is_hidden_para]);
  224. pd.parast.insert(vs);
  225. end
  226. else if (pd.typ=procvardef) and
  227. pd.is_methodpointer then
  228. begin
  229. { Generate self variable }
  230. vs:=tparavarsym.create('$self',paranr_self,vs_value,voidpointertype,[vo_is_self,vo_is_hidden_para]);
  231. pd.parast.insert(vs);
  232. end
  233. else
  234. begin
  235. if (pd.typ=procdef) and
  236. assigned(tprocdef(pd)._class) and
  237. (pd.parast.symtablelevel=normal_function_level) then
  238. begin
  239. { static class methods have no hidden self/vmt pointer }
  240. if pd.no_self_node then
  241. exit;
  242. storepos:=current_tokenpos;
  243. current_tokenpos:=tprocdef(pd).fileinfo;
  244. { Generate VMT variable for constructor/destructor }
  245. if (pd.proctypeoption in [potype_constructor,potype_destructor]) and not(is_cppclass(tprocdef(pd)._class)) then
  246. begin
  247. { can't use classrefdef as type because inheriting
  248. will then always file because of a type mismatch }
  249. vs:=tparavarsym.create('$vmt',paranr_vmt,vs_value,voidpointertype,[vo_is_vmt,vo_is_hidden_para]);
  250. pd.parast.insert(vs);
  251. end;
  252. { Generate self variable, for classes we need
  253. to use the generic voidpointer to be compatible with
  254. methodpointers }
  255. vsp:=vs_value;
  256. if (po_staticmethod in pd.procoptions) or
  257. (po_classmethod in pd.procoptions) then
  258. hdef:=tclassrefdef.create(tprocdef(pd)._class)
  259. else
  260. begin
  261. if is_object(tprocdef(pd)._class) then
  262. vsp:=vs_var;
  263. hdef:=tprocdef(pd)._class;
  264. end;
  265. vs:=tparavarsym.create('$self',paranr_self,vsp,hdef,[vo_is_self,vo_is_hidden_para]);
  266. pd.parast.insert(vs);
  267. current_tokenpos:=storepos;
  268. end;
  269. end;
  270. end;
  271. procedure insert_funcret_local(pd:tprocdef);
  272. var
  273. storepos : tfileposinfo;
  274. vs : tlocalvarsym;
  275. aliasvs : tabsolutevarsym;
  276. sl : tpropaccesslist;
  277. hs : string;
  278. begin
  279. { The result from constructors and destructors can't be accessed directly }
  280. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  281. not is_void(pd.returndef) then
  282. begin
  283. storepos:=current_tokenpos;
  284. current_tokenpos:=pd.fileinfo;
  285. { We need to insert a varsym for the result in the localst
  286. when it is returning in a register }
  287. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  288. begin
  289. vs:=tlocalvarsym.create('$result',vs_value,pd.returndef,[vo_is_funcret]);
  290. pd.localst.insert(vs);
  291. pd.funcretsym:=vs;
  292. end;
  293. { insert the name of the procedure as alias for the function result,
  294. we can't use realname because that will not work for compilerprocs
  295. as the name is lowercase and unreachable from the code }
  296. if assigned(pd.resultname) then
  297. hs:=pd.resultname^
  298. else
  299. hs:=pd.procsym.name;
  300. sl:=tpropaccesslist.create;
  301. sl.addsym(sl_load,pd.funcretsym);
  302. aliasvs:=tabsolutevarsym.create_ref(hs,pd.returndef,sl);
  303. include(aliasvs.varoptions,vo_is_funcret);
  304. tlocalsymtable(pd.localst).insert(aliasvs);
  305. { insert result also if support is on }
  306. if (m_result in current_settings.modeswitches) then
  307. begin
  308. sl:=tpropaccesslist.create;
  309. sl.addsym(sl_load,pd.funcretsym);
  310. aliasvs:=tabsolutevarsym.create_ref('RESULT',pd.returndef,sl);
  311. include(aliasvs.varoptions,vo_is_funcret);
  312. include(aliasvs.varoptions,vo_is_result);
  313. tlocalsymtable(pd.localst).insert(aliasvs);
  314. end;
  315. current_tokenpos:=storepos;
  316. end;
  317. end;
  318. procedure insert_hidden_para(p:TObject;arg:pointer);
  319. var
  320. hvs : tparavarsym;
  321. pd : tabstractprocdef absolute arg;
  322. begin
  323. if (tsym(p).typ<>paravarsym) then
  324. exit;
  325. with tparavarsym(p) do
  326. begin
  327. { We need a local copy for a value parameter when only the
  328. address is pushed. Open arrays and Array of Const are
  329. an exception because they are allocated at runtime and the
  330. address that is pushed is patched }
  331. if (varspez=vs_value) and
  332. paramanager.push_addr_param(varspez,vardef,pd.proccalloption) and
  333. not(is_open_array(vardef) or
  334. is_array_of_const(vardef)) then
  335. include(varoptions,vo_has_local_copy);
  336. { needs high parameter ? }
  337. if paramanager.push_high_param(varspez,vardef,pd.proccalloption) then
  338. begin
  339. hvs:=tparavarsym.create('$high'+name,paranr+1,vs_const,sinttype,[vo_is_high_para,vo_is_hidden_para]);
  340. hvs.symoptions:=[];
  341. owner.insert(hvs);
  342. end
  343. else
  344. begin
  345. { Give a warning that cdecl routines does not include high()
  346. support }
  347. if (pd.proccalloption in cdecl_pocalls) and
  348. paramanager.push_high_param(varspez,vardef,pocall_default) then
  349. begin
  350. if is_open_string(vardef) then
  351. MessagePos(fileinfo,parser_w_cdecl_no_openstring);
  352. if not(po_external in pd.procoptions) and
  353. (pd.typ<>procvardef) and
  354. not is_objc_class_or_protocol(tprocdef(pd)._class) then
  355. if is_array_of_const(vardef) then
  356. MessagePos(fileinfo,parser_e_varargs_need_cdecl_and_external)
  357. else
  358. MessagePos(fileinfo,parser_w_cdecl_has_no_high);
  359. end;
  360. if (vardef.typ=formaldef) and (Tformaldef(vardef).typed) then
  361. begin
  362. hvs:=tparavarsym.create('$typinfo'+name,paranr+1,vs_const,voidpointertype,
  363. [vo_is_typinfo_para,vo_is_hidden_para]);
  364. owner.insert(hvs);
  365. end;
  366. end;
  367. end;
  368. end;
  369. procedure check_c_para(pd:Tabstractprocdef);
  370. var
  371. i,
  372. lastparaidx : longint;
  373. sym : TSym;
  374. begin
  375. lastparaidx:=pd.parast.SymList.Count-1;
  376. for i:=0 to pd.parast.SymList.Count-1 do
  377. begin
  378. sym:=tsym(pd.parast.SymList[i]);
  379. if (sym.typ=paravarsym) and
  380. (tparavarsym(sym).vardef.typ=arraydef) then
  381. begin
  382. if not is_variant_array(tparavarsym(sym).vardef) and
  383. not is_array_of_const(tparavarsym(sym).vardef) and
  384. (tparavarsym(sym).varspez<>vs_var) then
  385. MessagePos(tparavarsym(sym).fileinfo,parser_h_c_arrays_are_references);
  386. if is_array_of_const(tparavarsym(sym).vardef) and
  387. (i<lastparaidx) and
  388. (tsym(pd.parast.SymList[i+1]).typ=paravarsym) and
  389. not(vo_is_high_para in tparavarsym(pd.parast.SymList[i+1]).varoptions) then
  390. MessagePos(tparavarsym(sym).fileinfo,parser_e_C_array_of_const_must_be_last);
  391. end;
  392. end;
  393. end;
  394. procedure check_msg_para(p:TObject;arg:pointer);
  395. begin
  396. if (tsym(p).typ<>paravarsym) then
  397. exit;
  398. with tparavarsym(p) do
  399. begin
  400. { Count parameters }
  401. if (paranr>=10) then
  402. inc(plongint(arg)^);
  403. { First parameter must be var }
  404. if (paranr=10) and
  405. (varspez<>vs_var) then
  406. MessagePos(fileinfo,parser_e_ill_msg_param);
  407. end;
  408. end;
  409. procedure set_addr_param_regable(p:TObject;arg:pointer);
  410. begin
  411. if (tsym(p).typ<>paravarsym) then
  412. exit;
  413. with tparavarsym(p) do
  414. begin
  415. if not is_managed_type(vardef) and
  416. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  417. varregable:=vr_intreg;
  418. end;
  419. end;
  420. procedure parse_parameter_dec(pd:tabstractprocdef);
  421. {
  422. handle_procvar needs the same changes
  423. }
  424. type
  425. tppv = (pv_none,pv_proc,pv_func);
  426. var
  427. sc : TFPObjectList;
  428. hdef : tdef;
  429. arrayelementdef : tdef;
  430. vs : tparavarsym;
  431. i : longint;
  432. srsym : tsym;
  433. pv : tprocvardef;
  434. varspez : Tvarspez;
  435. defaultvalue : tconstsym;
  436. defaultrequired : boolean;
  437. old_block_type : tblock_type;
  438. currparast : tparasymtable;
  439. parseprocvar : tppv;
  440. locationstr : string;
  441. paranr : integer;
  442. dummytype : ttypesym;
  443. explicit_paraloc,
  444. need_array,
  445. is_univ: boolean;
  446. begin
  447. old_block_type:=block_type;
  448. explicit_paraloc:=false;
  449. consume(_LKLAMMER);
  450. { Delphi/Kylix supports nonsense like }
  451. { procedure p(); }
  452. if try_to_consume(_RKLAMMER) and
  453. not(m_tp7 in current_settings.modeswitches) then
  454. exit;
  455. { parsing a proc or procvar ? }
  456. currparast:=tparasymtable(pd.parast);
  457. { reset }
  458. sc:=TFPObjectList.create(false);
  459. defaultrequired:=false;
  460. paranr:=0;
  461. inc(testcurobject);
  462. block_type:=bt_var;
  463. is_univ:=false;
  464. repeat
  465. parseprocvar:=pv_none;
  466. if try_to_consume(_VAR) then
  467. varspez:=vs_var
  468. else
  469. if try_to_consume(_CONST) then
  470. varspez:=vs_const
  471. else
  472. if (m_out in current_settings.modeswitches) and
  473. try_to_consume(_OUT) then
  474. varspez:=vs_out
  475. else
  476. if try_to_consume(_CONSTREF) then
  477. varspez:=vs_constref
  478. else
  479. if (m_mac in current_settings.modeswitches) and
  480. try_to_consume(_POINTPOINTPOINT) then
  481. begin
  482. include(pd.procoptions,po_varargs);
  483. break;
  484. end
  485. else
  486. if (m_nested_procvars in current_settings.modeswitches) and
  487. try_to_consume(_PROCEDURE) then
  488. begin
  489. parseprocvar:=pv_proc;
  490. varspez:=vs_const;
  491. end
  492. else
  493. if (m_nested_procvars in current_settings.modeswitches) and
  494. try_to_consume(_FUNCTION) then
  495. begin
  496. parseprocvar:=pv_func;
  497. varspez:=vs_const;
  498. end
  499. else
  500. varspez:=vs_value;
  501. defaultvalue:=nil;
  502. hdef:=nil;
  503. { read identifiers and insert with error type }
  504. sc.clear;
  505. repeat
  506. inc(paranr);
  507. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  508. currparast.insert(vs);
  509. if assigned(vs.owner) then
  510. sc.add(vs)
  511. else
  512. vs.free;
  513. consume(_ID);
  514. until not try_to_consume(_COMMA);
  515. locationstr:='';
  516. { macpas anonymous procvar }
  517. if parseprocvar<>pv_none then
  518. begin
  519. { inline procvar definitions are always nested procvars }
  520. pv:=tprocvardef.create(normal_function_level+1);
  521. if token=_LKLAMMER then
  522. parse_parameter_dec(pv);
  523. if parseprocvar=pv_func then
  524. begin
  525. block_type:=bt_var_type;
  526. consume(_COLON);
  527. single_type(pv.returndef,false,false);
  528. block_type:=bt_var;
  529. end;
  530. hdef:=pv;
  531. { possible proc directives }
  532. if check_proc_directive(true) then
  533. begin
  534. dummytype:=ttypesym.create('unnamed',hdef);
  535. parse_var_proc_directives(tsym(dummytype));
  536. dummytype.typedef:=nil;
  537. hdef.typesym:=nil;
  538. dummytype.free;
  539. end;
  540. { Add implicit hidden parameters and function result }
  541. handle_calling_convention(pv);
  542. end
  543. else
  544. { read type declaration, force reading for value paras }
  545. if (token=_COLON) or (varspez=vs_value) then
  546. begin
  547. consume(_COLON);
  548. { check for an open array }
  549. need_array:=false;
  550. { bitpacked open array are not yet supported }
  551. if (token=_PACKED) and
  552. not(cs_bitpacking in current_settings.localswitches) then
  553. begin
  554. consume(_PACKED);
  555. need_array:=true;
  556. end;
  557. if (token=_ARRAY) or
  558. need_array then
  559. begin
  560. consume(_ARRAY);
  561. consume(_OF);
  562. { define range and type of range }
  563. hdef:=tarraydef.create(0,-1,s32inttype);
  564. { array of const ? }
  565. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  566. begin
  567. consume(_CONST);
  568. srsym:=search_system_type('TVARREC');
  569. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  570. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  571. end
  572. else
  573. begin
  574. { define field type }
  575. single_type(arrayelementdef,false,false);
  576. tarraydef(hdef).elementdef:=arrayelementdef;
  577. end;
  578. end
  579. else
  580. begin
  581. if (m_mac in current_settings.modeswitches) then
  582. is_univ:=try_to_consume(_UNIV);
  583. if try_to_consume(_TYPE) then
  584. hdef:=ctypedformaltype
  585. else
  586. begin
  587. block_type:=bt_var_type;
  588. single_type(hdef,false,false);
  589. block_type:=bt_var;
  590. end;
  591. { open string ? }
  592. if is_shortstring(hdef) then
  593. begin
  594. case varspez of
  595. vs_var,vs_out,vs_constref:
  596. begin
  597. { not 100% Delphi-compatible: type xstr=string[255] cannot
  598. become an openstring there, while here it can }
  599. if (cs_openstring in current_settings.moduleswitches) and
  600. (tstringdef(hdef).len=255) then
  601. hdef:=openshortstringtype
  602. end;
  603. vs_value:
  604. begin
  605. { value "openstring" parameters don't make sense (the
  606. original string can never be modified, so there's no
  607. use in passing its original length), so change these
  608. into regular shortstring parameters (seems to be what
  609. Delphi also does) }
  610. if is_open_string(hdef) then
  611. hdef:=cshortstringtype;
  612. end;
  613. end;
  614. end;
  615. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  616. begin
  617. if (idtoken=_LOCATION) then
  618. begin
  619. consume(_LOCATION);
  620. locationstr:=cstringpattern;
  621. consume(_CSTRING);
  622. end
  623. else
  624. begin
  625. if explicit_paraloc then
  626. Message(parser_e_paraloc_all_paras);
  627. locationstr:='';
  628. end;
  629. end
  630. else
  631. locationstr:='';
  632. { default parameter }
  633. if (m_default_para in current_settings.modeswitches) then
  634. begin
  635. if try_to_consume(_EQUAL) then
  636. begin
  637. vs:=tparavarsym(sc[0]);
  638. if sc.count>1 then
  639. Message(parser_e_default_value_only_one_para);
  640. { prefix 'def' to the parameter name }
  641. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  642. if assigned(defaultvalue) then
  643. begin
  644. include(defaultvalue.symoptions,sp_internal);
  645. pd.parast.insert(defaultvalue);
  646. end;
  647. defaultrequired:=true;
  648. end
  649. else
  650. begin
  651. if defaultrequired then
  652. Message1(parser_e_default_value_expected_for_para,vs.name);
  653. end;
  654. end;
  655. end;
  656. end
  657. else
  658. hdef:=cformaltype;
  659. { File types are only allowed for var and out parameters }
  660. if (hdef.typ=filedef) and
  661. not(varspez in [vs_out,vs_var]) then
  662. CGMessage(cg_e_file_must_call_by_reference);
  663. { Dispinterfaces are restricted to using only automatable types }
  664. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd)._class) and
  665. not is_automatable(hdef) then
  666. Message1(type_e_not_automatable,hdef.typename);
  667. { univ cannot be used with types whose size is not known at compile
  668. time }
  669. if is_univ and
  670. not is_valid_univ_para_type(hdef) then
  671. Message1(parser_e_invalid_univ_para,hdef.typename);
  672. for i:=0 to sc.count-1 do
  673. begin
  674. vs:=tparavarsym(sc[i]);
  675. vs.univpara:=is_univ;
  676. { update varsym }
  677. vs.vardef:=hdef;
  678. vs.defaultconstsym:=defaultvalue;
  679. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  680. begin
  681. if locationstr<>'' then
  682. begin
  683. if sc.count>1 then
  684. Message(parser_e_paraloc_only_one_para);
  685. if (paranr>1) and not(explicit_paraloc) then
  686. Message(parser_e_paraloc_all_paras);
  687. explicit_paraloc:=true;
  688. include(vs.varoptions,vo_has_explicit_paraloc);
  689. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  690. message(parser_e_illegal_explicit_paraloc);
  691. end
  692. else
  693. if explicit_paraloc then
  694. Message(parser_e_paraloc_all_paras);
  695. end;
  696. end;
  697. until not try_to_consume(_SEMICOLON);
  698. if explicit_paraloc then
  699. begin
  700. pd.has_paraloc_info:=callerside;
  701. include(pd.procoptions,po_explicitparaloc);
  702. end;
  703. { remove parasymtable from stack }
  704. sc.free;
  705. { reset object options }
  706. dec(testcurobject);
  707. block_type:=old_block_type;
  708. consume(_RKLAMMER);
  709. end;
  710. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  711. var
  712. hs : string;
  713. orgsp,sp : TIDString;
  714. srsym : tsym;
  715. srsymtable : TSymtable;
  716. checkstack : psymtablestackitem;
  717. storepos,
  718. procstartfilepos : tfileposinfo;
  719. searchagain : boolean;
  720. st,
  721. genericst: TSymtable;
  722. aprocsym : tprocsym;
  723. popclass : integer;
  724. ImplIntf : TImplementedInterface;
  725. old_parse_generic : boolean;
  726. old_current_objectdef,
  727. old_current_genericdef,
  728. old_current_specializedef : tobjectdef;
  729. begin
  730. { Save the position where this procedure really starts }
  731. procstartfilepos:=current_tokenpos;
  732. old_parse_generic:=parse_generic;
  733. result:=false;
  734. pd:=nil;
  735. aprocsym:=nil;
  736. if potype=potype_operator then
  737. begin
  738. sp:=overloaded_names[optoken];
  739. orgsp:=sp;
  740. end
  741. else
  742. begin
  743. sp:=pattern;
  744. orgsp:=orgpattern;
  745. consume(_ID);
  746. end;
  747. { examine interface map: function/procedure iname.functionname=locfuncname }
  748. if assigned(aclass) and
  749. assigned(aclass.ImplementedInterfaces) and
  750. (aclass.ImplementedInterfaces.count>0) and
  751. try_to_consume(_POINT) then
  752. begin
  753. storepos:=current_tokenpos;
  754. current_tokenpos:=procstartfilepos;
  755. { get interface syms}
  756. searchsym(sp,srsym,srsymtable);
  757. if not assigned(srsym) then
  758. begin
  759. identifier_not_found(orgsp);
  760. srsym:=generrorsym;
  761. end;
  762. current_tokenpos:=storepos;
  763. { qualifier is interface? }
  764. ImplIntf:=nil;
  765. if (srsym.typ=typesym) and
  766. (ttypesym(srsym).typedef.typ=objectdef) then
  767. ImplIntf:=aclass.find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  768. if ImplIntf=nil then
  769. Message(parser_e_interface_id_expected);
  770. consume(_ID);
  771. { Create unique name <interface>.<method> }
  772. hs:=sp+'.'+pattern;
  773. consume(_EQUAL);
  774. if assigned(ImplIntf) and
  775. (token=_ID) then
  776. ImplIntf.AddMapping(hs,pattern);
  777. consume(_ID);
  778. result:=true;
  779. exit;
  780. end;
  781. { method ? }
  782. if not assigned(aclass) and
  783. (potype<>potype_operator) and
  784. (symtablestack.top.symtablelevel=main_program_level) and
  785. try_to_consume(_POINT) then
  786. begin
  787. repeat
  788. searchagain:=false;
  789. if not assigned(aclass) then
  790. begin
  791. { search for object name }
  792. storepos:=current_tokenpos;
  793. current_tokenpos:=procstartfilepos;
  794. searchsym(sp,srsym,srsymtable);
  795. if not assigned(srsym) then
  796. begin
  797. identifier_not_found(orgsp);
  798. srsym:=generrorsym;
  799. end;
  800. current_tokenpos:=storepos;
  801. end;
  802. { consume proc name }
  803. sp:=pattern;
  804. orgsp:=orgpattern;
  805. procstartfilepos:=current_tokenpos;
  806. consume(_ID);
  807. { qualifier is class name ? }
  808. if (srsym.typ=typesym) and
  809. (ttypesym(srsym).typedef.typ=objectdef) then
  810. begin
  811. aclass:=tobjectdef(ttypesym(srsym).typedef);
  812. if (token<>_POINT) and (potype in [potype_class_constructor,potype_class_destructor]) then
  813. sp := lower(sp);
  814. srsym:=tsym(aclass.symtable.Find(sp));
  815. if assigned(srsym) then
  816. begin
  817. if srsym.typ=procsym then
  818. aprocsym:=tprocsym(srsym)
  819. else
  820. if (srsym.typ=typesym) and
  821. (ttypesym(srsym).typedef.typ=objectdef) then
  822. begin
  823. searchagain:=true;
  824. consume(_POINT);
  825. end
  826. else
  827. begin
  828. { we use a different error message for tp7 so it looks more compatible }
  829. if (m_fpc in current_settings.modeswitches) then
  830. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  831. else
  832. Message(parser_e_methode_id_expected);
  833. { rename the name to an unique name to avoid an
  834. error when inserting the symbol in the symtable }
  835. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  836. end;
  837. end
  838. else
  839. begin
  840. Message(parser_e_methode_id_expected);
  841. { recover by making it a normal procedure instead of method }
  842. aclass:=nil;
  843. end;
  844. end
  845. else
  846. Message(parser_e_class_id_expected);
  847. until not searchagain;
  848. end
  849. else
  850. begin
  851. { check for constructor/destructor which is not allowed here }
  852. if (not parse_only) and
  853. (potype in [potype_constructor,potype_destructor,
  854. potype_class_constructor,potype_class_destructor]) then
  855. Message(parser_e_constructors_always_objects);
  856. repeat
  857. searchagain:=false;
  858. current_tokenpos:=procstartfilepos;
  859. srsymtable:=symtablestack.top;
  860. srsym:=tsym(srsymtable.Find(sp));
  861. { Also look in the globalsymtable if we didn't found
  862. the symbol in the localsymtable }
  863. if not assigned(srsym) and
  864. not(parse_only) and
  865. (srsymtable=current_module.localsymtable) and
  866. assigned(current_module.globalsymtable) then
  867. srsym:=tsym(current_module.globalsymtable.Find(sp));
  868. { Check if overloaded is a procsym }
  869. if assigned(srsym) then
  870. begin
  871. if srsym.typ=procsym then
  872. aprocsym:=tprocsym(srsym)
  873. else
  874. begin
  875. { when the other symbol is a unit symbol then hide the unit
  876. symbol, this is not supported in tp7 }
  877. if not(m_tp7 in current_settings.modeswitches) and
  878. (srsym.typ=unitsym) then
  879. begin
  880. HideSym(srsym);
  881. searchagain:=true;
  882. end
  883. else
  884. begin
  885. { we use a different error message for tp7 so it looks more compatible }
  886. if (m_fpc in current_settings.modeswitches) then
  887. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  888. else
  889. Message1(sym_e_duplicate_id,srsym.realname);
  890. { rename the name to an unique name to avoid an
  891. error when inserting the symbol in the symtable }
  892. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  893. end;
  894. end;
  895. end;
  896. until not searchagain;
  897. end;
  898. { test again if assigned, it can be reset to recover }
  899. if not assigned(aprocsym) then
  900. begin
  901. { create a new procsym and set the real filepos }
  902. current_tokenpos:=procstartfilepos;
  903. { for operator we have only one procsym for each overloaded
  904. operation }
  905. if (potype=potype_operator) then
  906. begin
  907. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  908. if aprocsym=nil then
  909. aprocsym:=tprocsym.create('$'+sp);
  910. end
  911. else
  912. if (potype in [potype_class_constructor,potype_class_destructor]) then
  913. aprocsym:=tprocsym.create('$'+lower(sp))
  914. else
  915. aprocsym:=tprocsym.create(orgsp);
  916. symtablestack.top.insert(aprocsym);
  917. end;
  918. { to get the correct symtablelevel we must ignore ObjectSymtables }
  919. st:=nil;
  920. checkstack:=symtablestack.stack;
  921. while assigned(checkstack) do
  922. begin
  923. st:=checkstack^.symtable;
  924. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  925. break;
  926. checkstack:=checkstack^.next;
  927. end;
  928. pd:=tprocdef.create(st.symtablelevel+1);
  929. pd._class:=aclass;
  930. pd.procsym:=aprocsym;
  931. pd.proctypeoption:=potype;
  932. { methods inherit df_generic or df_specialization from the objectdef }
  933. if assigned(pd._class) and
  934. (pd.parast.symtablelevel=normal_function_level) then
  935. begin
  936. if (df_generic in pd._class.defoptions) then
  937. begin
  938. include(pd.defoptions,df_generic);
  939. parse_generic:=true;
  940. end;
  941. if (df_specialization in pd._class.defoptions) then
  942. begin
  943. include(pd.defoptions,df_specialization);
  944. { Find corresponding genericdef, we need it later to
  945. replay the tokens to generate the body }
  946. if not assigned(pd._class.genericdef) then
  947. internalerror(200512113);
  948. genericst:=pd._class.genericdef.GetSymtable(gs_record);
  949. if not assigned(genericst) then
  950. internalerror(200512114);
  951. { We are parsing the same objectdef, the def index numbers
  952. are the same }
  953. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  954. if not assigned(pd.genericdef) or
  955. (pd.genericdef.typ<>procdef) then
  956. internalerror(200512115);
  957. end;
  958. end;
  959. { methods need to be exported }
  960. if assigned(aclass) and
  961. (
  962. (symtablestack.top.symtabletype=ObjectSymtable) or
  963. (symtablestack.top.symtablelevel=main_program_level)
  964. ) then
  965. include(pd.procoptions,po_global);
  966. { symbol options that need to be kept per procdef }
  967. pd.fileinfo:=procstartfilepos;
  968. pd.visibility:=symtablestack.top.currentvisibility;
  969. if symtablestack.top.currentlyoptional then
  970. include(pd.procoptions,po_optional);
  971. { parse parameters }
  972. if token=_LKLAMMER then
  973. begin
  974. { Add ObjectSymtable to be able to find nested type definitions }
  975. popclass:=0;
  976. if assigned(pd._class) and
  977. (pd.parast.symtablelevel=normal_function_level) and
  978. (symtablestack.top.symtabletype<>ObjectSymtable) then
  979. begin
  980. popclass:=push_nested_hierarchy(pd._class);
  981. old_current_objectdef:=current_objectdef;
  982. old_current_genericdef:=current_genericdef;
  983. old_current_specializedef:=current_specializedef;
  984. current_objectdef:=pd._class;
  985. if assigned(current_objectdef) and (df_generic in current_objectdef.defoptions) then
  986. current_genericdef:=current_objectdef;
  987. if assigned(current_objectdef) and (df_specialization in current_objectdef.defoptions) then
  988. current_specializedef:=current_objectdef;
  989. end;
  990. { Add parameter symtable }
  991. if pd.parast.symtabletype<>staticsymtable then
  992. symtablestack.push(pd.parast);
  993. parse_parameter_dec(pd);
  994. if pd.parast.symtabletype<>staticsymtable then
  995. symtablestack.pop(pd.parast);
  996. if popclass>0 then
  997. begin
  998. current_objectdef:=old_current_objectdef;
  999. current_genericdef:=old_current_genericdef;
  1000. current_specializedef:=old_current_specializedef;
  1001. dec(popclass,pop_nested_hierarchy(pd._class));
  1002. if popclass<>0 then
  1003. internalerror(201011260); // 11 nov 2010 index 0
  1004. end;
  1005. end;
  1006. parse_generic:=old_parse_generic;
  1007. result:=true;
  1008. end;
  1009. function parse_proc_dec(isclassmethod:boolean; aclass:tobjectdef):tprocdef;
  1010. var
  1011. pd : tprocdef;
  1012. locationstr: string;
  1013. old_parse_generic: boolean;
  1014. popclass: integer;
  1015. old_current_objectdef,
  1016. old_current_genericdef,
  1017. old_current_specializedef: tobjectdef;
  1018. begin
  1019. locationstr:='';
  1020. pd:=nil;
  1021. case token of
  1022. _FUNCTION :
  1023. begin
  1024. consume(_FUNCTION);
  1025. if parse_proc_head(aclass,potype_function,pd) then
  1026. begin
  1027. { pd=nil when it is a interface mapping }
  1028. if assigned(pd) then
  1029. begin
  1030. if try_to_consume(_COLON) then
  1031. begin
  1032. old_parse_generic:=parse_generic;
  1033. inc(testcurobject);
  1034. { Add ObjectSymtable to be able to find generic type definitions }
  1035. popclass:=0;
  1036. if assigned(pd._class) and
  1037. (pd.parast.symtablelevel=normal_function_level) and
  1038. (symtablestack.top.symtabletype<>ObjectSymtable) then
  1039. begin
  1040. popclass:=push_nested_hierarchy(pd._class);
  1041. parse_generic:=(df_generic in pd._class.defoptions);
  1042. old_current_objectdef:=current_objectdef;
  1043. old_current_genericdef:=current_genericdef;
  1044. old_current_specializedef:=current_specializedef;
  1045. current_objectdef:=pd._class;
  1046. if assigned(current_objectdef) and (df_generic in current_objectdef.defoptions) then
  1047. current_genericdef:=current_objectdef;
  1048. if assigned(current_objectdef) and (df_specialization in current_objectdef.defoptions) then
  1049. current_specializedef:=current_objectdef;
  1050. end;
  1051. single_type(pd.returndef,false,false);
  1052. if is_dispinterface(pd._class) and not is_automatable(pd.returndef) then
  1053. Message1(type_e_not_automatable,pd.returndef.typename);
  1054. if popclass>0 then
  1055. begin
  1056. current_objectdef:=old_current_objectdef;
  1057. current_genericdef:=old_current_genericdef;
  1058. current_specializedef:=old_current_specializedef;
  1059. dec(popclass,pop_nested_hierarchy(pd._class));
  1060. if popclass<>0 then
  1061. internalerror(201012020);
  1062. end;
  1063. dec(testcurobject);
  1064. parse_generic:=old_parse_generic;
  1065. if (target_info.system in [system_m68k_amiga]) then
  1066. begin
  1067. if (idtoken=_LOCATION) then
  1068. begin
  1069. if po_explicitparaloc in pd.procoptions then
  1070. begin
  1071. consume(_LOCATION);
  1072. locationstr:=cstringpattern;
  1073. consume(_CSTRING);
  1074. end
  1075. else
  1076. { I guess this needs a new message... (KB) }
  1077. Message(parser_e_paraloc_all_paras);
  1078. end
  1079. else
  1080. begin
  1081. if po_explicitparaloc in pd.procoptions then
  1082. { assign default locationstr, if none specified }
  1083. { and we've arguments with explicit paraloc }
  1084. locationstr:='D0';
  1085. end;
  1086. end;
  1087. end
  1088. else
  1089. begin
  1090. if (
  1091. parse_only and
  1092. not(is_interface(pd._class))
  1093. ) or
  1094. (m_repeat_forward in current_settings.modeswitches) then
  1095. begin
  1096. consume(_COLON);
  1097. consume_all_until(_SEMICOLON);
  1098. end;
  1099. end;
  1100. if isclassmethod then
  1101. include(pd.procoptions,po_classmethod);
  1102. end;
  1103. end
  1104. else
  1105. begin
  1106. { recover }
  1107. consume(_COLON);
  1108. consume_all_until(_SEMICOLON);
  1109. end;
  1110. end;
  1111. _PROCEDURE :
  1112. begin
  1113. consume(_PROCEDURE);
  1114. if parse_proc_head(aclass,potype_procedure,pd) then
  1115. begin
  1116. { pd=nil when it is an interface mapping }
  1117. if assigned(pd) then
  1118. begin
  1119. pd.returndef:=voidtype;
  1120. if isclassmethod then
  1121. include(pd.procoptions,po_classmethod);
  1122. end;
  1123. end;
  1124. end;
  1125. _CONSTRUCTOR :
  1126. begin
  1127. consume(_CONSTRUCTOR);
  1128. if isclassmethod then
  1129. parse_proc_head(aclass,potype_class_constructor,pd)
  1130. else
  1131. parse_proc_head(aclass,potype_constructor,pd);
  1132. if not isclassmethod and
  1133. assigned(pd) and
  1134. assigned(pd._class) then
  1135. begin
  1136. { Set return type, class constructors return the
  1137. created instance, object constructors return boolean }
  1138. if is_class(pd._class) then
  1139. pd.returndef:=pd._class
  1140. else
  1141. {$ifdef CPU64bitaddr}
  1142. pd.returndef:=bool64type;
  1143. {$else CPU64bitaddr}
  1144. pd.returndef:=bool32type;
  1145. {$endif CPU64bitaddr}
  1146. end
  1147. else
  1148. pd.returndef:=voidtype;
  1149. end;
  1150. _DESTRUCTOR :
  1151. begin
  1152. consume(_DESTRUCTOR);
  1153. if isclassmethod then
  1154. parse_proc_head(aclass,potype_class_destructor,pd)
  1155. else
  1156. parse_proc_head(aclass,potype_destructor,pd);
  1157. if assigned(pd) then
  1158. pd.returndef:=voidtype;
  1159. end;
  1160. _OPERATOR :
  1161. begin
  1162. consume(_OPERATOR);
  1163. if (token in [first_overloaded..last_overloaded]) then
  1164. begin
  1165. optoken:=token;
  1166. end
  1167. else
  1168. begin
  1169. { Use the dummy NOTOKEN that is also declared
  1170. for the overloaded_operator[] }
  1171. optoken:=NOTOKEN;
  1172. case token of
  1173. _CARET:
  1174. Message1(parser_e_overload_operator_failed,'**');
  1175. _ID:
  1176. if idtoken = _ENUMERATOR then
  1177. optoken := _OP_ENUMERATOR
  1178. else
  1179. Message1(parser_e_overload_operator_failed,'');
  1180. _UNEQUAL:
  1181. Message1(parser_e_overload_operator_failed,'=');
  1182. else
  1183. Message1(parser_e_overload_operator_failed,'');
  1184. end;
  1185. end;
  1186. consume(token);
  1187. parse_proc_head(aclass,potype_operator,pd);
  1188. if assigned(pd) then
  1189. begin
  1190. { operators always need to be searched in all units }
  1191. include(pd.procoptions,po_overload);
  1192. if pd.parast.symtablelevel>normal_function_level then
  1193. Message(parser_e_no_local_operator);
  1194. if token<>_ID then
  1195. begin
  1196. if not(m_result in current_settings.modeswitches) then
  1197. consume(_ID);
  1198. end
  1199. else
  1200. begin
  1201. pd.resultname:=stringdup(orgpattern);
  1202. consume(_ID);
  1203. end;
  1204. if not try_to_consume(_COLON) then
  1205. begin
  1206. consume(_COLON);
  1207. pd.returndef:=generrordef;
  1208. consume_all_until(_SEMICOLON);
  1209. end
  1210. else
  1211. begin
  1212. single_type(pd.returndef,false,false);
  1213. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  1214. ((pd.returndef.typ<>orddef) or
  1215. (torddef(pd.returndef).ordtype<>pasbool)) then
  1216. Message(parser_e_comparative_operator_return_boolean);
  1217. if (optoken=_ASSIGNMENT) and
  1218. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  1219. message(parser_e_no_such_assignment)
  1220. else if not isoperatoracceptable(pd,optoken) then
  1221. Message(parser_e_overload_impossible);
  1222. end;
  1223. end
  1224. else
  1225. begin
  1226. { recover }
  1227. try_to_consume(_ID);
  1228. consume(_COLON);
  1229. consume_all_until(_SEMICOLON);
  1230. end;
  1231. end;
  1232. end;
  1233. { file types can't be function results }
  1234. if assigned(pd) and
  1235. (pd.returndef.typ=filedef) then
  1236. message(parser_e_illegal_function_result);
  1237. { support procedure proc stdcall export; }
  1238. if not(check_proc_directive(false)) then
  1239. begin
  1240. if (token=_COLON) then
  1241. begin
  1242. message(parser_e_field_not_allowed_here);
  1243. consume_all_until(_SEMICOLON);
  1244. end;
  1245. consume(_SEMICOLON);
  1246. end;
  1247. result:=pd;
  1248. if locationstr<>'' then
  1249. begin
  1250. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1251. { I guess this needs a new message... (KB) }
  1252. message(parser_e_illegal_explicit_paraloc);
  1253. end;
  1254. end;
  1255. {****************************************************************************
  1256. Procedure directive handlers
  1257. ****************************************************************************}
  1258. procedure pd_far(pd:tabstractprocdef);
  1259. begin
  1260. Message1(parser_w_proc_directive_ignored,'FAR');
  1261. end;
  1262. procedure pd_near(pd:tabstractprocdef);
  1263. begin
  1264. Message1(parser_w_proc_directive_ignored,'NEAR');
  1265. end;
  1266. procedure pd_export(pd:tabstractprocdef);
  1267. begin
  1268. if pd.typ<>procdef then
  1269. internalerror(200304264);
  1270. if assigned(tprocdef(pd)._class) then
  1271. Message(parser_e_methods_dont_be_export);
  1272. if pd.parast.symtablelevel>normal_function_level then
  1273. Message(parser_e_dont_nest_export);
  1274. end;
  1275. procedure pd_forward(pd:tabstractprocdef);
  1276. begin
  1277. if pd.typ<>procdef then
  1278. internalerror(200304265);
  1279. tprocdef(pd).forwarddef:=true;
  1280. end;
  1281. procedure pd_alias(pd:tabstractprocdef);
  1282. begin
  1283. if pd.typ<>procdef then
  1284. internalerror(200304266);
  1285. consume(_COLON);
  1286. tprocdef(pd).aliasnames.insert(get_stringconst);
  1287. include(pd.procoptions,po_has_public_name);
  1288. end;
  1289. procedure pd_public(pd:tabstractprocdef);
  1290. begin
  1291. if pd.typ<>procdef then
  1292. internalerror(200304266);
  1293. if try_to_consume(_NAME) then
  1294. begin
  1295. tprocdef(pd).aliasnames.insert(get_stringconst);
  1296. include(pd.procoptions,po_has_public_name);
  1297. end;
  1298. end;
  1299. procedure pd_asmname(pd:tabstractprocdef);
  1300. begin
  1301. if pd.typ<>procdef then
  1302. internalerror(200304267);
  1303. if token=_CCHAR then
  1304. begin
  1305. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1306. consume(_CCHAR)
  1307. end
  1308. else
  1309. begin
  1310. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1311. consume(_CSTRING);
  1312. end;
  1313. { we don't need anything else }
  1314. tprocdef(pd).forwarddef:=false;
  1315. end;
  1316. procedure pd_internconst(pd:tabstractprocdef);
  1317. var v:Tconstexprint;
  1318. begin
  1319. if pd.typ<>procdef then
  1320. internalerror(200304268);
  1321. consume(_COLON);
  1322. v:=get_intconst;
  1323. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1324. message(parser_e_range_check_error)
  1325. else
  1326. Tprocdef(pd).extnumber:=longint(v.svalue);
  1327. end;
  1328. procedure pd_internproc(pd:tabstractprocdef);
  1329. var v:Tconstexprint;
  1330. begin
  1331. if pd.typ<>procdef then
  1332. internalerror(200304268);
  1333. consume(_COLON);
  1334. v:=get_intconst;
  1335. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1336. message(parser_e_range_check_error)
  1337. else
  1338. Tprocdef(pd).extnumber:=longint(v.svalue);
  1339. { the proc is defined }
  1340. tprocdef(pd).forwarddef:=false;
  1341. end;
  1342. procedure pd_interrupt(pd:tabstractprocdef);
  1343. begin
  1344. if pd.parast.symtablelevel>normal_function_level then
  1345. Message(parser_e_dont_nest_interrupt);
  1346. end;
  1347. procedure pd_abstract(pd:tabstractprocdef);
  1348. begin
  1349. if pd.typ<>procdef then
  1350. internalerror(200304269);
  1351. if oo_is_sealed in tprocdef(pd)._class.objectoptions then
  1352. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1353. else
  1354. if (po_virtualmethod in pd.procoptions) then
  1355. include(pd.procoptions,po_abstractmethod)
  1356. else
  1357. Message(parser_e_only_virtual_methods_abstract);
  1358. { the method is defined }
  1359. tprocdef(pd).forwarddef:=false;
  1360. end;
  1361. procedure pd_final(pd:tabstractprocdef);
  1362. begin
  1363. if pd.typ<>procdef then
  1364. internalerror(200910170);
  1365. if (po_virtualmethod in pd.procoptions) then
  1366. include(pd.procoptions,po_finalmethod)
  1367. else
  1368. Message(parser_e_only_virtual_methods_final);
  1369. end;
  1370. procedure pd_enumerator(pd:tabstractprocdef);
  1371. begin
  1372. if pd.typ<>procdef then
  1373. internalerror(200910250);
  1374. if (token = _ID) then
  1375. begin
  1376. if pattern='MOVENEXT' then
  1377. begin
  1378. if oo_has_enumerator_movenext in tprocdef(pd)._class.objectoptions then
  1379. message(parser_e_only_one_enumerator_movenext);
  1380. pd.calcparas;
  1381. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1382. (pd.minparacount = 0) then
  1383. begin
  1384. include(tprocdef(pd)._class.objectoptions, oo_has_enumerator_movenext);
  1385. include(pd.procoptions,po_enumerator_movenext);
  1386. end
  1387. else
  1388. Message(parser_e_enumerator_movenext_is_not_valid)
  1389. end
  1390. else
  1391. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1392. consume(token);
  1393. end
  1394. else
  1395. Message(parser_e_enumerator_identifier_required);
  1396. end;
  1397. procedure pd_virtual(pd:tabstractprocdef);
  1398. {$ifdef WITHDMT}
  1399. var
  1400. pt : tnode;
  1401. {$endif WITHDMT}
  1402. begin
  1403. if pd.typ<>procdef then
  1404. internalerror(2003042610);
  1405. if (pd.proctypeoption=potype_constructor) and
  1406. is_object(tprocdef(pd)._class) then
  1407. Message(parser_e_constructor_cannot_be_not_virtual);
  1408. {$ifdef WITHDMT}
  1409. if is_object(tprocdef(pd)._class) and
  1410. (token<>_SEMICOLON) then
  1411. begin
  1412. { any type of parameter is allowed here! }
  1413. pt:=comp_expr(true);
  1414. if is_constintnode(pt) then
  1415. begin
  1416. include(pd.procoptions,po_msgint);
  1417. pd.messageinf.i:=pt.value;
  1418. end
  1419. else
  1420. Message(parser_e_ill_msg_expr);
  1421. disposetree(pt);
  1422. end;
  1423. {$endif WITHDMT}
  1424. end;
  1425. procedure pd_dispid(pd:tabstractprocdef);
  1426. var pt:Tnode;
  1427. begin
  1428. if pd.typ<>procdef then
  1429. internalerror(200604301);
  1430. pt:=comp_expr(true);
  1431. if is_constintnode(pt) then
  1432. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1433. message(parser_e_range_check_error)
  1434. else
  1435. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1436. else
  1437. message(parser_e_dispid_must_be_ord_const);
  1438. pt.free;
  1439. end;
  1440. procedure pd_static(pd:tabstractprocdef);
  1441. begin
  1442. if pd.typ=procdef then
  1443. include(tprocdef(pd).procsym.symoptions,sp_static);
  1444. include(pd.procoptions,po_staticmethod);
  1445. end;
  1446. procedure pd_override(pd:tabstractprocdef);
  1447. begin
  1448. if pd.typ<>procdef then
  1449. internalerror(2003042611);
  1450. if not(is_class_or_interface_or_objc(tprocdef(pd)._class)) then
  1451. Message(parser_e_no_object_override)
  1452. else if is_objccategory(tprocdef(pd)._class) then
  1453. Message(parser_e_no_category_override);
  1454. end;
  1455. procedure pd_overload(pd:tabstractprocdef);
  1456. begin
  1457. if pd.typ<>procdef then
  1458. internalerror(2003042612);
  1459. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1460. end;
  1461. procedure pd_message(pd:tabstractprocdef);
  1462. var
  1463. pt : tnode;
  1464. paracnt : longint;
  1465. begin
  1466. if pd.typ<>procdef then
  1467. internalerror(2003042613);
  1468. if not is_class(tprocdef(pd)._class) and
  1469. not is_objc_class_or_protocol(tprocdef(pd)._class) then
  1470. Message(parser_e_msg_only_for_classes);
  1471. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1472. Message(parser_e_multiple_messages);
  1473. { check parameter type }
  1474. if not is_objc_class_or_protocol(tprocdef(pd)._class) then
  1475. begin
  1476. paracnt:=0;
  1477. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1478. if paracnt<>1 then
  1479. Message(parser_e_ill_msg_param);
  1480. end;
  1481. pt:=comp_expr(true);
  1482. { message is 1-character long }
  1483. if is_constcharnode(pt) then
  1484. begin
  1485. include(pd.procoptions,po_msgstr);
  1486. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1487. end
  1488. else if pt.nodetype=stringconstn then
  1489. begin
  1490. include(pd.procoptions,po_msgstr);
  1491. if (tstringconstnode(pt).len>255) then
  1492. Message(parser_e_message_string_too_long);
  1493. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1494. end
  1495. else
  1496. if is_constintnode(pt) and
  1497. is_class(tprocdef(pd)._class) then
  1498. begin
  1499. include(pd.procoptions,po_msgint);
  1500. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1501. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1502. message(parser_e_range_check_error)
  1503. else
  1504. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1505. end
  1506. else
  1507. Message(parser_e_ill_msg_expr);
  1508. { check whether the selector name is valid in case of Objective-C }
  1509. if (po_msgstr in pd.procoptions) and
  1510. is_objc_class_or_protocol(tprocdef(pd)._class) and
  1511. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1512. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1513. pt.free;
  1514. end;
  1515. procedure pd_reintroduce(pd:tabstractprocdef);
  1516. begin
  1517. if pd.typ<>procdef then
  1518. internalerror(200401211);
  1519. if not(is_class_or_interface_or_object(tprocdef(pd)._class)) and
  1520. not(is_objccategory(tprocdef(pd)._class)) then
  1521. Message(parser_e_no_object_reintroduce);
  1522. end;
  1523. procedure pd_syscall(pd:tabstractprocdef);
  1524. {$if defined(powerpc) or defined(m68k)}
  1525. var
  1526. vs : tparavarsym;
  1527. sym : tsym;
  1528. symtable : TSymtable;
  1529. v: Tconstexprint;
  1530. {$endif defined(powerpc) or defined(m68k)}
  1531. begin
  1532. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1533. internalerror(2003042614);
  1534. tprocdef(pd).forwarddef:=false;
  1535. {$ifdef m68k}
  1536. if target_info.system in [system_m68k_amiga] then
  1537. begin
  1538. include(pd.procoptions,po_syscall_legacy);
  1539. if consume_sym(sym,symtable) then
  1540. begin
  1541. if (sym.typ=staticvarsym) and
  1542. (
  1543. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1544. is_32bitint(tabstractvarsym(sym).vardef)
  1545. ) then
  1546. begin
  1547. tprocdef(pd).libsym:=sym;
  1548. if po_syscall_legacy in tprocdef(pd).procoptions then
  1549. begin
  1550. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1551. paramanager.parseparaloc(vs,'A6');
  1552. pd.parast.insert(vs);
  1553. end
  1554. end
  1555. else
  1556. Message(parser_e_32bitint_or_pointer_variable_expected);
  1557. end;
  1558. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1559. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1560. v:=get_intconst;
  1561. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1562. message(parser_e_range_check_error)
  1563. else
  1564. Tprocdef(pd).extnumber:=v.uvalue;
  1565. end;
  1566. {$endif m68k}
  1567. {$ifdef powerpc}
  1568. if target_info.system = system_powerpc_amiga then
  1569. begin
  1570. include(pd.procoptions,po_syscall_basesysv);
  1571. if consume_sym(sym,symtable) then
  1572. begin
  1573. if (sym.typ=staticvarsym) and
  1574. (
  1575. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1576. is_32bitint(tabstractvarsym(sym).vardef)
  1577. ) then
  1578. begin
  1579. tprocdef(pd).libsym:=sym;
  1580. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1581. pd.parast.insert(vs);
  1582. end
  1583. else
  1584. Message(parser_e_32bitint_or_pointer_variable_expected);
  1585. end;
  1586. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1587. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1588. v:=get_intconst;
  1589. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1590. message(parser_e_range_check_error)
  1591. else
  1592. Tprocdef(pd).extnumber:=v.uvalue;
  1593. end else
  1594. if target_info.system = system_powerpc_morphos then
  1595. begin
  1596. if idtoken=_LEGACY then
  1597. begin
  1598. consume(_LEGACY);
  1599. include(pd.procoptions,po_syscall_legacy);
  1600. end
  1601. else if idtoken=_SYSV then
  1602. begin
  1603. consume(_SYSV);
  1604. include(pd.procoptions,po_syscall_sysv);
  1605. end
  1606. else if idtoken=_BASESYSV then
  1607. begin
  1608. consume(_BASESYSV);
  1609. include(pd.procoptions,po_syscall_basesysv);
  1610. end
  1611. else if idtoken=_SYSVBASE then
  1612. begin
  1613. consume(_SYSVBASE);
  1614. include(pd.procoptions,po_syscall_sysvbase);
  1615. end
  1616. else if idtoken=_R12BASE then
  1617. begin
  1618. consume(_R12BASE);
  1619. include(pd.procoptions,po_syscall_r12base);
  1620. end
  1621. else
  1622. if syscall_convention='LEGACY' then
  1623. include(pd.procoptions,po_syscall_legacy)
  1624. else if syscall_convention='SYSV' then
  1625. include(pd.procoptions,po_syscall_sysv)
  1626. else if syscall_convention='BASESYSV' then
  1627. include(pd.procoptions,po_syscall_basesysv)
  1628. else if syscall_convention='SYSVBASE' then
  1629. include(pd.procoptions,po_syscall_sysvbase)
  1630. else if syscall_convention='R12BASE' then
  1631. include(pd.procoptions,po_syscall_r12base)
  1632. else
  1633. internalerror(2005010404);
  1634. if consume_sym(sym,symtable) then
  1635. begin
  1636. if (sym.typ=staticvarsym) and
  1637. (
  1638. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1639. is_32bitint(tabstractvarsym(sym).vardef)
  1640. ) then
  1641. begin
  1642. tprocdef(pd).libsym:=sym;
  1643. if po_syscall_legacy in tprocdef(pd).procoptions then
  1644. begin
  1645. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1646. paramanager.parseparaloc(vs,'A6');
  1647. pd.parast.insert(vs);
  1648. end
  1649. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1650. begin
  1651. { Nothing to be done for sysv here for now, but this might change }
  1652. end
  1653. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1654. begin
  1655. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1656. pd.parast.insert(vs);
  1657. end
  1658. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1659. begin
  1660. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1661. pd.parast.insert(vs);
  1662. end
  1663. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1664. begin
  1665. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1666. paramanager.parseparaloc(vs,'R12');
  1667. pd.parast.insert(vs);
  1668. end
  1669. else
  1670. internalerror(2005010501);
  1671. end
  1672. else
  1673. Message(parser_e_32bitint_or_pointer_variable_expected);
  1674. end;
  1675. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1676. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1677. v:=get_intconst;
  1678. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1679. message(parser_e_range_check_error)
  1680. else
  1681. Tprocdef(pd).extnumber:=v.uvalue;
  1682. end;
  1683. {$endif powerpc}
  1684. end;
  1685. procedure pd_external(pd:tabstractprocdef);
  1686. {
  1687. If import_dll=nil the procedure is assumed to be in another
  1688. object file. In that object file it should have the name to
  1689. which import_name is pointing to. Otherwise, the procedure is
  1690. assumed to be in the DLL to which import_dll is pointing to. In
  1691. that case either import_nr<>0 or import_name<>nil is true, so
  1692. the procedure is either imported by number or by name. (DM)
  1693. }
  1694. var
  1695. hs : string;
  1696. v:Tconstexprint;
  1697. begin
  1698. if pd.typ<>procdef then
  1699. internalerror(2003042615);
  1700. with tprocdef(pd) do
  1701. begin
  1702. forwarddef:=false;
  1703. { forbid local external procedures }
  1704. if parast.symtablelevel>normal_function_level then
  1705. Message(parser_e_no_local_proc_external);
  1706. { If the procedure should be imported from a DLL, a constant string follows.
  1707. This isn't really correct, an contant string expression follows
  1708. so we check if an semicolon follows, else a string constant have to
  1709. follow (FK) }
  1710. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1711. begin
  1712. { Always add library prefix and suffix to create an uniform name }
  1713. hs:=get_stringconst;
  1714. if ExtractFileExt(hs)='' then
  1715. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1716. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1717. hs:=target_info.sharedlibprefix+hs;
  1718. import_dll:=stringdup(hs);
  1719. include(procoptions,po_has_importdll);
  1720. if (idtoken=_NAME) then
  1721. begin
  1722. consume(_NAME);
  1723. import_name:=stringdup(get_stringconst);
  1724. include(procoptions,po_has_importname);
  1725. if import_name^='' then
  1726. message(parser_e_empty_import_name);
  1727. end;
  1728. if (idtoken=_INDEX) then
  1729. begin
  1730. {After the word index follows the index number in the DLL.}
  1731. consume(_INDEX);
  1732. v:=get_intconst;
  1733. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1734. message(parser_e_range_check_error)
  1735. else
  1736. import_nr:=longint(v.svalue);
  1737. end;
  1738. { default is to used the realname of the procedure }
  1739. if (import_nr=0) and not assigned(import_name) then
  1740. begin
  1741. import_name:=stringdup(procsym.realname);
  1742. include(procoptions,po_has_importname);
  1743. end;
  1744. end
  1745. else
  1746. begin
  1747. if (idtoken=_NAME) then
  1748. begin
  1749. consume(_NAME);
  1750. import_name:=stringdup(get_stringconst);
  1751. include(procoptions,po_has_importname);
  1752. if import_name^='' then
  1753. message(parser_e_empty_import_name);
  1754. end;
  1755. end;
  1756. end;
  1757. end;
  1758. procedure pd_weakexternal(pd:tabstractprocdef);
  1759. begin
  1760. if not(target_info.system in systems_weak_linking) then
  1761. message(parser_e_weak_external_not_supported)
  1762. else
  1763. pd_external(pd);
  1764. end;
  1765. type
  1766. pd_handler=procedure(pd:tabstractprocdef);
  1767. proc_dir_rec=record
  1768. idtok : ttoken;
  1769. pd_flags : tpdflags;
  1770. handler : pd_handler;
  1771. pocall : tproccalloption;
  1772. pooption : tprocoptions;
  1773. mutexclpocall : tproccalloptions;
  1774. mutexclpotype : tproctypeoptions;
  1775. mutexclpo : tprocoptions;
  1776. end;
  1777. const
  1778. {Should contain the number of procedure directives we support.}
  1779. num_proc_directives=42;
  1780. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1781. (
  1782. (
  1783. idtok:_ABSTRACT;
  1784. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1785. handler : @pd_abstract;
  1786. pocall : pocall_none;
  1787. pooption : [po_abstractmethod];
  1788. mutexclpocall : [pocall_internproc];
  1789. mutexclpotype : [];
  1790. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1791. ),(
  1792. idtok:_ALIAS;
  1793. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1794. handler : @pd_alias;
  1795. pocall : pocall_none;
  1796. pooption : [];
  1797. mutexclpocall : [];
  1798. mutexclpotype : [];
  1799. mutexclpo : [po_external,po_inline]
  1800. ),(
  1801. idtok:_ASMNAME;
  1802. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1803. handler : @pd_asmname;
  1804. pocall : pocall_cdecl;
  1805. pooption : [po_external];
  1806. mutexclpocall : [pocall_internproc];
  1807. mutexclpotype : [];
  1808. mutexclpo : [po_external,po_inline]
  1809. ),(
  1810. idtok:_ASSEMBLER;
  1811. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1812. handler : nil;
  1813. pocall : pocall_none;
  1814. pooption : [po_assembler];
  1815. mutexclpocall : [];
  1816. mutexclpotype : [];
  1817. mutexclpo : [po_external]
  1818. ),(
  1819. idtok:_C; {same as cdecl for mode mac}
  1820. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1821. handler : nil;
  1822. pocall : pocall_cdecl;
  1823. pooption : [];
  1824. mutexclpocall : [];
  1825. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1826. mutexclpo : [po_assembler,po_external]
  1827. ),(
  1828. idtok:_CDECL;
  1829. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1830. handler : nil;
  1831. pocall : pocall_cdecl;
  1832. pooption : [];
  1833. mutexclpocall : [];
  1834. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1835. mutexclpo : [po_assembler,po_external]
  1836. ),(
  1837. idtok:_DISPID;
  1838. pd_flags : [pd_dispinterface];
  1839. handler : @pd_dispid;
  1840. pocall : pocall_none;
  1841. pooption : [po_dispid];
  1842. mutexclpocall : [pocall_internproc];
  1843. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1844. mutexclpo : [po_interrupt,po_external,po_inline]
  1845. ),(
  1846. idtok:_DYNAMIC;
  1847. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1848. handler : @pd_virtual;
  1849. pocall : pocall_none;
  1850. pooption : [po_virtualmethod];
  1851. mutexclpocall : [pocall_internproc];
  1852. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  1853. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1854. ),(
  1855. idtok:_EXPORT;
  1856. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1857. handler : @pd_export;
  1858. pocall : pocall_none;
  1859. pooption : [po_exports,po_global];
  1860. mutexclpocall : [pocall_internproc];
  1861. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1862. mutexclpo : [po_external,po_interrupt,po_inline]
  1863. ),(
  1864. idtok:_EXTERNAL;
  1865. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1866. handler : @pd_external;
  1867. pocall : pocall_none;
  1868. pooption : [po_external];
  1869. mutexclpocall : [pocall_internproc,pocall_syscall];
  1870. { allowed for external cpp classes }
  1871. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  1872. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1873. ),(
  1874. idtok:_FAR;
  1875. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1876. handler : @pd_far;
  1877. pocall : pocall_none;
  1878. pooption : [];
  1879. mutexclpocall : [pocall_internproc];
  1880. mutexclpotype : [];
  1881. mutexclpo : [po_inline]
  1882. ),(
  1883. idtok:_FAR16;
  1884. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1885. handler : nil;
  1886. pocall : pocall_far16;
  1887. pooption : [];
  1888. mutexclpocall : [];
  1889. mutexclpotype : [];
  1890. mutexclpo : [po_external]
  1891. ),(
  1892. idtok:_FINAL;
  1893. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1894. handler : @pd_final;
  1895. pocall : pocall_none;
  1896. pooption : [po_finalmethod];
  1897. mutexclpocall : [pocall_internproc];
  1898. mutexclpotype : [];
  1899. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1900. ),(
  1901. idtok:_FORWARD;
  1902. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1903. handler : @pd_forward;
  1904. pocall : pocall_none;
  1905. pooption : [];
  1906. mutexclpocall : [pocall_internproc];
  1907. mutexclpotype : [];
  1908. mutexclpo : [po_external,po_inline]
  1909. ),(
  1910. idtok:_OLDFPCCALL;
  1911. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1912. handler : nil;
  1913. pocall : pocall_oldfpccall;
  1914. pooption : [];
  1915. mutexclpocall : [];
  1916. mutexclpotype : [];
  1917. mutexclpo : []
  1918. ),(
  1919. idtok:_INLINE;
  1920. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1921. handler : nil;
  1922. pocall : pocall_none;
  1923. pooption : [po_inline];
  1924. mutexclpocall : [];
  1925. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1926. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1927. ),(
  1928. idtok:_INTERNCONST;
  1929. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf];
  1930. handler : @pd_internconst;
  1931. pocall : pocall_none;
  1932. pooption : [po_internconst];
  1933. mutexclpocall : [];
  1934. mutexclpotype : [potype_operator];
  1935. mutexclpo : []
  1936. ),(
  1937. idtok:_INTERNPROC;
  1938. pd_flags : [pd_interface,pd_notobject,pd_notobjintf];
  1939. handler : @pd_internproc;
  1940. pocall : pocall_internproc;
  1941. pooption : [];
  1942. mutexclpocall : [];
  1943. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1944. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1945. ),(
  1946. idtok:_INTERRUPT;
  1947. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1948. handler : @pd_interrupt;
  1949. pocall : pocall_oldfpccall;
  1950. pooption : [po_interrupt];
  1951. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1952. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1953. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1954. mutexclpo : [po_external,po_inline]
  1955. ),(
  1956. idtok:_IOCHECK;
  1957. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1958. handler : nil;
  1959. pocall : pocall_none;
  1960. pooption : [po_iocheck];
  1961. mutexclpocall : [pocall_internproc];
  1962. mutexclpotype : [];
  1963. mutexclpo : [po_external]
  1964. ),(
  1965. idtok:_LOCAL;
  1966. pd_flags : [pd_implemen,pd_body];
  1967. handler : nil;
  1968. pocall : pocall_none;
  1969. pooption : [po_kylixlocal];
  1970. mutexclpocall : [pocall_internproc,pocall_far16];
  1971. mutexclpotype : [];
  1972. mutexclpo : [po_external,po_exports]
  1973. ),(
  1974. idtok:_MESSAGE;
  1975. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass, pd_objcprot];
  1976. handler : @pd_message;
  1977. pocall : pocall_none;
  1978. pooption : []; { can be po_msgstr or po_msgint }
  1979. mutexclpocall : [pocall_internproc];
  1980. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1981. mutexclpo : [po_interrupt,po_external,po_inline]
  1982. ),(
  1983. idtok:_MWPASCAL;
  1984. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1985. handler : nil;
  1986. pocall : pocall_mwpascal;
  1987. pooption : [];
  1988. mutexclpocall : [];
  1989. mutexclpotype : [];
  1990. mutexclpo : []
  1991. ),(
  1992. idtok:_NEAR;
  1993. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1994. handler : @pd_near;
  1995. pocall : pocall_none;
  1996. pooption : [];
  1997. mutexclpocall : [pocall_internproc];
  1998. mutexclpotype : [];
  1999. mutexclpo : []
  2000. ),(
  2001. idtok:_NOSTACKFRAME;
  2002. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2003. handler : nil;
  2004. pocall : pocall_none;
  2005. pooption : [po_nostackframe];
  2006. mutexclpocall : [pocall_internproc];
  2007. mutexclpotype : [];
  2008. mutexclpo : []
  2009. ),(
  2010. idtok:_OVERLOAD;
  2011. pd_flags : [pd_implemen,pd_interface,pd_body];
  2012. handler : @pd_overload;
  2013. pocall : pocall_none;
  2014. pooption : [po_overload];
  2015. mutexclpocall : [pocall_internproc];
  2016. mutexclpotype : [];
  2017. mutexclpo : []
  2018. ),(
  2019. idtok:_OVERRIDE;
  2020. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass];
  2021. handler : @pd_override;
  2022. pocall : pocall_none;
  2023. pooption : [po_overridingmethod,po_virtualmethod];
  2024. mutexclpocall : [pocall_internproc];
  2025. mutexclpotype : [];
  2026. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_inline]
  2027. ),(
  2028. idtok:_PASCAL;
  2029. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2030. handler : nil;
  2031. pocall : pocall_pascal;
  2032. pooption : [];
  2033. mutexclpocall : [];
  2034. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2035. mutexclpo : [po_external]
  2036. ),(
  2037. idtok:_PUBLIC;
  2038. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  2039. handler : @pd_public;
  2040. pocall : pocall_none;
  2041. pooption : [po_public,po_global];
  2042. mutexclpocall : [pocall_internproc];
  2043. mutexclpotype : [];
  2044. mutexclpo : [po_external,po_inline]
  2045. ),(
  2046. idtok:_REGISTER;
  2047. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2048. handler : nil;
  2049. pocall : pocall_register;
  2050. pooption : [];
  2051. mutexclpocall : [];
  2052. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2053. mutexclpo : [po_external]
  2054. ),(
  2055. idtok:_REINTRODUCE;
  2056. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass];
  2057. handler : @pd_reintroduce;
  2058. pocall : pocall_none;
  2059. pooption : [po_reintroduce];
  2060. mutexclpocall : [pocall_internproc];
  2061. mutexclpotype : [];
  2062. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2063. ),(
  2064. idtok:_SAFECALL;
  2065. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2066. handler : nil;
  2067. pocall : pocall_safecall;
  2068. pooption : [];
  2069. mutexclpocall : [];
  2070. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2071. mutexclpo : [po_external]
  2072. ),(
  2073. idtok:_SOFTFLOAT;
  2074. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2075. handler : nil;
  2076. pocall : pocall_softfloat;
  2077. pooption : [];
  2078. mutexclpocall : [];
  2079. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2080. { it's available with po_external because the libgcc floating point routines on the arm
  2081. uses this calling convention }
  2082. mutexclpo : []
  2083. ),(
  2084. idtok:_STATIC;
  2085. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_notobjintf];
  2086. handler : @pd_static;
  2087. pocall : pocall_none;
  2088. pooption : [po_staticmethod];
  2089. mutexclpocall : [pocall_internproc];
  2090. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2091. mutexclpo : [po_external,po_interrupt,po_exports]
  2092. ),(
  2093. idtok:_STDCALL;
  2094. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2095. handler : nil;
  2096. pocall : pocall_stdcall;
  2097. pooption : [];
  2098. mutexclpocall : [];
  2099. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2100. mutexclpo : [po_external]
  2101. ),(
  2102. idtok:_SYSCALL;
  2103. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2104. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2105. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2106. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2107. handler : @pd_syscall;
  2108. pocall : pocall_syscall;
  2109. pooption : [];
  2110. mutexclpocall : [];
  2111. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2112. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2113. ),(
  2114. idtok:_VIRTUAL;
  2115. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  2116. handler : @pd_virtual;
  2117. pocall : pocall_none;
  2118. pooption : [po_virtualmethod];
  2119. mutexclpocall : [pocall_internproc];
  2120. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2121. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2122. ),(
  2123. idtok:_CPPDECL;
  2124. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2125. handler : nil;
  2126. pocall : pocall_cppdecl;
  2127. pooption : [];
  2128. mutexclpocall : [];
  2129. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2130. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2131. ),(
  2132. idtok:_VARARGS;
  2133. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass, pd_objcprot];
  2134. handler : nil;
  2135. pocall : pocall_none;
  2136. pooption : [po_varargs];
  2137. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2138. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2139. mutexclpotype : [];
  2140. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2141. ),(
  2142. idtok:_COMPILERPROC;
  2143. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2144. handler : nil;
  2145. pocall : pocall_none;
  2146. pooption : [po_compilerproc];
  2147. mutexclpocall : [];
  2148. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2149. mutexclpo : [po_interrupt]
  2150. ),(
  2151. idtok:_WEAKEXTERNAL;
  2152. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  2153. handler : @pd_weakexternal;
  2154. pocall : pocall_none;
  2155. { mark it both external and weak external, so we don't have to
  2156. adapt all code for external symbols to also check for weak external
  2157. }
  2158. pooption : [po_external,po_weakexternal];
  2159. mutexclpocall : [pocall_internproc,pocall_syscall];
  2160. { allowed for external cpp classes }
  2161. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2162. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2163. ),(
  2164. idtok:_ENUMERATOR;
  2165. pd_flags : [pd_interface,pd_object];
  2166. handler : @pd_enumerator;
  2167. pocall : pocall_none;
  2168. pooption : [];
  2169. mutexclpocall : [pocall_internproc];
  2170. mutexclpotype : [];
  2171. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2172. )
  2173. );
  2174. function check_proc_directive(isprocvar:boolean):boolean;
  2175. var
  2176. i : longint;
  2177. begin
  2178. result:=false;
  2179. for i:=1 to num_proc_directives do
  2180. if proc_direcdata[i].idtok=idtoken then
  2181. begin
  2182. if ((not isprocvar) or
  2183. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2184. { don't eat a public directive in classes }
  2185. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2186. result:=true;
  2187. exit;
  2188. end;
  2189. end;
  2190. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2191. {
  2192. Parse the procedure directive, returns true if a correct directive is found
  2193. }
  2194. var
  2195. p : longint;
  2196. found : boolean;
  2197. name : TIDString;
  2198. begin
  2199. parse_proc_direc:=false;
  2200. name:=tokeninfo^[idtoken].str;
  2201. found:=false;
  2202. { Hint directive? Then exit immediatly }
  2203. if (m_hintdirective in current_settings.modeswitches) then
  2204. begin
  2205. case idtoken of
  2206. _LIBRARY,
  2207. _PLATFORM,
  2208. _UNIMPLEMENTED,
  2209. _EXPERIMENTAL,
  2210. _DEPRECATED :
  2211. exit;
  2212. end;
  2213. end;
  2214. { C directive is MacPas only, because it breaks too much existing code
  2215. on other platforms (PFV) }
  2216. if (idtoken=_C) and
  2217. not(m_mac in current_settings.modeswitches) then
  2218. exit;
  2219. { retrieve data for directive if found }
  2220. for p:=1 to num_proc_directives do
  2221. if proc_direcdata[p].idtok=idtoken then
  2222. begin
  2223. found:=true;
  2224. break;
  2225. end;
  2226. { Check if the procedure directive is known }
  2227. if not found then
  2228. begin
  2229. { parsing a procvar type the name can be any
  2230. next variable !! }
  2231. if ((pdflags * [pd_procvar,pd_object,pd_objcclass,pd_objcprot])=[]) and
  2232. not(idtoken=_PROPERTY) then
  2233. Message1(parser_w_unknown_proc_directive_ignored,name);
  2234. exit;
  2235. end;
  2236. { check if method and directive not for object, like public.
  2237. This needs to be checked also for procvars }
  2238. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2239. (symtablestack.top.symtabletype=ObjectSymtable) and
  2240. { directive allowed for cpp classes? }
  2241. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  2242. exit;
  2243. { Conflicts between directives ? }
  2244. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2245. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2246. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2247. begin
  2248. Message1(parser_e_proc_dir_conflict,name);
  2249. exit;
  2250. end;
  2251. { set calling convention }
  2252. if proc_direcdata[p].pocall<>pocall_none then
  2253. begin
  2254. if (po_hascallingconvention in pd.procoptions) then
  2255. begin
  2256. Message2(parser_w_proc_overriding_calling,
  2257. proccalloptionStr[pd.proccalloption],
  2258. proccalloptionStr[proc_direcdata[p].pocall]);
  2259. end;
  2260. { check if the target processor supports this calling convention }
  2261. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2262. begin
  2263. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2264. { recover }
  2265. proc_direcdata[p].pocall:=pocall_stdcall;
  2266. end;
  2267. pd.proccalloption:=proc_direcdata[p].pocall;
  2268. include(pd.procoptions,po_hascallingconvention);
  2269. end;
  2270. if pd.typ=procdef then
  2271. begin
  2272. { Check if the directive is only for objects }
  2273. if (pd_object in proc_direcdata[p].pd_flags) and
  2274. not assigned(tprocdef(pd)._class) then
  2275. exit;
  2276. { check if method and directive not for interface }
  2277. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2278. is_interface(tprocdef(pd)._class) then
  2279. exit;
  2280. { check if method and directive not for interface }
  2281. if is_dispinterface(tprocdef(pd)._class) and
  2282. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2283. exit;
  2284. { check if method and directive not for objcclass }
  2285. if is_objcclass(tprocdef(pd)._class) and
  2286. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2287. exit;
  2288. { check if method and directive not for objcprotocol }
  2289. if is_objcprotocol(tprocdef(pd)._class) and
  2290. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2291. exit;
  2292. end;
  2293. { consume directive, and turn flag on }
  2294. consume(token);
  2295. parse_proc_direc:=true;
  2296. { Check the pd_flags if the directive should be allowed }
  2297. if (pd_interface in pdflags) and
  2298. not(pd_interface in proc_direcdata[p].pd_flags) then
  2299. begin
  2300. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2301. exit;
  2302. end;
  2303. if (pd_implemen in pdflags) and
  2304. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2305. begin
  2306. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2307. exit;
  2308. end;
  2309. if (pd_procvar in pdflags) and
  2310. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2311. begin
  2312. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2313. exit;
  2314. end;
  2315. { Return the new pd_flags }
  2316. if not(pd_body in proc_direcdata[p].pd_flags) then
  2317. exclude(pdflags,pd_body);
  2318. { Add the correct flag }
  2319. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2320. { Call the handler }
  2321. if pointer(proc_direcdata[p].handler)<>nil then
  2322. proc_direcdata[p].handler(pd);
  2323. end;
  2324. function proc_get_importname(pd:tprocdef):string;
  2325. function maybe_cprefix(const s:string):string;
  2326. begin
  2327. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2328. result:=s
  2329. else
  2330. result:=target_info.Cprefix+s;
  2331. end;
  2332. begin
  2333. result:='';
  2334. if not(po_external in pd.procoptions) then
  2335. internalerror(200412151);
  2336. { external name or number is specified }
  2337. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2338. begin
  2339. if assigned(pd.import_dll) then
  2340. begin
  2341. { If we are not using direct dll linking under win32 then imports
  2342. need to use the normal name since two functions can refer to the
  2343. same DLL function. This is also needed for compatability
  2344. with Delphi and TP7 }
  2345. (*
  2346. case target_info.system of
  2347. system_i386_emx,
  2348. system_i386_os2 :
  2349. begin
  2350. { keep normal mangledname }
  2351. if not (Assigned (PD.Import_Name)) then
  2352. Result := PD.MangledName;
  2353. end;
  2354. else
  2355. *)
  2356. if assigned(pd.import_name) then
  2357. begin
  2358. if target_info.system in (systems_all_windows + systems_nativent +
  2359. [system_i386_emx, system_i386_os2]) then
  2360. { cprefix is not used in DLL imports under Windows or OS/2 }
  2361. result:=pd.import_name^
  2362. else
  2363. result:=maybe_cprefix(pd.import_name^);
  2364. end
  2365. else
  2366. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2367. end
  2368. else
  2369. result:=maybe_cprefix(pd.import_name^);
  2370. end
  2371. else
  2372. begin
  2373. { Default names when importing variables }
  2374. case pd.proccalloption of
  2375. pocall_cdecl :
  2376. begin
  2377. if assigned(pd._class) then
  2378. result:=target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname
  2379. else
  2380. result:=target_info.Cprefix+pd.procsym.realname;
  2381. end;
  2382. pocall_cppdecl :
  2383. begin
  2384. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2385. end;
  2386. else
  2387. begin
  2388. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2389. { but according to MacPas mode description
  2390. Cprefix should still be used PM }
  2391. if (m_mac in current_settings.modeswitches) then
  2392. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2393. end;
  2394. end;
  2395. end;
  2396. end;
  2397. procedure proc_set_mangledname(pd:tprocdef);
  2398. var
  2399. s : string;
  2400. begin
  2401. { When the mangledname is already set we aren't allowed to change
  2402. it because it can already be used somewhere (PFV) }
  2403. if not(po_has_mangledname in pd.procoptions) then
  2404. begin
  2405. if (po_external in pd.procoptions) then
  2406. begin
  2407. { External Procedures are only allowed to change the mangledname
  2408. in their first declaration }
  2409. if (pd.forwarddef or (not pd.hasforward)) then
  2410. begin
  2411. s:=proc_get_importname(pd);
  2412. if s<>'' then
  2413. begin
  2414. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  2415. { This allows to import VC++ mangled names from DLLs. }
  2416. { Do not perform replacement, if external symbol is not imported from DLL. }
  2417. if (target_info.system in systems_all_windows) and (pd.import_dll<>nil) then
  2418. begin
  2419. Replace(s,'?','__q$$');
  2420. {$ifdef arm}
  2421. { @ symbol is not allowed in ARM assembler only }
  2422. Replace(s,'@','__a$$');
  2423. {$endif arm}
  2424. end;
  2425. pd.setmangledname(s);
  2426. end;
  2427. end;
  2428. end
  2429. else
  2430. { Normal procedures }
  2431. begin
  2432. if (po_compilerproc in pd.procoptions) then
  2433. begin
  2434. pd.setmangledname(lower(pd.procsym.name));
  2435. end;
  2436. end;
  2437. end;
  2438. { Public/exported alias names }
  2439. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2440. not(po_has_public_name in pd.procoptions) then
  2441. begin
  2442. case pd.proccalloption of
  2443. pocall_cdecl :
  2444. begin
  2445. if assigned(pd._class) then
  2446. pd.aliasnames.insert(target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname)
  2447. else
  2448. begin
  2449. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2450. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2451. (po_exports in pd.procoptions) then
  2452. pd.aliasnames.insert(pd.procsym.realname)
  2453. else
  2454. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2455. end;
  2456. end;
  2457. pocall_cppdecl :
  2458. begin
  2459. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2460. end;
  2461. end;
  2462. { prevent adding the alias a second time }
  2463. include(pd.procoptions,po_has_public_name);
  2464. end;
  2465. end;
  2466. procedure handle_calling_convention(pd:tabstractprocdef);
  2467. begin
  2468. { set the default calling convention if none provided }
  2469. if (pd.typ=procdef) and
  2470. (is_objc_class_or_protocol(tprocdef(pd)._class) or
  2471. is_cppclass(tprocdef(pd)._class)) then
  2472. begin
  2473. { none of the explicit calling conventions should be allowed }
  2474. if (po_hascallingconvention in pd.procoptions) then
  2475. internalerror(2009032501);
  2476. if is_cppclass(tprocdef(pd)._class) then
  2477. pd.proccalloption:=pocall_cppdecl
  2478. else
  2479. pd.proccalloption:=pocall_cdecl;
  2480. end
  2481. else if not(po_hascallingconvention in pd.procoptions) then
  2482. pd.proccalloption:=current_settings.defproccall
  2483. else
  2484. begin
  2485. if pd.proccalloption=pocall_none then
  2486. internalerror(200309081);
  2487. end;
  2488. { handle proccall specific settings }
  2489. case pd.proccalloption of
  2490. pocall_cdecl,
  2491. pocall_cppdecl :
  2492. begin
  2493. { check C cdecl para types }
  2494. check_c_para(pd);
  2495. end;
  2496. pocall_far16 :
  2497. begin
  2498. { Temporary stub, must be rewritten to support OS/2 far16 }
  2499. Message1(parser_w_proc_directive_ignored,'FAR16');
  2500. end;
  2501. end;
  2502. { Inlining is enabled and supported? }
  2503. if (po_inline in pd.procoptions) and
  2504. not(cs_do_inline in current_settings.localswitches) then
  2505. begin
  2506. { Give an error if inline is not supported by the compiler mode,
  2507. otherwise only give a warning that this procedure will not be inlined }
  2508. if not(m_default_inline in current_settings.modeswitches) then
  2509. Message(parser_e_proc_inline_not_supported)
  2510. else
  2511. Message(parser_w_inlining_disabled);
  2512. exclude(pd.procoptions,po_inline);
  2513. end;
  2514. { For varargs directive also cdecl and external must be defined }
  2515. if (po_varargs in pd.procoptions) then
  2516. begin
  2517. { check first for external in the interface, if available there
  2518. then the cdecl must also be there since there is no implementation
  2519. available to contain it }
  2520. if parse_only then
  2521. begin
  2522. { if external is available, then cdecl must also be available,
  2523. procvars don't need external }
  2524. if not((po_external in pd.procoptions) or
  2525. (pd.typ=procvardef) or
  2526. { for objcclasses this is checked later, because the entire
  2527. class may be external. }
  2528. is_objc_class_or_protocol(tprocdef(pd)._class)) and
  2529. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2530. Message(parser_e_varargs_need_cdecl_and_external);
  2531. end
  2532. else
  2533. begin
  2534. { both must be defined now }
  2535. if not((po_external in pd.procoptions) or
  2536. (pd.typ=procvardef)) or
  2537. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2538. Message(parser_e_varargs_need_cdecl_and_external);
  2539. end;
  2540. end;
  2541. { insert hidden high parameters }
  2542. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2543. { insert hidden self parameter }
  2544. insert_self_and_vmt_para(pd);
  2545. { insert funcret parameter if required }
  2546. insert_funcret_para(pd);
  2547. { Make var parameters regable, this must be done after the calling
  2548. convention is set. }
  2549. { this must be done before parentfp is insert, because getting all cases
  2550. where parentfp must be in a memory location isn't catched properly so
  2551. we put parentfp never in a register }
  2552. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2553. { insert parentfp parameter if required }
  2554. insert_parentfp_para(pd);
  2555. { Calculate parameter tlist }
  2556. pd.calcparas;
  2557. end;
  2558. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2559. {
  2560. Parse the procedure directives. It does not matter if procedure directives
  2561. are written using ;procdir; or ['procdir'] syntax.
  2562. }
  2563. var
  2564. res : boolean;
  2565. begin
  2566. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2567. begin
  2568. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2569. include(pd.procoptions,po_public);
  2570. include(pd.procoptions,po_has_public_name);
  2571. include(pd.procoptions,po_global);
  2572. end;
  2573. { Class constructors and destructor are static class methods in real. }
  2574. { There are many places in the compiler where either class or static }
  2575. { method flag changes the behavior. It is simplier to add them to }
  2576. { the class constructors/destructors options than to fix all the }
  2577. { occurencies. (Paul) }
  2578. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2579. begin
  2580. include(pd.procoptions,po_classmethod);
  2581. include(pd.procoptions,po_staticmethod);
  2582. end;
  2583. while token in [_ID,_LECKKLAMMER] do
  2584. begin
  2585. if try_to_consume(_LECKKLAMMER) then
  2586. begin
  2587. repeat
  2588. parse_proc_direc(pd,pdflags);
  2589. until not try_to_consume(_COMMA);
  2590. consume(_RECKKLAMMER);
  2591. { we always expect at least '[];' }
  2592. res:=true;
  2593. end
  2594. else
  2595. begin
  2596. res:=parse_proc_direc(pd,pdflags);
  2597. end;
  2598. { A procedure directive normally followed by a semicolon, but in
  2599. a const section or reading a type we should stop when _EQUAL is found,
  2600. because a constant/default value follows }
  2601. if res then
  2602. begin
  2603. if (block_type=bt_const_type) and
  2604. (token=_EQUAL) then
  2605. break;
  2606. { support procedure proc;stdcall export; }
  2607. if not(check_proc_directive((pd.typ=procvardef))) then
  2608. begin
  2609. { support "record p : procedure stdcall end;" and
  2610. "var p : procedure stdcall = nil;" }
  2611. if (pd_procvar in pdflags) and
  2612. (token in [_END,_RKLAMMER,_EQUAL]) then
  2613. break
  2614. else
  2615. begin
  2616. if (token=_COLON) then
  2617. begin
  2618. Message(parser_e_field_not_allowed_here);
  2619. consume_all_until(_SEMICOLON);
  2620. end;
  2621. consume(_SEMICOLON)
  2622. end;
  2623. end;
  2624. end
  2625. else
  2626. break;
  2627. end;
  2628. end;
  2629. procedure parse_var_proc_directives(sym:tsym);
  2630. var
  2631. pdflags : tpdflags;
  2632. pd : tabstractprocdef;
  2633. begin
  2634. pdflags:=[pd_procvar];
  2635. pd:=nil;
  2636. case sym.typ of
  2637. fieldvarsym,
  2638. staticvarsym,
  2639. localvarsym,
  2640. paravarsym :
  2641. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2642. typesym :
  2643. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2644. else
  2645. internalerror(2003042617);
  2646. end;
  2647. if pd.typ<>procvardef then
  2648. internalerror(2003042618);
  2649. { names should never be used anyway }
  2650. parse_proc_directives(pd,pdflags);
  2651. end;
  2652. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2653. var
  2654. pdflags : tpdflags;
  2655. begin
  2656. pdflags:=[pd_object];
  2657. parse_proc_directives(pd,pdflags);
  2658. end;
  2659. function proc_add_definition(var currpd:tprocdef):boolean;
  2660. {
  2661. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2662. forwarddef is found and reused it returns true
  2663. }
  2664. var
  2665. fwpd : tprocdef;
  2666. currparasym,
  2667. fwparasym : tsym;
  2668. currparacnt,
  2669. fwparacnt,
  2670. curridx,
  2671. fwidx,
  2672. i : longint;
  2673. po_comp : tprocoptions;
  2674. paracompopt: tcompare_paras_options;
  2675. forwardfound : boolean;
  2676. symentry: TSymEntry;
  2677. begin
  2678. forwardfound:=false;
  2679. { check overloaded functions if the same function already exists }
  2680. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2681. begin
  2682. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2683. { Skip overloaded definitions that are declared in other units }
  2684. if fwpd.procsym<>currpd.procsym then
  2685. continue;
  2686. { check the parameters, for delphi/tp it is possible to
  2687. leave the parameters away in the implementation (forwarddef=false).
  2688. But for an overload declared function this is not allowed }
  2689. if { check if empty implementation arguments match is allowed }
  2690. (
  2691. not(m_repeat_forward in current_settings.modeswitches) and
  2692. not(currpd.forwarddef) and
  2693. is_bareprocdef(currpd) and
  2694. not(po_overload in fwpd.procoptions)
  2695. ) or
  2696. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2697. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2698. (
  2699. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2700. (fwpd.returndef=currpd.returndef)
  2701. ) then
  2702. begin
  2703. { Check if we've found the forwarddef, if found then
  2704. we need to update the forward def with the current
  2705. implementation settings }
  2706. if fwpd.forwarddef then
  2707. begin
  2708. forwardfound:=true;
  2709. if not(m_repeat_forward in current_settings.modeswitches) and
  2710. (fwpd.proccalloption<>currpd.proccalloption) then
  2711. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2712. else
  2713. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2714. { Check calling convention }
  2715. if (fwpd.proccalloption<>currpd.proccalloption) then
  2716. begin
  2717. { In delphi it is possible to specify the calling
  2718. convention in the interface or implementation if
  2719. there was no convention specified in the other
  2720. part }
  2721. if (m_delphi in current_settings.modeswitches) then
  2722. begin
  2723. if not(po_hascallingconvention in currpd.procoptions) then
  2724. currpd.proccalloption:=fwpd.proccalloption
  2725. else
  2726. if not(po_hascallingconvention in fwpd.procoptions) then
  2727. fwpd.proccalloption:=currpd.proccalloption
  2728. else
  2729. begin
  2730. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2731. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2732. { restore interface settings }
  2733. currpd.proccalloption:=fwpd.proccalloption;
  2734. end;
  2735. end
  2736. else
  2737. begin
  2738. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2739. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2740. { restore interface settings }
  2741. currpd.proccalloption:=fwpd.proccalloption;
  2742. end;
  2743. end;
  2744. { Check static }
  2745. if (po_staticmethod in fwpd.procoptions) then
  2746. begin
  2747. if not (po_staticmethod in currpd.procoptions) then
  2748. begin
  2749. include(currpd.procoptions, po_staticmethod);
  2750. if (po_classmethod in currpd.procoptions) then
  2751. begin
  2752. { remove self from the hidden paras }
  2753. symentry:=currpd.parast.Find('self');
  2754. if symentry<>nil then
  2755. begin
  2756. currpd.parast.Delete(symentry);
  2757. currpd.calcparas;
  2758. end;
  2759. end;
  2760. end;
  2761. end;
  2762. { Check if the procedure type and return type are correct,
  2763. also the parameters must match also with the type }
  2764. if ((m_repeat_forward in current_settings.modeswitches) or
  2765. not is_bareprocdef(currpd)) and
  2766. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2767. (fwpd.returndef<>currpd.returndef)) then
  2768. begin
  2769. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2770. fwpd.fullprocname(false));
  2771. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2772. break;
  2773. end;
  2774. { Check if both are declared forward }
  2775. if fwpd.forwarddef and currpd.forwarddef then
  2776. begin
  2777. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2778. currpd.fullprocname(false));
  2779. end;
  2780. { internconst or internproc only need to be defined once }
  2781. if (fwpd.proccalloption=pocall_internproc) then
  2782. currpd.proccalloption:=fwpd.proccalloption
  2783. else
  2784. if (currpd.proccalloption=pocall_internproc) then
  2785. fwpd.proccalloption:=currpd.proccalloption;
  2786. { Check procedure options, Delphi requires that class is
  2787. repeated in the implementation for class methods }
  2788. if (m_fpc in current_settings.modeswitches) then
  2789. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2790. else
  2791. po_comp:=[po_classmethod,po_methodpointer];
  2792. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2793. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2794. begin
  2795. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2796. fwpd.fullprocname(false));
  2797. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2798. { This error is non-fatal, we can recover }
  2799. end;
  2800. { Forward declaration is external? }
  2801. if (po_external in fwpd.procoptions) then
  2802. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2803. { Check parameters }
  2804. if (m_repeat_forward in current_settings.modeswitches) or
  2805. (currpd.minparacount>0) then
  2806. begin
  2807. { If mangled names are equal then they have the same amount of arguments }
  2808. { We can check the names of the arguments }
  2809. { both symtables are in the same order from left to right }
  2810. curridx:=0;
  2811. fwidx:=0;
  2812. currparacnt:=currpd.parast.SymList.Count;
  2813. fwparacnt:=fwpd.parast.SymList.Count;
  2814. repeat
  2815. { skip default parameter constsyms }
  2816. while (curridx<currparacnt) and
  2817. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2818. inc(curridx);
  2819. while (fwidx<fwparacnt) and
  2820. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2821. inc(fwidx);
  2822. { stop when one of the two lists is at the end }
  2823. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2824. break;
  2825. { compare names of parameters, ignore implictly
  2826. renamed parameters }
  2827. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2828. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2829. if not(sp_implicitrename in currparasym.symoptions) and
  2830. not(sp_implicitrename in fwparasym.symoptions) then
  2831. begin
  2832. if (currparasym.name<>fwparasym.name) then
  2833. begin
  2834. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2835. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2836. break;
  2837. end;
  2838. end;
  2839. { next parameter }
  2840. inc(curridx);
  2841. inc(fwidx);
  2842. until false;
  2843. end;
  2844. { Everything is checked, now we can update the forward declaration
  2845. with the new data from the implementation }
  2846. fwpd.forwarddef:=currpd.forwarddef;
  2847. fwpd.hasforward:=true;
  2848. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2849. { marked as local but exported from unit? }
  2850. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2851. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2852. if fwpd.extnumber=$ffff then
  2853. fwpd.extnumber:=currpd.extnumber;
  2854. while not currpd.aliasnames.empty do
  2855. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2856. { update fileinfo so position references the implementation,
  2857. also update funcretsym if it is already generated }
  2858. fwpd.fileinfo:=currpd.fileinfo;
  2859. if assigned(fwpd.funcretsym) then
  2860. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2861. if assigned(currpd.deprecatedmsg) then
  2862. begin
  2863. stringdispose(fwpd.deprecatedmsg);
  2864. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  2865. end;
  2866. { import names }
  2867. if assigned(currpd.import_dll) then
  2868. begin
  2869. stringdispose(fwpd.import_dll);
  2870. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2871. end;
  2872. if assigned(currpd.import_name) then
  2873. begin
  2874. stringdispose(fwpd.import_name);
  2875. fwpd.import_name:=stringdup(currpd.import_name^);
  2876. end;
  2877. fwpd.import_nr:=currpd.import_nr;
  2878. { for compilerproc defines we need to rename and update the
  2879. symbolname to lowercase }
  2880. if (po_compilerproc in fwpd.procoptions) then
  2881. begin
  2882. { rename to lowercase so users can't access it }
  2883. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2884. { the mangeled name is already changed by the pd_compilerproc }
  2885. { handler. It must be done immediately because if we have a }
  2886. { call to a compilerproc before it's implementation is }
  2887. { encountered, it must already use the new mangled name (JM) }
  2888. end;
  2889. { Release current procdef }
  2890. currpd.owner.deletedef(currpd);
  2891. currpd:=fwpd;
  2892. end
  2893. else
  2894. begin
  2895. { abstract methods aren't forward defined, but this }
  2896. { needs another error message }
  2897. if (po_abstractmethod in fwpd.procoptions) then
  2898. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2899. else
  2900. begin
  2901. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2902. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2903. end;
  2904. end;
  2905. { we found one proc with the same arguments, there are no others
  2906. so we can stop }
  2907. break;
  2908. end;
  2909. { check for allowing overload directive }
  2910. if not(m_fpc in current_settings.modeswitches) then
  2911. begin
  2912. { overload directive turns on overloading }
  2913. if ((po_overload in currpd.procoptions) or
  2914. (po_overload in fwpd.procoptions)) then
  2915. begin
  2916. { check if all procs have overloading, but not if the proc is a method or
  2917. already declared forward, then the check is already done }
  2918. if not(fwpd.hasforward or
  2919. assigned(currpd._class) or
  2920. (currpd.forwarddef<>fwpd.forwarddef) or
  2921. ((po_overload in currpd.procoptions) and
  2922. (po_overload in fwpd.procoptions))) then
  2923. begin
  2924. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2925. break;
  2926. end;
  2927. end
  2928. else
  2929. begin
  2930. if not(fwpd.forwarddef) then
  2931. begin
  2932. if (m_tp7 in current_settings.modeswitches) then
  2933. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  2934. else
  2935. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2936. break;
  2937. end;
  2938. end;
  2939. end; { equal arguments }
  2940. end;
  2941. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2942. list }
  2943. if not forwardfound then
  2944. begin
  2945. { can happen in Delphi mode }
  2946. if (currpd.proctypeoption = potype_function) and
  2947. is_void(currpd.returndef) then
  2948. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2949. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2950. end;
  2951. proc_add_definition:=forwardfound;
  2952. end;
  2953. end.