pdecsub.pas 129 KB

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