pdecsub.pas 106 KB

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