pdecsub.pas 130 KB

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