pdecsub.pas 98 KB

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