pdecsub.pas 123 KB

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