pdecsub.pas 125 KB

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