pdecsub.pas 124 KB

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