pdecsub.pas 100 KB

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