pdecsub.pas 104 KB

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