pdecsub.pas 98 KB

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