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