pdecsub.pas 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716
  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) and
  769. (pd.parast.symtablelevel=normal_function_level) then
  770. begin
  771. if (df_generic in pd._class.defoptions) then
  772. include(pd.defoptions,df_generic);
  773. if (df_specialization in pd._class.defoptions) then
  774. begin
  775. include(pd.defoptions,df_specialization);
  776. { Find corresponding genericdef, we need it later to
  777. replay the tokens to generate the body }
  778. if not assigned(pd._class.genericdef) then
  779. internalerror(200512113);
  780. genericst:=pd._class.genericdef.GetSymtable(gs_record);
  781. if not assigned(genericst) then
  782. internalerror(200512114);
  783. { We are parsing the same objectdef, the def index numbers
  784. are the same }
  785. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  786. if not assigned(pd.genericdef) or
  787. (pd.genericdef.typ<>procdef) then
  788. internalerror(200512115);
  789. end;
  790. end;
  791. { methods need to be exported }
  792. if assigned(aclass) and
  793. (
  794. (symtablestack.top.symtabletype=ObjectSymtable) or
  795. (symtablestack.top.symtablelevel=main_program_level)
  796. ) then
  797. include(pd.procoptions,po_global);
  798. { symbol options that need to be kept per procdef }
  799. pd.fileinfo:=procstartfilepos;
  800. pd.symoptions:=current_object_option;
  801. { parse parameters }
  802. if token=_LKLAMMER then
  803. begin
  804. { Add ObjectSymtable to be able to find generic type definitions }
  805. popclass:=false;
  806. if assigned(pd._class) and
  807. (pd.parast.symtablelevel=normal_function_level) and
  808. (symtablestack.top.symtabletype<>ObjectSymtable) then
  809. begin
  810. symtablestack.push(pd._class.symtable);
  811. popclass:=true;
  812. end;
  813. { Add parameter symtable }
  814. if pd.parast.symtabletype<>staticsymtable then
  815. symtablestack.push(pd.parast);
  816. parse_parameter_dec(pd);
  817. if pd.parast.symtabletype<>staticsymtable then
  818. symtablestack.pop(pd.parast);
  819. if popclass then
  820. symtablestack.pop(pd._class.symtable);
  821. end;
  822. result:=true;
  823. end;
  824. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  825. var
  826. pd : tprocdef;
  827. isclassmethod : boolean;
  828. locationstr: string;
  829. popclass : boolean;
  830. begin
  831. locationstr:='';
  832. pd:=nil;
  833. isclassmethod:=false;
  834. { read class method }
  835. if try_to_consume(_CLASS) then
  836. begin
  837. { class method only allowed for procedures and functions }
  838. if not(token in [_FUNCTION,_PROCEDURE]) then
  839. Message(parser_e_procedure_or_function_expected);
  840. if is_interface(aclass) then
  841. Message(parser_e_no_static_method_in_interfaces)
  842. else
  843. isclassmethod:=true;
  844. end;
  845. case token of
  846. _FUNCTION :
  847. begin
  848. consume(_FUNCTION);
  849. if parse_proc_head(aclass,potype_function,pd) then
  850. begin
  851. { pd=nil when it is a interface mapping }
  852. if assigned(pd) then
  853. begin
  854. if try_to_consume(_COLON) then
  855. begin
  856. inc(testcurobject);
  857. { Add ObjectSymtable to be able to find generic type definitions }
  858. popclass:=false;
  859. if assigned(pd._class) and
  860. (pd.parast.symtablelevel=normal_function_level) and
  861. (symtablestack.top.symtabletype<>ObjectSymtable) then
  862. begin
  863. symtablestack.push(pd._class.symtable);
  864. popclass:=true;
  865. end;
  866. single_type(pd.returndef,false);
  867. if popclass then
  868. symtablestack.pop(pd._class.symtable);
  869. pd.test_if_fpu_result;
  870. dec(testcurobject);
  871. if (target_info.system in [system_m68k_amiga]) then
  872. begin
  873. if (idtoken=_LOCATION) then
  874. begin
  875. if po_explicitparaloc in pd.procoptions then
  876. begin
  877. consume(_LOCATION);
  878. locationstr:=pattern;
  879. consume(_CSTRING);
  880. end
  881. else
  882. { I guess this needs a new message... (KB) }
  883. Message(parser_e_paraloc_all_paras);
  884. end
  885. else
  886. begin
  887. if po_explicitparaloc in pd.procoptions then
  888. { assign default locationstr, if none specified }
  889. { and we've arguments with explicit paraloc }
  890. locationstr:='D0';
  891. end;
  892. end;
  893. end
  894. else
  895. begin
  896. if (
  897. parse_only and
  898. not(is_interface(pd._class))
  899. ) or
  900. (m_repeat_forward in current_settings.modeswitches) then
  901. begin
  902. consume(_COLON);
  903. consume_all_until(_SEMICOLON);
  904. end;
  905. end;
  906. if isclassmethod then
  907. include(pd.procoptions,po_classmethod);
  908. end;
  909. end
  910. else
  911. begin
  912. { recover }
  913. consume(_COLON);
  914. consume_all_until(_SEMICOLON);
  915. end;
  916. end;
  917. _PROCEDURE :
  918. begin
  919. consume(_PROCEDURE);
  920. if parse_proc_head(aclass,potype_procedure,pd) then
  921. begin
  922. { pd=nil when it is a interface mapping }
  923. if assigned(pd) then
  924. begin
  925. pd.returndef:=voidtype;
  926. if isclassmethod then
  927. include(pd.procoptions,po_classmethod);
  928. end;
  929. end;
  930. end;
  931. _CONSTRUCTOR :
  932. begin
  933. consume(_CONSTRUCTOR);
  934. parse_proc_head(aclass,potype_constructor,pd);
  935. if assigned(pd) and
  936. assigned(pd._class) then
  937. begin
  938. { Set return type, class constructors return the
  939. created instance, object constructors return boolean }
  940. if is_class(pd._class) then
  941. pd.returndef:=pd._class
  942. else
  943. {$ifdef CPU64bit}
  944. pd.returndef:=bool64type;
  945. {$else CPU64bit}
  946. pd.returndef:=bool32type;
  947. {$endif CPU64bit}
  948. end;
  949. end;
  950. _DESTRUCTOR :
  951. begin
  952. consume(_DESTRUCTOR);
  953. parse_proc_head(aclass,potype_destructor,pd);
  954. if assigned(pd) then
  955. pd.returndef:=voidtype;
  956. end;
  957. _OPERATOR :
  958. begin
  959. consume(_OPERATOR);
  960. if (token in [first_overloaded..last_overloaded]) then
  961. begin
  962. optoken:=token;
  963. end
  964. else
  965. begin
  966. Message(parser_e_overload_operator_failed);
  967. { Use the dummy NOTOKEN that is also declared
  968. for the overloaded_operator[] }
  969. optoken:=NOTOKEN;
  970. end;
  971. consume(token);
  972. parse_proc_head(aclass,potype_operator,pd);
  973. if assigned(pd) then
  974. begin
  975. if pd.parast.symtablelevel>normal_function_level then
  976. Message(parser_e_no_local_operator);
  977. if token<>_ID then
  978. begin
  979. if not(m_result in current_settings.modeswitches) then
  980. consume(_ID);
  981. end
  982. else
  983. begin
  984. pd.resultname:=stringdup(orgpattern);
  985. consume(_ID);
  986. end;
  987. if not try_to_consume(_COLON) then
  988. begin
  989. consume(_COLON);
  990. pd.returndef:=generrordef;
  991. consume_all_until(_SEMICOLON);
  992. end
  993. else
  994. begin
  995. single_type(pd.returndef,false);
  996. pd.test_if_fpu_result;
  997. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  998. ((pd.returndef.typ<>orddef) or
  999. (torddef(pd.returndef).ordtype<>bool8bit)) then
  1000. Message(parser_e_comparative_operator_return_boolean);
  1001. if (optoken=_ASSIGNMENT) and
  1002. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  1003. message(parser_e_no_such_assignment)
  1004. else if not isoperatoracceptable(pd,optoken) then
  1005. Message(parser_e_overload_impossible);
  1006. end;
  1007. end
  1008. else
  1009. begin
  1010. { recover }
  1011. try_to_consume(_ID);
  1012. consume(_COLON);
  1013. consume_all_until(_SEMICOLON);
  1014. end;
  1015. end;
  1016. end;
  1017. { file types can't be function results }
  1018. if assigned(pd) and
  1019. (pd.returndef.typ=filedef) then
  1020. message(parser_e_illegal_function_result);
  1021. { support procedure proc stdcall export; }
  1022. if not(check_proc_directive(false)) then
  1023. consume(_SEMICOLON);
  1024. result:=pd;
  1025. if locationstr<>'' then
  1026. begin
  1027. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1028. { I guess this needs a new message... (KB) }
  1029. message(parser_e_illegal_explicit_paraloc);
  1030. end;
  1031. end;
  1032. {****************************************************************************
  1033. Procedure directive handlers
  1034. ****************************************************************************}
  1035. procedure pd_far(pd:tabstractprocdef);
  1036. begin
  1037. Message1(parser_w_proc_directive_ignored,'FAR');
  1038. end;
  1039. procedure pd_near(pd:tabstractprocdef);
  1040. begin
  1041. Message1(parser_w_proc_directive_ignored,'NEAR');
  1042. end;
  1043. procedure pd_export(pd:tabstractprocdef);
  1044. begin
  1045. if pd.typ<>procdef then
  1046. internalerror(200304264);
  1047. if assigned(tprocdef(pd)._class) then
  1048. Message(parser_e_methods_dont_be_export);
  1049. if pd.parast.symtablelevel>normal_function_level then
  1050. Message(parser_e_dont_nest_export);
  1051. end;
  1052. procedure pd_forward(pd:tabstractprocdef);
  1053. begin
  1054. if pd.typ<>procdef then
  1055. internalerror(200304265);
  1056. tprocdef(pd).forwarddef:=true;
  1057. end;
  1058. procedure pd_alias(pd:tabstractprocdef);
  1059. begin
  1060. if pd.typ<>procdef then
  1061. internalerror(200304266);
  1062. consume(_COLON);
  1063. tprocdef(pd).aliasnames.insert(get_stringconst);
  1064. include(pd.procoptions,po_has_public_name);
  1065. end;
  1066. procedure pd_public(pd:tabstractprocdef);
  1067. begin
  1068. if pd.typ<>procdef then
  1069. internalerror(200304266);
  1070. if try_to_consume(_NAME) then
  1071. begin
  1072. tprocdef(pd).aliasnames.insert(get_stringconst);
  1073. include(pd.procoptions,po_has_public_name);
  1074. end;
  1075. end;
  1076. procedure pd_asmname(pd:tabstractprocdef);
  1077. begin
  1078. if pd.typ<>procdef then
  1079. internalerror(200304267);
  1080. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1081. if token=_CCHAR then
  1082. consume(_CCHAR)
  1083. else
  1084. consume(_CSTRING);
  1085. { we don't need anything else }
  1086. tprocdef(pd).forwarddef:=false;
  1087. end;
  1088. procedure pd_inline(pd:tabstractprocdef);
  1089. begin
  1090. { Check if there are parameters that can't be inlined }
  1091. pd.parast.SymList.ForEachCall(@check_inline_para,pd);
  1092. end;
  1093. procedure pd_internconst(pd:tabstractprocdef);
  1094. begin
  1095. if pd.typ<>procdef then
  1096. internalerror(200304268);
  1097. consume(_COLON);
  1098. tprocdef(pd).extnumber:=longint(get_intconst);
  1099. end;
  1100. procedure pd_internproc(pd:tabstractprocdef);
  1101. begin
  1102. if pd.typ<>procdef then
  1103. internalerror(200304268);
  1104. consume(_COLON);
  1105. tprocdef(pd).extnumber:=longint(get_intconst);
  1106. { the proc is defined }
  1107. tprocdef(pd).forwarddef:=false;
  1108. end;
  1109. procedure pd_interrupt(pd:tabstractprocdef);
  1110. begin
  1111. if pd.parast.symtablelevel>normal_function_level then
  1112. Message(parser_e_dont_nest_interrupt);
  1113. end;
  1114. procedure pd_abstract(pd:tabstractprocdef);
  1115. begin
  1116. if pd.typ<>procdef then
  1117. internalerror(200304269);
  1118. if (po_virtualmethod in pd.procoptions) then
  1119. include(pd.procoptions,po_abstractmethod)
  1120. else
  1121. Message(parser_e_only_virtual_methods_abstract);
  1122. { the method is defined }
  1123. tprocdef(pd).forwarddef:=false;
  1124. end;
  1125. procedure pd_virtual(pd:tabstractprocdef);
  1126. {$ifdef WITHDMT}
  1127. var
  1128. pt : tnode;
  1129. {$endif WITHDMT}
  1130. begin
  1131. if pd.typ<>procdef then
  1132. internalerror(2003042610);
  1133. if (pd.proctypeoption=potype_constructor) and
  1134. is_object(tprocdef(pd)._class) then
  1135. Message(parser_e_constructor_cannot_be_not_virtual);
  1136. {$ifdef WITHDMT}
  1137. if is_object(tprocdef(pd)._class) and
  1138. (token<>_SEMICOLON) then
  1139. begin
  1140. { any type of parameter is allowed here! }
  1141. pt:=comp_expr(true);
  1142. if is_constintnode(pt) then
  1143. begin
  1144. include(pd.procoptions,po_msgint);
  1145. pd.messageinf.i:=pt.value;
  1146. end
  1147. else
  1148. Message(parser_e_ill_msg_expr);
  1149. disposetree(pt);
  1150. end;
  1151. {$endif WITHDMT}
  1152. end;
  1153. procedure pd_dispid(pd:tabstractprocdef);
  1154. var
  1155. pt : tnode;
  1156. begin
  1157. if pd.typ<>procdef then
  1158. internalerror(200604301);
  1159. pt:=comp_expr(true);
  1160. if is_constintnode(pt) then
  1161. tprocdef(pd).dispid:=tordconstnode(pt).value
  1162. else
  1163. Message(parser_e_dispid_must_be_ord_const);
  1164. pt.free;
  1165. end;
  1166. procedure pd_static(pd:tabstractprocdef);
  1167. begin
  1168. if (cs_static_keyword in current_settings.moduleswitches) then
  1169. begin
  1170. if pd.typ=procdef then
  1171. include(tprocdef(pd).procsym.symoptions,sp_static);
  1172. include(pd.procoptions,po_staticmethod);
  1173. end;
  1174. end;
  1175. procedure pd_override(pd:tabstractprocdef);
  1176. begin
  1177. if pd.typ<>procdef then
  1178. internalerror(2003042611);
  1179. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1180. Message(parser_e_no_object_override);
  1181. end;
  1182. procedure pd_overload(pd:tabstractprocdef);
  1183. begin
  1184. if pd.typ<>procdef then
  1185. internalerror(2003042612);
  1186. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1187. end;
  1188. procedure pd_message(pd:tabstractprocdef);
  1189. var
  1190. pt : tnode;
  1191. paracnt : longint;
  1192. begin
  1193. if pd.typ<>procdef then
  1194. internalerror(2003042613);
  1195. if not is_class(tprocdef(pd)._class) then
  1196. Message(parser_e_msg_only_for_classes);
  1197. { check parameter type }
  1198. paracnt:=0;
  1199. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1200. if paracnt<>1 then
  1201. Message(parser_e_ill_msg_param);
  1202. pt:=comp_expr(true);
  1203. if pt.nodetype=stringconstn then
  1204. begin
  1205. include(pd.procoptions,po_msgstr);
  1206. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1207. end
  1208. else
  1209. if is_constintnode(pt) then
  1210. begin
  1211. include(pd.procoptions,po_msgint);
  1212. tprocdef(pd).messageinf.i:=tordconstnode(pt).value;
  1213. end
  1214. else
  1215. Message(parser_e_ill_msg_expr);
  1216. pt.free;
  1217. end;
  1218. procedure pd_reintroduce(pd:tabstractprocdef);
  1219. begin
  1220. if pd.typ<>procdef then
  1221. internalerror(200401211);
  1222. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1223. Message(parser_e_no_object_reintroduce);
  1224. end;
  1225. procedure pd_syscall(pd:tabstractprocdef);
  1226. {$if defined(powerpc) or defined(m68k)}
  1227. var
  1228. vs : tparavarsym;
  1229. sym : tsym;
  1230. symtable : TSymtable;
  1231. {$endif defined(powerpc) or defined(m68k)}
  1232. begin
  1233. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1234. internalerror(2003042614);
  1235. tprocdef(pd).forwarddef:=false;
  1236. {$ifdef m68k}
  1237. if target_info.system in [system_m68k_amiga] then
  1238. begin
  1239. include(pd.procoptions,po_syscall_legacy);
  1240. if consume_sym(sym,symtable) then
  1241. begin
  1242. if (sym.typ=staticvarsym) and
  1243. (
  1244. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1245. is_32bitint(tabstractvarsym(sym).vardef)
  1246. ) then
  1247. begin
  1248. tprocdef(pd).libsym:=sym;
  1249. if po_syscall_legacy in tprocdef(pd).procoptions then
  1250. begin
  1251. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1252. paramanager.parseparaloc(vs,'A6');
  1253. pd.parast.insert(vs);
  1254. end
  1255. end
  1256. else
  1257. Message(parser_e_32bitint_or_pointer_variable_expected);
  1258. end;
  1259. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1260. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1261. tprocdef(pd).extnumber:=get_intconst;
  1262. end;
  1263. {$endif m68k}
  1264. {$ifdef powerpc}
  1265. if target_info.system = system_powerpc_amiga then
  1266. begin
  1267. include(pd.procoptions,po_syscall_basesysv);
  1268. if consume_sym(sym,symtable) then
  1269. begin
  1270. if (sym.typ=staticvarsym) and
  1271. (
  1272. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1273. is_32bitint(tabstractvarsym(sym).vardef)
  1274. ) then
  1275. begin
  1276. tprocdef(pd).libsym:=sym;
  1277. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1278. pd.parast.insert(vs);
  1279. end
  1280. else
  1281. Message(parser_e_32bitint_or_pointer_variable_expected);
  1282. end;
  1283. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1284. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1285. tprocdef(pd).extnumber:=get_intconst;
  1286. end else
  1287. if target_info.system = system_powerpc_morphos then
  1288. begin
  1289. if idtoken=_LEGACY then
  1290. begin
  1291. consume(_LEGACY);
  1292. include(pd.procoptions,po_syscall_legacy);
  1293. end
  1294. else if idtoken=_SYSV then
  1295. begin
  1296. consume(_SYSV);
  1297. include(pd.procoptions,po_syscall_sysv);
  1298. end
  1299. else if idtoken=_BASESYSV then
  1300. begin
  1301. consume(_BASESYSV);
  1302. include(pd.procoptions,po_syscall_basesysv);
  1303. end
  1304. else if idtoken=_SYSVBASE then
  1305. begin
  1306. consume(_SYSVBASE);
  1307. include(pd.procoptions,po_syscall_sysvbase);
  1308. end
  1309. else if idtoken=_R12BASE then
  1310. begin
  1311. consume(_R12BASE);
  1312. include(pd.procoptions,po_syscall_r12base);
  1313. end
  1314. else
  1315. if syscall_convention='LEGACY' then
  1316. include(pd.procoptions,po_syscall_legacy)
  1317. else if syscall_convention='SYSV' then
  1318. include(pd.procoptions,po_syscall_sysv)
  1319. else if syscall_convention='BASESYSV' then
  1320. include(pd.procoptions,po_syscall_basesysv)
  1321. else if syscall_convention='SYSVBASE' then
  1322. include(pd.procoptions,po_syscall_sysvbase)
  1323. else if syscall_convention='R12BASE' then
  1324. include(pd.procoptions,po_syscall_r12base)
  1325. else
  1326. internalerror(2005010404);
  1327. if consume_sym(sym,symtable) then
  1328. begin
  1329. if (sym.typ=staticvarsym) and
  1330. (
  1331. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1332. is_32bitint(tabstractvarsym(sym).vardef)
  1333. ) then
  1334. begin
  1335. tprocdef(pd).libsym:=sym;
  1336. if po_syscall_legacy in tprocdef(pd).procoptions then
  1337. begin
  1338. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1339. paramanager.parseparaloc(vs,'A6');
  1340. pd.parast.insert(vs);
  1341. end
  1342. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1343. begin
  1344. { Nothing to be done for sysv here for now, but this might change }
  1345. end
  1346. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1347. begin
  1348. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1349. pd.parast.insert(vs);
  1350. end
  1351. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1352. begin
  1353. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1354. pd.parast.insert(vs);
  1355. end
  1356. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1357. begin
  1358. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1359. paramanager.parseparaloc(vs,'R12');
  1360. pd.parast.insert(vs);
  1361. end
  1362. else
  1363. internalerror(2005010501);
  1364. end
  1365. else
  1366. Message(parser_e_32bitint_or_pointer_variable_expected);
  1367. end;
  1368. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1369. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1370. tprocdef(pd).extnumber:=get_intconst;
  1371. end;
  1372. {$endif powerpc}
  1373. end;
  1374. procedure pd_external(pd:tabstractprocdef);
  1375. {
  1376. If import_dll=nil the procedure is assumed to be in another
  1377. object file. In that object file it should have the name to
  1378. which import_name is pointing to. Otherwise, the procedure is
  1379. assumed to be in the DLL to which import_dll is pointing to. In
  1380. that case either import_nr<>0 or import_name<>nil is true, so
  1381. the procedure is either imported by number or by name. (DM)
  1382. }
  1383. var
  1384. hs : string;
  1385. begin
  1386. if pd.typ<>procdef then
  1387. internalerror(2003042615);
  1388. with tprocdef(pd) do
  1389. begin
  1390. forwarddef:=false;
  1391. { forbid local external procedures }
  1392. if parast.symtablelevel>normal_function_level then
  1393. Message(parser_e_no_local_proc_external);
  1394. { If the procedure should be imported from a DLL, a constant string follows.
  1395. This isn't really correct, an contant string expression follows
  1396. so we check if an semicolon follows, else a string constant have to
  1397. follow (FK) }
  1398. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1399. begin
  1400. { Always add library prefix and suffix to create an uniform name }
  1401. hs:=get_stringconst;
  1402. if ExtractFileExt(hs)='' then
  1403. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1404. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1405. hs:=target_info.sharedlibprefix+hs;
  1406. import_dll:=stringdup(hs);
  1407. include(procoptions,po_has_importdll);
  1408. if (idtoken=_NAME) then
  1409. begin
  1410. consume(_NAME);
  1411. import_name:=stringdup(get_stringconst);
  1412. include(procoptions,po_has_importname);
  1413. if import_name^='' then
  1414. message(parser_e_empty_import_name);
  1415. end;
  1416. if (idtoken=_INDEX) then
  1417. begin
  1418. {After the word index follows the index number in the DLL.}
  1419. consume(_INDEX);
  1420. import_nr:=longint(get_intconst);
  1421. end;
  1422. { default is to used the realname of the procedure }
  1423. if (import_nr=0) and not assigned(import_name) then
  1424. begin
  1425. import_name:=stringdup(procsym.realname);
  1426. include(procoptions,po_has_importname);
  1427. end;
  1428. end
  1429. else
  1430. begin
  1431. if (idtoken=_NAME) then
  1432. begin
  1433. consume(_NAME);
  1434. import_name:=stringdup(get_stringconst);
  1435. include(procoptions,po_has_importname);
  1436. if import_name^='' then
  1437. message(parser_e_empty_import_name);
  1438. end;
  1439. end;
  1440. end;
  1441. end;
  1442. type
  1443. pd_handler=procedure(pd:tabstractprocdef);
  1444. proc_dir_rec=record
  1445. idtok : ttoken;
  1446. pd_flags : tpdflags;
  1447. handler : pd_handler;
  1448. pocall : tproccalloption;
  1449. pooption : tprocoptions;
  1450. mutexclpocall : tproccalloptions;
  1451. mutexclpotype : tproctypeoptions;
  1452. mutexclpo : tprocoptions;
  1453. end;
  1454. const
  1455. {Should contain the number of procedure directives we support.}
  1456. num_proc_directives=39;
  1457. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1458. (
  1459. (
  1460. idtok:_ABSTRACT;
  1461. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1462. handler : @pd_abstract;
  1463. pocall : pocall_none;
  1464. pooption : [po_abstractmethod];
  1465. mutexclpocall : [pocall_internproc];
  1466. mutexclpotype : [];
  1467. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1468. ),(
  1469. idtok:_ALIAS;
  1470. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1471. handler : @pd_alias;
  1472. pocall : pocall_none;
  1473. pooption : [];
  1474. mutexclpocall : [];
  1475. mutexclpotype : [];
  1476. mutexclpo : [po_external,po_inline]
  1477. ),(
  1478. idtok:_ASMNAME;
  1479. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1480. handler : @pd_asmname;
  1481. pocall : pocall_cdecl;
  1482. pooption : [po_external];
  1483. mutexclpocall : [pocall_internproc];
  1484. mutexclpotype : [];
  1485. mutexclpo : [po_external,po_inline]
  1486. ),(
  1487. idtok:_ASSEMBLER;
  1488. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1489. handler : nil;
  1490. pocall : pocall_none;
  1491. pooption : [po_assembler];
  1492. mutexclpocall : [];
  1493. mutexclpotype : [];
  1494. mutexclpo : [po_external]
  1495. ),(
  1496. idtok:_C; {same as cdecl for mode mac}
  1497. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1498. handler : nil;
  1499. pocall : pocall_cdecl;
  1500. pooption : [];
  1501. mutexclpocall : [];
  1502. mutexclpotype : [potype_constructor,potype_destructor];
  1503. mutexclpo : [po_assembler,po_external]
  1504. ),(
  1505. idtok:_CDECL;
  1506. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1507. handler : nil;
  1508. pocall : pocall_cdecl;
  1509. pooption : [];
  1510. mutexclpocall : [];
  1511. mutexclpotype : [potype_constructor,potype_destructor];
  1512. mutexclpo : [po_assembler,po_external]
  1513. ),(
  1514. idtok:_DISPID;
  1515. pd_flags : [pd_dispinterface];
  1516. handler : @pd_dispid;
  1517. pocall : pocall_none;
  1518. pooption : [po_dispid];
  1519. mutexclpocall : [pocall_internproc];
  1520. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1521. mutexclpo : [po_interrupt,po_external,po_inline]
  1522. ),(
  1523. idtok:_DYNAMIC;
  1524. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1525. handler : @pd_virtual;
  1526. pocall : pocall_none;
  1527. pooption : [po_virtualmethod];
  1528. mutexclpocall : [pocall_internproc];
  1529. mutexclpotype : [];
  1530. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1531. ),(
  1532. idtok:_EXPORT;
  1533. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1534. handler : @pd_export;
  1535. pocall : pocall_none;
  1536. pooption : [po_exports,po_global];
  1537. mutexclpocall : [pocall_internproc];
  1538. mutexclpotype : [potype_constructor,potype_destructor];
  1539. mutexclpo : [po_external,po_interrupt,po_inline]
  1540. ),(
  1541. idtok:_EXTERNAL;
  1542. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1543. handler : @pd_external;
  1544. pocall : pocall_none;
  1545. pooption : [po_external];
  1546. mutexclpocall : [pocall_internproc,pocall_syscall];
  1547. { allowed for external cpp classes }
  1548. mutexclpotype : [{potype_constructor,potype_destructor}];
  1549. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1550. ),(
  1551. idtok:_FAR;
  1552. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1553. handler : @pd_far;
  1554. pocall : pocall_none;
  1555. pooption : [];
  1556. mutexclpocall : [pocall_internproc];
  1557. mutexclpotype : [];
  1558. mutexclpo : [po_inline]
  1559. ),(
  1560. idtok:_FAR16;
  1561. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1562. handler : nil;
  1563. pocall : pocall_far16;
  1564. pooption : [];
  1565. mutexclpocall : [];
  1566. mutexclpotype : [];
  1567. mutexclpo : [po_external]
  1568. ),(
  1569. idtok:_FORWARD;
  1570. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1571. handler : @pd_forward;
  1572. pocall : pocall_none;
  1573. pooption : [];
  1574. mutexclpocall : [pocall_internproc];
  1575. mutexclpotype : [];
  1576. mutexclpo : [po_external,po_inline]
  1577. ),(
  1578. idtok:_OLDFPCCALL;
  1579. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1580. handler : nil;
  1581. pocall : pocall_oldfpccall;
  1582. pooption : [];
  1583. mutexclpocall : [];
  1584. mutexclpotype : [];
  1585. mutexclpo : []
  1586. ),(
  1587. idtok:_INLINE;
  1588. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1589. handler : @pd_inline;
  1590. pocall : pocall_none;
  1591. pooption : [po_inline];
  1592. mutexclpocall : [];
  1593. mutexclpotype : [potype_constructor,potype_destructor];
  1594. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1595. ),(
  1596. idtok:_INTERNCONST;
  1597. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf];
  1598. handler : @pd_internconst;
  1599. pocall : pocall_none;
  1600. pooption : [po_internconst];
  1601. mutexclpocall : [];
  1602. mutexclpotype : [potype_operator];
  1603. mutexclpo : []
  1604. ),(
  1605. idtok:_INTERNPROC;
  1606. pd_flags : [pd_interface,pd_notobject,pd_notobjintf];
  1607. handler : @pd_internproc;
  1608. pocall : pocall_internproc;
  1609. pooption : [];
  1610. mutexclpocall : [];
  1611. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1612. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1613. ),(
  1614. idtok:_INTERRUPT;
  1615. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1616. handler : @pd_interrupt;
  1617. pocall : pocall_oldfpccall;
  1618. pooption : [po_interrupt];
  1619. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1620. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1621. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1622. mutexclpo : [po_external,po_inline]
  1623. ),(
  1624. idtok:_IOCHECK;
  1625. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1626. handler : nil;
  1627. pocall : pocall_none;
  1628. pooption : [po_iocheck];
  1629. mutexclpocall : [pocall_internproc];
  1630. mutexclpotype : [];
  1631. mutexclpo : [po_external]
  1632. ),(
  1633. idtok:_LOCAL;
  1634. pd_flags : [pd_implemen,pd_body];
  1635. handler : nil;
  1636. pocall : pocall_none;
  1637. pooption : [po_kylixlocal];
  1638. mutexclpocall : [pocall_internproc,pocall_far16];
  1639. mutexclpotype : [];
  1640. mutexclpo : [po_external,po_exports]
  1641. ),(
  1642. idtok:_MESSAGE;
  1643. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1644. handler : @pd_message;
  1645. pocall : pocall_none;
  1646. pooption : []; { can be po_msgstr or po_msgint }
  1647. mutexclpocall : [pocall_internproc];
  1648. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1649. mutexclpo : [po_interrupt,po_external,po_inline]
  1650. ),(
  1651. idtok:_MWPASCAL;
  1652. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1653. handler : nil;
  1654. pocall : pocall_mwpascal;
  1655. pooption : [];
  1656. mutexclpocall : [];
  1657. mutexclpotype : [];
  1658. mutexclpo : []
  1659. ),(
  1660. idtok:_NEAR;
  1661. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1662. handler : @pd_near;
  1663. pocall : pocall_none;
  1664. pooption : [];
  1665. mutexclpocall : [pocall_internproc];
  1666. mutexclpotype : [];
  1667. mutexclpo : []
  1668. ),(
  1669. idtok:_NOSTACKFRAME;
  1670. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1671. handler : nil;
  1672. pocall : pocall_none;
  1673. pooption : [po_nostackframe];
  1674. mutexclpocall : [pocall_internproc];
  1675. mutexclpotype : [];
  1676. mutexclpo : []
  1677. ),(
  1678. idtok:_OVERLOAD;
  1679. pd_flags : [pd_implemen,pd_interface,pd_body];
  1680. handler : @pd_overload;
  1681. pocall : pocall_none;
  1682. pooption : [po_overload];
  1683. mutexclpocall : [pocall_internproc];
  1684. mutexclpotype : [];
  1685. mutexclpo : []
  1686. ),(
  1687. idtok:_OVERRIDE;
  1688. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1689. handler : @pd_override;
  1690. pocall : pocall_none;
  1691. pooption : [po_overridingmethod,po_virtualmethod];
  1692. mutexclpocall : [pocall_internproc];
  1693. mutexclpotype : [];
  1694. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_inline]
  1695. ),(
  1696. idtok:_PASCAL;
  1697. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1698. handler : nil;
  1699. pocall : pocall_pascal;
  1700. pooption : [];
  1701. mutexclpocall : [];
  1702. mutexclpotype : [potype_constructor,potype_destructor];
  1703. mutexclpo : [po_external]
  1704. ),(
  1705. idtok:_PUBLIC;
  1706. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1707. handler : @pd_public;
  1708. pocall : pocall_none;
  1709. pooption : [po_public,po_global];
  1710. mutexclpocall : [pocall_internproc];
  1711. mutexclpotype : [];
  1712. mutexclpo : [po_external,po_inline]
  1713. ),(
  1714. idtok:_REGISTER;
  1715. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1716. handler : nil;
  1717. pocall : pocall_register;
  1718. pooption : [];
  1719. mutexclpocall : [];
  1720. mutexclpotype : [potype_constructor,potype_destructor];
  1721. mutexclpo : [po_external]
  1722. ),(
  1723. idtok:_REINTRODUCE;
  1724. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1725. handler : @pd_reintroduce;
  1726. pocall : pocall_none;
  1727. pooption : [po_reintroduce];
  1728. mutexclpocall : [pocall_internproc];
  1729. mutexclpotype : [];
  1730. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1731. ),(
  1732. idtok:_SAFECALL;
  1733. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1734. handler : nil;
  1735. pocall : pocall_safecall;
  1736. pooption : [];
  1737. mutexclpocall : [];
  1738. mutexclpotype : [potype_constructor,potype_destructor];
  1739. mutexclpo : [po_external]
  1740. ),(
  1741. idtok:_SOFTFLOAT;
  1742. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1743. handler : nil;
  1744. pocall : pocall_softfloat;
  1745. pooption : [];
  1746. mutexclpocall : [];
  1747. mutexclpotype : [potype_constructor,potype_destructor];
  1748. { it's available with po_external because the libgcc floating point routines on the arm
  1749. uses this calling convention }
  1750. mutexclpo : []
  1751. ),(
  1752. idtok:_STATIC;
  1753. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1754. handler : @pd_static;
  1755. pocall : pocall_none;
  1756. pooption : [po_staticmethod];
  1757. mutexclpocall : [pocall_internproc];
  1758. mutexclpotype : [potype_constructor,potype_destructor];
  1759. mutexclpo : [po_external,po_interrupt,po_exports,po_inline]
  1760. ),(
  1761. idtok:_STDCALL;
  1762. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1763. handler : nil;
  1764. pocall : pocall_stdcall;
  1765. pooption : [];
  1766. mutexclpocall : [];
  1767. mutexclpotype : [potype_constructor,potype_destructor];
  1768. mutexclpo : [po_external]
  1769. ),(
  1770. idtok:_SYSCALL;
  1771. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  1772. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  1773. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  1774. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1775. handler : @pd_syscall;
  1776. pocall : pocall_syscall;
  1777. pooption : [];
  1778. mutexclpocall : [];
  1779. mutexclpotype : [potype_constructor,potype_destructor];
  1780. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1781. ),(
  1782. idtok:_VIRTUAL;
  1783. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1784. handler : @pd_virtual;
  1785. pocall : pocall_none;
  1786. pooption : [po_virtualmethod];
  1787. mutexclpocall : [pocall_internproc];
  1788. mutexclpotype : [];
  1789. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1790. ),(
  1791. idtok:_CPPDECL;
  1792. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1793. handler : nil;
  1794. pocall : pocall_cppdecl;
  1795. pooption : [];
  1796. mutexclpocall : [];
  1797. mutexclpotype : [potype_constructor,potype_destructor];
  1798. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1799. ),(
  1800. idtok:_VARARGS;
  1801. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1802. handler : nil;
  1803. pocall : pocall_none;
  1804. pooption : [po_varargs];
  1805. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1806. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  1807. mutexclpotype : [];
  1808. mutexclpo : [po_assembler,po_interrupt,po_inline]
  1809. ),(
  1810. idtok:_COMPILERPROC;
  1811. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1812. handler : nil;
  1813. pocall : pocall_none;
  1814. pooption : [po_compilerproc];
  1815. mutexclpocall : [];
  1816. mutexclpotype : [potype_constructor,potype_destructor];
  1817. mutexclpo : [po_interrupt]
  1818. )
  1819. );
  1820. function check_proc_directive(isprocvar:boolean):boolean;
  1821. var
  1822. i : longint;
  1823. begin
  1824. result:=false;
  1825. for i:=1 to num_proc_directives do
  1826. if proc_direcdata[i].idtok=idtoken then
  1827. begin
  1828. if ((not isprocvar) or
  1829. (pd_procvar in proc_direcdata[i].pd_flags)) and
  1830. { don't eat a public directive in classes }
  1831. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  1832. result:=true;
  1833. exit;
  1834. end;
  1835. end;
  1836. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1837. {
  1838. Parse the procedure directive, returns true if a correct directive is found
  1839. }
  1840. var
  1841. p : longint;
  1842. found : boolean;
  1843. name : TIDString;
  1844. begin
  1845. parse_proc_direc:=false;
  1846. name:=tokeninfo^[idtoken].str;
  1847. found:=false;
  1848. { Hint directive? Then exit immediatly }
  1849. if (m_hintdirective in current_settings.modeswitches) then
  1850. begin
  1851. case idtoken of
  1852. _LIBRARY,
  1853. _PLATFORM,
  1854. _UNIMPLEMENTED,
  1855. _DEPRECATED :
  1856. exit;
  1857. end;
  1858. end;
  1859. { C directive is MAC only, because it breaks too much existing code
  1860. on other platforms (PFV) }
  1861. if (idtoken=_C) and
  1862. not(m_mac in current_settings.modeswitches) then
  1863. exit;
  1864. { retrieve data for directive if found }
  1865. for p:=1 to num_proc_directives do
  1866. if proc_direcdata[p].idtok=idtoken then
  1867. begin
  1868. found:=true;
  1869. break;
  1870. end;
  1871. { Check if the procedure directive is known }
  1872. if not found then
  1873. begin
  1874. { parsing a procvar type the name can be any
  1875. next variable !! }
  1876. if ((pdflags * [pd_procvar,pd_object])=[]) and
  1877. not(idtoken=_PROPERTY) then
  1878. Message1(parser_w_unknown_proc_directive_ignored,name);
  1879. exit;
  1880. end;
  1881. { static needs a special treatment }
  1882. if (idtoken=_STATIC) and not (cs_static_keyword in current_settings.moduleswitches) then
  1883. exit;
  1884. { check if method and directive not for object, like public.
  1885. This needs to be checked also for procvars }
  1886. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1887. (symtablestack.top.symtabletype=ObjectSymtable) and
  1888. { directive allowed for cpp classes? }
  1889. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  1890. exit;
  1891. { Conflicts between directives ? }
  1892. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1893. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1894. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1895. begin
  1896. Message1(parser_e_proc_dir_conflict,name);
  1897. exit;
  1898. end;
  1899. { set calling convention }
  1900. if proc_direcdata[p].pocall<>pocall_none then
  1901. begin
  1902. if (po_hascallingconvention in pd.procoptions) then
  1903. begin
  1904. Message2(parser_w_proc_overriding_calling,
  1905. proccalloptionStr[pd.proccalloption],
  1906. proccalloptionStr[proc_direcdata[p].pocall]);
  1907. end;
  1908. { check if the target processor supports this calling convention }
  1909. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1910. begin
  1911. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1912. { recover }
  1913. proc_direcdata[p].pocall:=pocall_stdcall;
  1914. end;
  1915. pd.proccalloption:=proc_direcdata[p].pocall;
  1916. include(pd.procoptions,po_hascallingconvention);
  1917. end;
  1918. if pd.typ=procdef then
  1919. begin
  1920. { Check if the directive is only for objects }
  1921. if (pd_object in proc_direcdata[p].pd_flags) and
  1922. not assigned(tprocdef(pd)._class) then
  1923. exit;
  1924. { check if method and directive not for interface }
  1925. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1926. is_interface(tprocdef(pd)._class) then
  1927. exit;
  1928. { check if method and directive not for interface }
  1929. if is_dispinterface(tprocdef(pd)._class) and
  1930. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  1931. exit;
  1932. end;
  1933. { consume directive, and turn flag on }
  1934. consume(token);
  1935. parse_proc_direc:=true;
  1936. { Check the pd_flags if the directive should be allowed }
  1937. if (pd_interface in pdflags) and
  1938. not(pd_interface in proc_direcdata[p].pd_flags) then
  1939. begin
  1940. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1941. exit;
  1942. end;
  1943. if (pd_implemen in pdflags) and
  1944. not(pd_implemen in proc_direcdata[p].pd_flags) then
  1945. begin
  1946. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1947. exit;
  1948. end;
  1949. if (pd_procvar in pdflags) and
  1950. not(pd_procvar in proc_direcdata[p].pd_flags) then
  1951. begin
  1952. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1953. exit;
  1954. end;
  1955. { Return the new pd_flags }
  1956. if not(pd_body in proc_direcdata[p].pd_flags) then
  1957. exclude(pdflags,pd_body);
  1958. { Add the correct flag }
  1959. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  1960. { Call the handler }
  1961. if pointer(proc_direcdata[p].handler)<>nil then
  1962. proc_direcdata[p].handler(pd);
  1963. end;
  1964. function proc_get_importname(pd:tprocdef):string;
  1965. function maybe_cprefix(const s:string):string;
  1966. begin
  1967. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1968. result:=s
  1969. else
  1970. result:=target_info.Cprefix+s;
  1971. end;
  1972. begin
  1973. result:='';
  1974. if not(po_external in pd.procoptions) then
  1975. internalerror(200412151);
  1976. { external name or number is specified }
  1977. if assigned(pd.import_name) or (pd.import_nr<>0) then
  1978. begin
  1979. if assigned(pd.import_dll) then
  1980. begin
  1981. { If we are not using direct dll linking under win32 then imports
  1982. need to use the normal name since two functions can refer to the
  1983. same DLL function. This is also needed for compatability
  1984. with Delphi and TP7 }
  1985. (*
  1986. case target_info.system of
  1987. system_i386_emx,
  1988. system_i386_os2 :
  1989. begin
  1990. { keep normal mangledname }
  1991. if not (Assigned (PD.Import_Name)) then
  1992. Result := PD.MangledName;
  1993. end;
  1994. else
  1995. *)
  1996. if assigned(pd.import_name) then
  1997. begin
  1998. if target_info.system in (system_all_windows +
  1999. [system_i386_emx, system_i386_os2]) then
  2000. { cprefix is not used in DLL imports under Windows or OS/2 }
  2001. result:=pd.import_name^
  2002. else
  2003. result:=maybe_cprefix(pd.import_name^);
  2004. end
  2005. else
  2006. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2007. end
  2008. else
  2009. result:=maybe_cprefix(pd.import_name^);
  2010. end
  2011. else
  2012. begin
  2013. { Default names when importing variables }
  2014. case pd.proccalloption of
  2015. pocall_cdecl :
  2016. begin
  2017. if assigned(pd._class) then
  2018. result:=target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname
  2019. else
  2020. result:=target_info.Cprefix+pd.procsym.realname;
  2021. end;
  2022. pocall_cppdecl :
  2023. begin
  2024. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2025. end;
  2026. else
  2027. begin
  2028. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2029. if (m_mac in current_settings.modeswitches) then
  2030. result:=tprocdef(pd).procsym.realname;
  2031. end;
  2032. end;
  2033. end;
  2034. end;
  2035. procedure proc_set_mangledname(pd:tprocdef);
  2036. var
  2037. s : string;
  2038. begin
  2039. { When the mangledname is already set we aren't allowed to change
  2040. it because it can already be used somewhere (PFV) }
  2041. if not(po_has_mangledname in pd.procoptions) then
  2042. begin
  2043. if (po_external in pd.procoptions) then
  2044. begin
  2045. { External Procedures are only allowed to change the mangledname
  2046. in their first declaration }
  2047. if (pd.forwarddef or (not pd.hasforward)) then
  2048. begin
  2049. s:=proc_get_importname(pd);
  2050. if s<>'' then
  2051. begin
  2052. { Replace ? and @ in import name }
  2053. Replace(s,'?','$_q_$');
  2054. Replace(s,'@','$_a_$');
  2055. pd.setmangledname(s);
  2056. end;
  2057. end;
  2058. end
  2059. else
  2060. { Normal procedures }
  2061. begin
  2062. if (po_compilerproc in pd.procoptions) then
  2063. begin
  2064. pd.setmangledname(lower(pd.procsym.name));
  2065. end;
  2066. end;
  2067. end;
  2068. { Public/exported alias names }
  2069. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2070. not(po_has_public_name in pd.procoptions) then
  2071. begin
  2072. case pd.proccalloption of
  2073. pocall_cdecl :
  2074. begin
  2075. if assigned(pd._class) then
  2076. pd.aliasnames.insert(target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname)
  2077. else
  2078. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2079. end;
  2080. pocall_cppdecl :
  2081. begin
  2082. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2083. end;
  2084. end;
  2085. { prevent adding the alias a second time }
  2086. include(pd.procoptions,po_has_public_name);
  2087. end;
  2088. end;
  2089. procedure handle_calling_convention(pd:tabstractprocdef);
  2090. begin
  2091. { set the default calling convention if none provided }
  2092. if not(po_hascallingconvention in pd.procoptions) then
  2093. pd.proccalloption:=current_settings.defproccall
  2094. else
  2095. begin
  2096. if pd.proccalloption=pocall_none then
  2097. internalerror(200309081);
  2098. end;
  2099. { handle proccall specific settings }
  2100. case pd.proccalloption of
  2101. pocall_cdecl,
  2102. pocall_cppdecl :
  2103. begin
  2104. { check C cdecl para types }
  2105. check_c_para(pd);
  2106. end;
  2107. pocall_far16 :
  2108. begin
  2109. { Temporary stub, must be rewritten to support OS/2 far16 }
  2110. Message1(parser_w_proc_directive_ignored,'FAR16');
  2111. end;
  2112. end;
  2113. { Inlining is enabled and supported? }
  2114. if (po_inline in pd.procoptions) and
  2115. not(cs_do_inline in current_settings.localswitches) then
  2116. begin
  2117. { Give an error if inline is not supported by the compiler mode,
  2118. otherwise only give a warning that this procedure will not be inlined }
  2119. if not(m_default_inline in current_settings.modeswitches) then
  2120. Message(parser_e_proc_inline_not_supported)
  2121. else
  2122. Message(parser_w_inlining_disabled);
  2123. exclude(pd.procoptions,po_inline);
  2124. end;
  2125. { For varargs directive also cdecl and external must be defined }
  2126. if (po_varargs in pd.procoptions) then
  2127. begin
  2128. { check first for external in the interface, if available there
  2129. then the cdecl must also be there since there is no implementation
  2130. available to contain it }
  2131. if parse_only then
  2132. begin
  2133. { if external is available, then cdecl must also be available,
  2134. procvars don't need external }
  2135. if not((po_external in pd.procoptions) or
  2136. (pd.typ=procvardef)) and
  2137. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2138. Message(parser_e_varargs_need_cdecl_and_external);
  2139. end
  2140. else
  2141. begin
  2142. { both must be defined now }
  2143. if not((po_external in pd.procoptions) or
  2144. (pd.typ=procvardef)) or
  2145. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2146. Message(parser_e_varargs_need_cdecl_and_external);
  2147. end;
  2148. end;
  2149. { insert hidden high parameters }
  2150. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2151. { insert hidden self parameter }
  2152. insert_self_and_vmt_para(pd);
  2153. { insert funcret parameter if required }
  2154. insert_funcret_para(pd);
  2155. { Make var parameters regable, this must be done after the calling
  2156. convention is set. }
  2157. { this must be done before parentfp is insert, because getting all cases
  2158. where parentfp must be in a memory location isn't catched properly so
  2159. we put parentfp never in a register }
  2160. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2161. { insert parentfp parameter if required }
  2162. insert_parentfp_para(pd);
  2163. { Calculate parameter tlist }
  2164. pd.calcparas;
  2165. end;
  2166. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2167. {
  2168. Parse the procedure directives. It does not matter if procedure directives
  2169. are written using ;procdir; or ['procdir'] syntax.
  2170. }
  2171. var
  2172. res : boolean;
  2173. begin
  2174. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2175. begin
  2176. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  2177. include(pd.procoptions,po_public);
  2178. include(pd.procoptions,po_has_public_name);
  2179. include(pd.procoptions,po_global);
  2180. end;
  2181. while token in [_ID,_LECKKLAMMER] do
  2182. begin
  2183. if try_to_consume(_LECKKLAMMER) then
  2184. begin
  2185. repeat
  2186. parse_proc_direc(pd,pdflags);
  2187. until not try_to_consume(_COMMA);
  2188. consume(_RECKKLAMMER);
  2189. { we always expect at least '[];' }
  2190. res:=true;
  2191. end
  2192. else
  2193. begin
  2194. res:=parse_proc_direc(pd,pdflags);
  2195. end;
  2196. { A procedure directive normally followed by a semicolon, but in
  2197. a const section or reading a type we should stop when _EQUAL is found,
  2198. because a constant/default value follows }
  2199. if res then
  2200. begin
  2201. if (block_type in [bt_const,bt_type]) and
  2202. (token=_EQUAL) then
  2203. break;
  2204. { support procedure proc;stdcall export; }
  2205. if not(check_proc_directive((pd.typ=procvardef))) then
  2206. begin
  2207. { support "record p : procedure stdcall end;" and
  2208. "var p : procedure stdcall = nil;" }
  2209. if (pd_procvar in pdflags) and
  2210. (token in [_END,_RKLAMMER,_EQUAL]) then
  2211. break
  2212. else
  2213. consume(_SEMICOLON);
  2214. end;
  2215. end
  2216. else
  2217. break;
  2218. end;
  2219. end;
  2220. procedure parse_var_proc_directives(sym:tsym);
  2221. var
  2222. pdflags : tpdflags;
  2223. pd : tabstractprocdef;
  2224. begin
  2225. pdflags:=[pd_procvar];
  2226. pd:=nil;
  2227. case sym.typ of
  2228. fieldvarsym,
  2229. staticvarsym,
  2230. localvarsym,
  2231. paravarsym :
  2232. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2233. typesym :
  2234. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2235. else
  2236. internalerror(2003042617);
  2237. end;
  2238. if pd.typ<>procvardef then
  2239. internalerror(2003042618);
  2240. { names should never be used anyway }
  2241. parse_proc_directives(pd,pdflags);
  2242. end;
  2243. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2244. var
  2245. pdflags : tpdflags;
  2246. begin
  2247. pdflags:=[pd_object];
  2248. parse_proc_directives(pd,pdflags);
  2249. end;
  2250. function proc_add_definition(var currpd:tprocdef):boolean;
  2251. {
  2252. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2253. forwarddef is found and reused it returns true
  2254. }
  2255. var
  2256. fwpd : tprocdef;
  2257. currparasym,
  2258. fwparasym : tsym;
  2259. currparacnt,
  2260. fwparacnt,
  2261. curridx,
  2262. fwidx,
  2263. i : longint;
  2264. po_comp : tprocoptions;
  2265. paracompopt: tcompare_paras_options;
  2266. forwardfound : boolean;
  2267. begin
  2268. forwardfound:=false;
  2269. { check overloaded functions if the same function already exists }
  2270. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2271. begin
  2272. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2273. { Skip overloaded definitions that are declared in other units }
  2274. if fwpd.procsym<>currpd.procsym then
  2275. continue;
  2276. { check the parameters, for delphi/tp it is possible to
  2277. leave the parameters away in the implementation (forwarddef=false).
  2278. But for an overload declared function this is not allowed }
  2279. if { check if empty implementation arguments match is allowed }
  2280. (
  2281. not(m_repeat_forward in current_settings.modeswitches) and
  2282. not(currpd.forwarddef) and
  2283. is_bareprocdef(currpd) and
  2284. not(po_overload in fwpd.procoptions)
  2285. ) or
  2286. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2287. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2288. (
  2289. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden])>=te_equal) and
  2290. { for operators equal_paras is not enough !! }
  2291. ((currpd.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  2292. { be careful here, equal_defs doesn't take care of unique }
  2293. (fwpd.returndef=currpd.returndef) or
  2294. (equal_defs(fwpd.returndef,currpd.returndef) and
  2295. not(df_unique in fwpd.returndef.defoptions) and not(df_unique in currpd.returndef.defoptions)
  2296. )
  2297. )
  2298. ) then
  2299. begin
  2300. { Check if we've found the forwarddef, if found then
  2301. we need to update the forward def with the current
  2302. implementation settings }
  2303. if fwpd.forwarddef then
  2304. begin
  2305. forwardfound:=true;
  2306. if not(m_repeat_forward in current_settings.modeswitches) and
  2307. (fwpd.proccalloption<>currpd.proccalloption) then
  2308. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue]
  2309. else
  2310. paracompopt:=[cpo_comparedefaultvalue];
  2311. { Check calling convention }
  2312. if (fwpd.proccalloption<>currpd.proccalloption) then
  2313. begin
  2314. { In delphi it is possible to specify the calling
  2315. convention in the interface or implementation if
  2316. there was no convention specified in the other
  2317. part }
  2318. if (m_delphi in current_settings.modeswitches) then
  2319. begin
  2320. if not(po_hascallingconvention in currpd.procoptions) then
  2321. currpd.proccalloption:=fwpd.proccalloption
  2322. else
  2323. if not(po_hascallingconvention in fwpd.procoptions) then
  2324. fwpd.proccalloption:=currpd.proccalloption
  2325. else
  2326. begin
  2327. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2328. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2329. { restore interface settings }
  2330. currpd.proccalloption:=fwpd.proccalloption;
  2331. end;
  2332. end
  2333. else
  2334. begin
  2335. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2336. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2337. { restore interface settings }
  2338. currpd.proccalloption:=fwpd.proccalloption;
  2339. end;
  2340. end;
  2341. { Check if the procedure type and return type are correct,
  2342. also the parameters must match also with the type }
  2343. if ((m_repeat_forward in current_settings.modeswitches) or
  2344. not is_bareprocdef(currpd)) and
  2345. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<te_equal) or
  2346. (not equal_defs(fwpd.returndef,currpd.returndef))) then
  2347. begin
  2348. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2349. fwpd.fullprocname(false));
  2350. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2351. break;
  2352. end;
  2353. { Check if both are declared forward }
  2354. if fwpd.forwarddef and currpd.forwarddef then
  2355. begin
  2356. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2357. currpd.fullprocname(false));
  2358. end;
  2359. { internconst or internproc only need to be defined once }
  2360. if (fwpd.proccalloption=pocall_internproc) then
  2361. currpd.proccalloption:=fwpd.proccalloption
  2362. else
  2363. if (currpd.proccalloption=pocall_internproc) then
  2364. fwpd.proccalloption:=currpd.proccalloption;
  2365. { Check procedure options, Delphi requires that class is
  2366. repeated in the implementation for class methods }
  2367. if (m_fpc in current_settings.modeswitches) then
  2368. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2369. else
  2370. po_comp:=[po_classmethod,po_methodpointer];
  2371. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2372. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2373. begin
  2374. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2375. fwpd.fullprocname(false));
  2376. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2377. { This error is non-fatal, we can recover }
  2378. end;
  2379. { Forward declaration is external? }
  2380. if (po_external in fwpd.procoptions) then
  2381. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2382. { Check parameters }
  2383. if (m_repeat_forward in current_settings.modeswitches) or
  2384. (currpd.minparacount>0) then
  2385. begin
  2386. { If mangled names are equal then they have the same amount of arguments }
  2387. { We can check the names of the arguments }
  2388. { both symtables are in the same order from left to right }
  2389. curridx:=0;
  2390. fwidx:=0;
  2391. currparacnt:=currpd.parast.SymList.Count;
  2392. fwparacnt:=fwpd.parast.SymList.Count;
  2393. repeat
  2394. { skip default parameter constsyms }
  2395. while (curridx<currparacnt) and
  2396. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2397. inc(curridx);
  2398. while (fwidx<fwparacnt) and
  2399. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2400. inc(fwidx);
  2401. { stop when one of the two lists is at the end }
  2402. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2403. break;
  2404. { compare names of parameters, ignore implictly
  2405. renamed parameters }
  2406. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2407. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2408. if not(sp_implicitrename in currparasym.symoptions) and
  2409. not(sp_implicitrename in fwparasym.symoptions) then
  2410. begin
  2411. if (currparasym.name<>fwparasym.name) then
  2412. begin
  2413. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2414. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2415. break;
  2416. end;
  2417. end;
  2418. { next parameter }
  2419. inc(curridx);
  2420. inc(fwidx);
  2421. until false;
  2422. end;
  2423. { Everything is checked, now we can update the forward declaration
  2424. with the new data from the implementation }
  2425. fwpd.forwarddef:=currpd.forwarddef;
  2426. fwpd.hasforward:=true;
  2427. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2428. { marked as local but exported from unit? }
  2429. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2430. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2431. if fwpd.extnumber=$ffff then
  2432. fwpd.extnumber:=currpd.extnumber;
  2433. while not currpd.aliasnames.empty do
  2434. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2435. { update fileinfo so position references the implementation,
  2436. also update funcretsym if it is already generated }
  2437. fwpd.fileinfo:=currpd.fileinfo;
  2438. if assigned(fwpd.funcretsym) then
  2439. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2440. { import names }
  2441. if assigned(currpd.import_dll) then
  2442. begin
  2443. stringdispose(fwpd.import_dll);
  2444. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2445. end;
  2446. if assigned(currpd.import_name) then
  2447. begin
  2448. stringdispose(fwpd.import_name);
  2449. fwpd.import_name:=stringdup(currpd.import_name^);
  2450. end;
  2451. fwpd.import_nr:=currpd.import_nr;
  2452. { for compilerproc defines we need to rename and update the
  2453. symbolname to lowercase }
  2454. if (po_compilerproc in fwpd.procoptions) then
  2455. begin
  2456. { rename to lowercase so users can't access it }
  2457. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2458. { the mangeled name is already changed by the pd_compilerproc }
  2459. { handler. It must be done immediately because if we have a }
  2460. { call to a compilerproc before it's implementation is }
  2461. { encountered, it must already use the new mangled name (JM) }
  2462. end;
  2463. { Release current procdef }
  2464. currpd.owner.deletedef(currpd);
  2465. currpd:=fwpd;
  2466. end
  2467. else
  2468. begin
  2469. { abstract methods aren't forward defined, but this }
  2470. { needs another error message }
  2471. if (po_abstractmethod in fwpd.procoptions) then
  2472. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2473. else
  2474. begin
  2475. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2476. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2477. end;
  2478. end;
  2479. { we found one proc with the same arguments, there are no others
  2480. so we can stop }
  2481. break;
  2482. end;
  2483. { check for allowing overload directive }
  2484. if not(m_fpc in current_settings.modeswitches) then
  2485. begin
  2486. { overload directive turns on overloading }
  2487. if ((po_overload in currpd.procoptions) or
  2488. (po_overload in fwpd.procoptions)) then
  2489. begin
  2490. { check if all procs have overloading, but not if the proc is a method or
  2491. already declared forward, then the check is already done }
  2492. if not(fwpd.hasforward or
  2493. assigned(currpd._class) or
  2494. (currpd.forwarddef<>fwpd.forwarddef) or
  2495. ((po_overload in currpd.procoptions) and
  2496. (po_overload in fwpd.procoptions))) then
  2497. begin
  2498. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2499. break;
  2500. end;
  2501. end
  2502. else
  2503. begin
  2504. if not(fwpd.forwarddef) then
  2505. begin
  2506. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off);
  2507. break;
  2508. end;
  2509. end;
  2510. end; { equal arguments }
  2511. end;
  2512. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2513. list }
  2514. if not forwardfound then
  2515. begin
  2516. { can happen in Delphi mode }
  2517. if (currpd.proctypeoption = potype_function) and
  2518. is_void(currpd.returndef) then
  2519. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2520. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2521. end;
  2522. proc_add_definition:=forwardfound;
  2523. end;
  2524. end.