pdecsub.pas 99 KB

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