pdecsub.pas 97 KB

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