pdecsub.pas 97 KB

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