pdecsub.pas 101 KB

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