pdecsub.pas 126 KB

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