pdecsub.pas 103 KB

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