pdecsub.pas 97 KB

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