pdecsub.pas 101 KB

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