pdecsub.pas 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296
  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 (that
  1140. contain operators) }
  1141. include(pd.procoptions,po_overload);
  1142. pd.procsym.owner.includeoption(sto_has_operator);
  1143. if pd.parast.symtablelevel>normal_function_level then
  1144. Message(parser_e_no_local_operator);
  1145. if isclassmethod then
  1146. include(pd.procoptions,po_classmethod);
  1147. if token<>_ID then
  1148. begin
  1149. if not(m_result in current_settings.modeswitches) then
  1150. consume(_ID);
  1151. end
  1152. else
  1153. begin
  1154. pd.resultname:=stringdup(orgpattern);
  1155. consume(_ID);
  1156. end;
  1157. if not try_to_consume(_COLON) then
  1158. begin
  1159. consume(_COLON);
  1160. pd.returndef:=generrordef;
  1161. consume_all_until(_SEMICOLON);
  1162. end
  1163. else
  1164. begin
  1165. read_returndef(pd);
  1166. { check that class operators have either return type of structure or }
  1167. { at least one argument of that type }
  1168. if (po_classmethod in pd.procoptions) and
  1169. (pd.returndef <> pd.struct) then
  1170. begin
  1171. found:=false;
  1172. for i := 0 to pd.parast.SymList.Count - 1 do
  1173. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1174. begin
  1175. found:=true;
  1176. break;
  1177. end;
  1178. if not found then
  1179. if assigned(pd.struct) then
  1180. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName)
  1181. else
  1182. MessagePos(pd.fileinfo,type_e_type_id_expected);
  1183. end;
  1184. if (optoken in [_EQ,_NE,_GT,_LT,_GTE,_LTE,_OP_IN]) and
  1185. ((pd.returndef.typ<>orddef) or
  1186. (torddef(pd.returndef).ordtype<>pasbool8)) then
  1187. Message(parser_e_comparative_operator_return_boolean);
  1188. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1189. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1190. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1191. message(parser_e_no_such_assignment)
  1192. else if not isoperatoracceptable(pd,optoken) then
  1193. Message(parser_e_overload_impossible);
  1194. end;
  1195. end
  1196. else
  1197. begin
  1198. { recover }
  1199. try_to_consume(_ID);
  1200. consume(_COLON);
  1201. consume_all_until(_SEMICOLON);
  1202. end;
  1203. end;
  1204. end;
  1205. { file types can't be function results }
  1206. if assigned(pd) and
  1207. (pd.returndef.typ=filedef) then
  1208. message(parser_e_illegal_function_result);
  1209. { support procedure proc stdcall export; }
  1210. if not(check_proc_directive(false)) then
  1211. begin
  1212. if (token=_COLON) then
  1213. begin
  1214. message(parser_e_field_not_allowed_here);
  1215. consume_all_until(_SEMICOLON);
  1216. end;
  1217. consume(_SEMICOLON);
  1218. end;
  1219. result:=pd;
  1220. if locationstr<>'' then
  1221. begin
  1222. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1223. { I guess this needs a new message... (KB) }
  1224. message(parser_e_illegal_explicit_paraloc);
  1225. end;
  1226. end;
  1227. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  1228. var
  1229. oldparse_only: boolean;
  1230. begin
  1231. oldparse_only:=parse_only;
  1232. parse_only:=true;
  1233. result:=parse_proc_dec(is_classdef,astruct);
  1234. { this is for error recovery as well as forward }
  1235. { interface mappings, i.e. mapping to a method }
  1236. { which isn't declared yet }
  1237. if assigned(result) then
  1238. begin
  1239. parse_record_proc_directives(result);
  1240. { since records have no inheritance, don't allow non-static
  1241. class methods. Selphi does the same. }
  1242. if (result.proctypeoption<>potype_operator) and
  1243. is_classdef and
  1244. not (po_staticmethod in result.procoptions) then
  1245. MessagePos(result.fileinfo, parser_e_class_methods_only_static_in_records);
  1246. // we can't add hidden params here because record is not yet defined
  1247. // and therefore record size which has influence on paramter passing rules may change too
  1248. // look at record_dec to see where calling conventions are applied (issue #0021044)
  1249. handle_calling_convention(result,[hcc_check]);
  1250. { add definition to procsym }
  1251. proc_add_definition(result);
  1252. end;
  1253. maybe_parse_hint_directives(result);
  1254. parse_only:=oldparse_only;
  1255. end;
  1256. procedure insert_record_hidden_paras(astruct: trecorddef);
  1257. var
  1258. pd: tdef;
  1259. i: longint;
  1260. oldpos : tfileposinfo;
  1261. oldparse_only: boolean;
  1262. begin
  1263. // handle calling conventions of record methods
  1264. oldpos:=current_filepos;
  1265. oldparse_only:=parse_only;
  1266. parse_only:=true;
  1267. { don't keep track of procdefs in a separate list, because the
  1268. compiler may add additional procdefs (e.g. property wrappers for
  1269. the jvm backend) }
  1270. for i := 0 to astruct.symtable.deflist.count - 1 do
  1271. begin
  1272. pd:=tdef(astruct.symtable.deflist[i]);
  1273. if pd.typ<>procdef then
  1274. continue;
  1275. current_filepos:=tprocdef(pd).fileinfo;
  1276. handle_calling_convention(tprocdef(pd),[hcc_insert_hidden_paras]);
  1277. end;
  1278. parse_only:=oldparse_only;
  1279. current_filepos:=oldpos;
  1280. end;
  1281. {****************************************************************************
  1282. Procedure directive handlers
  1283. ****************************************************************************}
  1284. procedure pd_far(pd:tabstractprocdef);
  1285. begin
  1286. Message1(parser_w_proc_directive_ignored,'FAR');
  1287. end;
  1288. procedure pd_near(pd:tabstractprocdef);
  1289. begin
  1290. Message1(parser_w_proc_directive_ignored,'NEAR');
  1291. end;
  1292. procedure pd_export(pd:tabstractprocdef);
  1293. begin
  1294. if pd.typ<>procdef then
  1295. internalerror(200304264);
  1296. if assigned(tprocdef(pd).struct) then
  1297. Message(parser_e_methods_dont_be_export);
  1298. if pd.parast.symtablelevel>normal_function_level then
  1299. Message(parser_e_dont_nest_export);
  1300. end;
  1301. procedure pd_forward(pd:tabstractprocdef);
  1302. begin
  1303. if pd.typ<>procdef then
  1304. internalerror(200304265);
  1305. tprocdef(pd).forwarddef:=true;
  1306. end;
  1307. procedure pd_alias(pd:tabstractprocdef);
  1308. begin
  1309. if pd.typ<>procdef then
  1310. internalerror(200304266);
  1311. consume(_COLON);
  1312. tprocdef(pd).aliasnames.insert(get_stringconst);
  1313. include(pd.procoptions,po_has_public_name);
  1314. end;
  1315. procedure pd_public(pd:tabstractprocdef);
  1316. begin
  1317. if pd.typ<>procdef then
  1318. internalerror(200304266);
  1319. if try_to_consume(_NAME) then
  1320. begin
  1321. tprocdef(pd).aliasnames.insert(get_stringconst);
  1322. include(pd.procoptions,po_has_public_name);
  1323. end;
  1324. end;
  1325. procedure pd_asmname(pd:tabstractprocdef);
  1326. begin
  1327. if pd.typ<>procdef then
  1328. internalerror(200304267);
  1329. if token=_CCHAR then
  1330. begin
  1331. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1332. consume(_CCHAR)
  1333. end
  1334. else
  1335. begin
  1336. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1337. consume(_CSTRING);
  1338. end;
  1339. { we don't need anything else }
  1340. tprocdef(pd).forwarddef:=false;
  1341. end;
  1342. procedure pd_internconst(pd:tabstractprocdef);
  1343. var v:Tconstexprint;
  1344. begin
  1345. if pd.typ<>procdef then
  1346. internalerror(200304268);
  1347. consume(_COLON);
  1348. v:=get_intconst;
  1349. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1350. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1351. else
  1352. Tprocdef(pd).extnumber:=longint(v.svalue);
  1353. end;
  1354. procedure pd_internproc(pd:tabstractprocdef);
  1355. var v:Tconstexprint;
  1356. begin
  1357. if pd.typ<>procdef then
  1358. internalerror(200304268);
  1359. consume(_COLON);
  1360. v:=get_intconst;
  1361. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1362. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1363. else
  1364. Tprocdef(pd).extnumber:=longint(v.svalue);
  1365. { the proc is defined }
  1366. tprocdef(pd).forwarddef:=false;
  1367. end;
  1368. procedure pd_interrupt(pd:tabstractprocdef);
  1369. begin
  1370. if pd.parast.symtablelevel>normal_function_level then
  1371. Message(parser_e_dont_nest_interrupt);
  1372. end;
  1373. procedure pd_abstract(pd:tabstractprocdef);
  1374. begin
  1375. if pd.typ<>procdef then
  1376. internalerror(200304269);
  1377. if is_objectpascal_helper(tprocdef(pd).struct) then
  1378. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1379. if assigned(tprocdef(pd).struct) and
  1380. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1381. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1382. else if (po_virtualmethod in pd.procoptions) then
  1383. begin
  1384. include(pd.procoptions,po_abstractmethod);
  1385. { one more abstract method }
  1386. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1387. end
  1388. else
  1389. Message(parser_e_only_virtual_methods_abstract);
  1390. { the method is defined }
  1391. tprocdef(pd).forwarddef:=false;
  1392. end;
  1393. procedure pd_final(pd:tabstractprocdef);
  1394. begin
  1395. if pd.typ<>procdef then
  1396. internalerror(200910170);
  1397. if is_objectpascal_helper(tprocdef(pd).struct) and
  1398. (m_objfpc in current_settings.modeswitches) then
  1399. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1400. if (po_virtualmethod in pd.procoptions) or
  1401. (is_javaclass(tprocdef(pd).struct) and
  1402. (po_classmethod in pd.procoptions)) then
  1403. include(pd.procoptions,po_finalmethod)
  1404. else
  1405. Message(parser_e_only_virtual_methods_final);
  1406. end;
  1407. procedure pd_enumerator(pd:tabstractprocdef);
  1408. begin
  1409. if pd.typ<>procdef then
  1410. internalerror(200910250);
  1411. if (token = _ID) then
  1412. begin
  1413. if pattern='MOVENEXT' then
  1414. begin
  1415. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1416. message(parser_e_only_one_enumerator_movenext);
  1417. pd.calcparas;
  1418. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1419. (pd.minparacount = 0) then
  1420. begin
  1421. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1422. include(pd.procoptions,po_enumerator_movenext);
  1423. end
  1424. else
  1425. Message(parser_e_enumerator_movenext_is_not_valid)
  1426. end
  1427. else
  1428. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1429. consume(token);
  1430. end
  1431. else
  1432. Message(parser_e_enumerator_identifier_required);
  1433. end;
  1434. procedure pd_virtual(pd:tabstractprocdef);
  1435. {$ifdef WITHDMT}
  1436. var
  1437. pt : tnode;
  1438. {$endif WITHDMT}
  1439. begin
  1440. if (not assigned(pd.owner.defowner) or
  1441. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1442. (po_external in pd.procoptions) then
  1443. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1444. if pd.typ<>procdef then
  1445. internalerror(2003042610);
  1446. if (pd.proctypeoption=potype_constructor) and
  1447. is_object(tprocdef(pd).struct) then
  1448. Message(parser_e_constructor_cannot_be_not_virtual);
  1449. if is_objectpascal_helper(tprocdef(pd).struct) and
  1450. (m_objfpc in current_settings.modeswitches) then
  1451. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1452. {$ifdef WITHDMT}
  1453. if is_object(tprocdef(pd).struct) and
  1454. (token<>_SEMICOLON) then
  1455. begin
  1456. { any type of parameter is allowed here! }
  1457. pt:=comp_expr(true);
  1458. if is_constintnode(pt) then
  1459. begin
  1460. include(pd.procoptions,po_msgint);
  1461. pd.messageinf.i:=pt.value;
  1462. end
  1463. else
  1464. Message(parser_e_ill_msg_expr);
  1465. disposetree(pt);
  1466. end;
  1467. {$endif WITHDMT}
  1468. end;
  1469. procedure pd_dispid(pd:tabstractprocdef);
  1470. var pt:Tnode;
  1471. begin
  1472. if pd.typ<>procdef then
  1473. internalerror(200604301);
  1474. pt:=comp_expr(true,false);
  1475. if is_constintnode(pt) then
  1476. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1477. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1478. else
  1479. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1480. else
  1481. message(parser_e_dispid_must_be_ord_const);
  1482. pt.free;
  1483. end;
  1484. procedure pd_static(pd:tabstractprocdef);
  1485. begin
  1486. if pd.typ=procdef then
  1487. include(tprocdef(pd).procsym.symoptions,sp_static);
  1488. include(pd.procoptions,po_staticmethod);
  1489. end;
  1490. procedure pd_override(pd:tabstractprocdef);
  1491. begin
  1492. if pd.typ<>procdef then
  1493. internalerror(2003042611);
  1494. if is_objectpascal_helper(tprocdef(pd).struct) then
  1495. begin
  1496. if m_objfpc in current_settings.modeswitches then
  1497. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1498. end
  1499. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1500. Message(parser_e_no_object_override)
  1501. else if is_objccategory(tprocdef(pd).struct) then
  1502. Message(parser_e_no_category_override)
  1503. else if (po_external in pd.procoptions) and
  1504. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1505. not is_cppclass(tprocdef(pd).struct) and
  1506. not is_java_class_or_interface(tprocdef(pd).struct) then
  1507. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1508. end;
  1509. procedure pd_overload(pd:tabstractprocdef);
  1510. begin
  1511. if pd.typ<>procdef then
  1512. internalerror(2003042612);
  1513. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1514. end;
  1515. procedure pd_message(pd:tabstractprocdef);
  1516. var
  1517. pt : tnode;
  1518. paracnt : longint;
  1519. begin
  1520. if pd.typ<>procdef then
  1521. internalerror(2003042613);
  1522. if is_objectpascal_helper(tprocdef(pd).struct) then
  1523. begin
  1524. if m_objfpc in current_settings.modeswitches then
  1525. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1526. end
  1527. else
  1528. if not is_class(tprocdef(pd).struct) and
  1529. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1530. Message(parser_e_msg_only_for_classes);
  1531. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1532. Message(parser_e_multiple_messages);
  1533. { check parameter type }
  1534. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1535. begin
  1536. if po_external in pd.procoptions then
  1537. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1538. paracnt:=0;
  1539. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1540. if paracnt<>1 then
  1541. Message(parser_e_ill_msg_param);
  1542. end;
  1543. pt:=comp_expr(true,false);
  1544. { message is 1-character long }
  1545. if is_constcharnode(pt) then
  1546. begin
  1547. include(pd.procoptions,po_msgstr);
  1548. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1549. end
  1550. else if pt.nodetype=stringconstn then
  1551. begin
  1552. include(pd.procoptions,po_msgstr);
  1553. if (tstringconstnode(pt).len>255) then
  1554. Message(parser_e_message_string_too_long);
  1555. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1556. end
  1557. else
  1558. if is_constintnode(pt) and
  1559. (is_class(tprocdef(pd).struct) or
  1560. is_objectpascal_helper(tprocdef(pd).struct)) then
  1561. begin
  1562. include(pd.procoptions,po_msgint);
  1563. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1564. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1565. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  1566. else
  1567. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1568. end
  1569. else
  1570. Message(parser_e_ill_msg_expr);
  1571. { check whether the selector name is valid in case of Objective-C }
  1572. if (po_msgstr in pd.procoptions) and
  1573. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1574. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1575. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1576. pt.free;
  1577. end;
  1578. procedure pd_reintroduce(pd:tabstractprocdef);
  1579. begin
  1580. if pd.typ<>procdef then
  1581. internalerror(200401211);
  1582. if is_objectpascal_helper(tprocdef(pd).struct) then
  1583. begin
  1584. if m_objfpc in current_settings.modeswitches then
  1585. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1586. end
  1587. else
  1588. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1589. not(is_objccategory(tprocdef(pd).struct)) and
  1590. not(is_javaclass(tprocdef(pd).struct)) then
  1591. Message(parser_e_no_object_reintroduce);
  1592. end;
  1593. procedure pd_syscall(pd:tabstractprocdef);
  1594. {$if defined(powerpc) or defined(m68k)}
  1595. var
  1596. vs : tparavarsym;
  1597. sym : tsym;
  1598. symtable : TSymtable;
  1599. v: Tconstexprint;
  1600. {$endif defined(powerpc) or defined(m68k)}
  1601. begin
  1602. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1603. internalerror(2003042614);
  1604. tprocdef(pd).forwarddef:=false;
  1605. {$ifdef m68k}
  1606. if target_info.system in [system_m68k_amiga] then
  1607. begin
  1608. include(pd.procoptions,po_syscall_legacy);
  1609. if consume_sym(sym,symtable) then
  1610. begin
  1611. if (sym.typ=staticvarsym) and
  1612. (
  1613. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1614. is_32bitint(tabstractvarsym(sym).vardef)
  1615. ) then
  1616. begin
  1617. tprocdef(pd).libsym:=sym;
  1618. if po_syscall_legacy in tprocdef(pd).procoptions then
  1619. begin
  1620. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1621. paramanager.parseparaloc(vs,'A6');
  1622. pd.parast.insert(vs);
  1623. end
  1624. end
  1625. else
  1626. Message(parser_e_32bitint_or_pointer_variable_expected);
  1627. end;
  1628. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1629. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1630. v:=get_intconst;
  1631. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1632. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  1633. else
  1634. Tprocdef(pd).extnumber:=v.uvalue;
  1635. end;
  1636. {$endif m68k}
  1637. {$ifdef powerpc}
  1638. if target_info.system = system_powerpc_amiga then
  1639. begin
  1640. include(pd.procoptions,po_syscall_basesysv);
  1641. if consume_sym(sym,symtable) then
  1642. begin
  1643. if (sym.typ=staticvarsym) and
  1644. (
  1645. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1646. is_32bitint(tabstractvarsym(sym).vardef)
  1647. ) then
  1648. begin
  1649. tprocdef(pd).libsym:=sym;
  1650. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1651. pd.parast.insert(vs);
  1652. end
  1653. else
  1654. Message(parser_e_32bitint_or_pointer_variable_expected);
  1655. end;
  1656. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1657. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1658. v:=get_intconst;
  1659. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1660. message(parser_e_range_check_error)
  1661. else
  1662. Tprocdef(pd).extnumber:=v.uvalue;
  1663. end else
  1664. if target_info.system = system_powerpc_morphos then
  1665. begin
  1666. if idtoken=_LEGACY then
  1667. begin
  1668. consume(_LEGACY);
  1669. include(pd.procoptions,po_syscall_legacy);
  1670. end
  1671. else if idtoken=_SYSV then
  1672. begin
  1673. consume(_SYSV);
  1674. include(pd.procoptions,po_syscall_sysv);
  1675. end
  1676. else if idtoken=_BASESYSV then
  1677. begin
  1678. consume(_BASESYSV);
  1679. include(pd.procoptions,po_syscall_basesysv);
  1680. end
  1681. else if idtoken=_SYSVBASE then
  1682. begin
  1683. consume(_SYSVBASE);
  1684. include(pd.procoptions,po_syscall_sysvbase);
  1685. end
  1686. else if idtoken=_R12BASE then
  1687. begin
  1688. consume(_R12BASE);
  1689. include(pd.procoptions,po_syscall_r12base);
  1690. end
  1691. else
  1692. if syscall_convention='LEGACY' then
  1693. include(pd.procoptions,po_syscall_legacy)
  1694. else if syscall_convention='SYSV' then
  1695. include(pd.procoptions,po_syscall_sysv)
  1696. else if syscall_convention='BASESYSV' then
  1697. include(pd.procoptions,po_syscall_basesysv)
  1698. else if syscall_convention='SYSVBASE' then
  1699. include(pd.procoptions,po_syscall_sysvbase)
  1700. else if syscall_convention='R12BASE' then
  1701. include(pd.procoptions,po_syscall_r12base)
  1702. else
  1703. internalerror(2005010404);
  1704. if consume_sym(sym,symtable) then
  1705. begin
  1706. if (sym.typ=staticvarsym) and
  1707. (
  1708. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1709. is_32bitint(tabstractvarsym(sym).vardef)
  1710. ) then
  1711. begin
  1712. tprocdef(pd).libsym:=sym;
  1713. if po_syscall_legacy in tprocdef(pd).procoptions then
  1714. begin
  1715. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1716. paramanager.parseparaloc(vs,'A6');
  1717. pd.parast.insert(vs);
  1718. end
  1719. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1720. begin
  1721. { Nothing to be done for sysv here for now, but this might change }
  1722. end
  1723. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1724. begin
  1725. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1726. pd.parast.insert(vs);
  1727. end
  1728. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1729. begin
  1730. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1731. pd.parast.insert(vs);
  1732. end
  1733. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1734. begin
  1735. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1736. paramanager.parseparaloc(vs,'R12');
  1737. pd.parast.insert(vs);
  1738. end
  1739. else
  1740. internalerror(2005010501);
  1741. end
  1742. else
  1743. Message(parser_e_32bitint_or_pointer_variable_expected);
  1744. end;
  1745. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1746. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1747. v:=get_intconst;
  1748. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1749. message(parser_e_range_check_error)
  1750. else
  1751. Tprocdef(pd).extnumber:=v.uvalue;
  1752. end;
  1753. {$endif powerpc}
  1754. end;
  1755. procedure pd_external(pd:tabstractprocdef);
  1756. {
  1757. If import_dll=nil the procedure is assumed to be in another
  1758. object file. In that object file it should have the name to
  1759. which import_name is pointing to. Otherwise, the procedure is
  1760. assumed to be in the DLL to which import_dll is pointing to. In
  1761. that case either import_nr<>0 or import_name<>nil is true, so
  1762. the procedure is either imported by number or by name. (DM)
  1763. }
  1764. var
  1765. hs : string;
  1766. v:Tconstexprint;
  1767. is_java_external: boolean;
  1768. begin
  1769. if pd.typ<>procdef then
  1770. internalerror(2003042615);
  1771. { Allow specifying a separate external name for methods in external Java
  1772. because its identifier naming constraints are laxer than FPC's
  1773. (e.g., case sensitive).
  1774. Limitation: only allows specifying the symbol name and not the package name,
  1775. and only for external classes/interfaces }
  1776. is_java_external:=
  1777. (pd.typ=procdef) and
  1778. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  1779. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  1780. with tprocdef(pd) do
  1781. begin
  1782. forwarddef:=false;
  1783. { forbid local external procedures }
  1784. if parast.symtablelevel>normal_function_level then
  1785. Message(parser_e_no_local_proc_external);
  1786. { If the procedure should be imported from a DLL, a constant string follows.
  1787. This isn't really correct, an contant string expression follows
  1788. so we check if an semicolon follows, else a string constant have to
  1789. follow (FK) }
  1790. if not is_java_external and
  1791. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1792. begin
  1793. { Always add library prefix and suffix to create an uniform name }
  1794. hs:=get_stringconst;
  1795. if ExtractFileExt(hs)='' then
  1796. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1797. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1798. hs:=target_info.sharedlibprefix+hs;
  1799. { the JVM expects java/lang/Object rather than java.lang.Object }
  1800. if target_info.system in systems_jvm then
  1801. Replace(hs,'.','/');
  1802. import_dll:=stringdup(hs);
  1803. include(procoptions,po_has_importdll);
  1804. if (idtoken=_NAME) then
  1805. begin
  1806. consume(_NAME);
  1807. import_name:=stringdup(get_stringconst);
  1808. include(procoptions,po_has_importname);
  1809. if import_name^='' then
  1810. message(parser_e_empty_import_name);
  1811. end;
  1812. if (idtoken=_INDEX) then
  1813. begin
  1814. {After the word index follows the index number in the DLL.}
  1815. consume(_INDEX);
  1816. v:=get_intconst;
  1817. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1818. message(parser_e_range_check_error)
  1819. else
  1820. import_nr:=longint(v.svalue);
  1821. end;
  1822. { default is to used the realname of the procedure }
  1823. if (import_nr=0) and not assigned(import_name) then
  1824. begin
  1825. import_name:=stringdup(procsym.realname);
  1826. include(procoptions,po_has_importname);
  1827. end;
  1828. end
  1829. else
  1830. begin
  1831. if (idtoken=_NAME) or
  1832. is_java_external then
  1833. begin
  1834. consume(_NAME);
  1835. import_name:=stringdup(get_stringconst);
  1836. include(procoptions,po_has_importname);
  1837. if import_name^='' then
  1838. message(parser_e_empty_import_name);
  1839. end;
  1840. end;
  1841. end;
  1842. end;
  1843. procedure pd_weakexternal(pd:tabstractprocdef);
  1844. begin
  1845. if not(target_info.system in systems_weak_linking) then
  1846. message(parser_e_weak_external_not_supported)
  1847. else
  1848. pd_external(pd);
  1849. end;
  1850. type
  1851. pd_handler=procedure(pd:tabstractprocdef);
  1852. proc_dir_rec=record
  1853. idtok : ttoken;
  1854. pd_flags : tpdflags;
  1855. handler : pd_handler;
  1856. pocall : tproccalloption;
  1857. pooption : tprocoptions;
  1858. mutexclpocall : tproccalloptions;
  1859. mutexclpotype : tproctypeoptions;
  1860. mutexclpo : tprocoptions;
  1861. end;
  1862. const
  1863. {Should contain the number of procedure directives we support.}
  1864. num_proc_directives=43;
  1865. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1866. (
  1867. (
  1868. idtok:_ABSTRACT;
  1869. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  1870. handler : @pd_abstract;
  1871. pocall : pocall_none;
  1872. pooption : [po_abstractmethod];
  1873. mutexclpocall : [pocall_internproc];
  1874. mutexclpotype : [];
  1875. mutexclpo : [po_exports,po_interrupt,po_inline]
  1876. ),(
  1877. idtok:_ALIAS;
  1878. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1879. handler : @pd_alias;
  1880. pocall : pocall_none;
  1881. pooption : [];
  1882. mutexclpocall : [];
  1883. mutexclpotype : [];
  1884. mutexclpo : [po_external,po_inline]
  1885. ),(
  1886. idtok:_ASMNAME;
  1887. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1888. handler : @pd_asmname;
  1889. pocall : pocall_cdecl;
  1890. pooption : [po_external];
  1891. mutexclpocall : [pocall_internproc];
  1892. mutexclpotype : [];
  1893. mutexclpo : [po_external,po_inline]
  1894. ),(
  1895. idtok:_ASSEMBLER;
  1896. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1897. handler : nil;
  1898. pocall : pocall_none;
  1899. pooption : [po_assembler];
  1900. mutexclpocall : [];
  1901. mutexclpotype : [];
  1902. mutexclpo : [po_external]
  1903. ),(
  1904. idtok:_C; {same as cdecl for mode mac}
  1905. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1906. handler : nil;
  1907. pocall : pocall_cdecl;
  1908. pooption : [];
  1909. mutexclpocall : [];
  1910. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1911. mutexclpo : [po_assembler,po_external]
  1912. ),(
  1913. idtok:_CDECL;
  1914. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1915. handler : nil;
  1916. pocall : pocall_cdecl;
  1917. pooption : [];
  1918. mutexclpocall : [];
  1919. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1920. mutexclpo : [po_assembler,po_external]
  1921. ),(
  1922. idtok:_DISPID;
  1923. pd_flags : [pd_dispinterface];
  1924. handler : @pd_dispid;
  1925. pocall : pocall_none;
  1926. pooption : [po_dispid];
  1927. mutexclpocall : [pocall_internproc];
  1928. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  1929. mutexclpo : [po_interrupt,po_external,po_inline]
  1930. ),(
  1931. idtok:_DYNAMIC;
  1932. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  1933. handler : @pd_virtual;
  1934. pocall : pocall_none;
  1935. pooption : [po_virtualmethod];
  1936. mutexclpocall : [pocall_internproc];
  1937. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  1938. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1939. ),(
  1940. idtok:_EXPORT;
  1941. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  1942. handler : @pd_export;
  1943. pocall : pocall_none;
  1944. pooption : [po_exports,po_global];
  1945. mutexclpocall : [pocall_internproc];
  1946. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1947. mutexclpo : [po_external,po_interrupt,po_inline]
  1948. ),(
  1949. idtok:_EXTERNAL;
  1950. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  1951. handler : @pd_external;
  1952. pocall : pocall_none;
  1953. pooption : [po_external];
  1954. mutexclpocall : [pocall_syscall];
  1955. { allowed for external cpp classes }
  1956. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  1957. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1958. ),(
  1959. idtok:_FAR;
  1960. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1961. handler : @pd_far;
  1962. pocall : pocall_none;
  1963. pooption : [];
  1964. mutexclpocall : [pocall_internproc];
  1965. mutexclpotype : [];
  1966. mutexclpo : [po_inline]
  1967. ),(
  1968. idtok:_FAR16;
  1969. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  1970. handler : nil;
  1971. pocall : pocall_far16;
  1972. pooption : [];
  1973. mutexclpocall : [];
  1974. mutexclpotype : [];
  1975. mutexclpo : [po_external]
  1976. ),(
  1977. idtok:_FINAL;
  1978. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  1979. handler : @pd_final;
  1980. pocall : pocall_none;
  1981. pooption : [po_finalmethod];
  1982. mutexclpocall : [pocall_internproc];
  1983. mutexclpotype : [];
  1984. mutexclpo : [po_exports,po_interrupt,po_inline]
  1985. ),(
  1986. idtok:_FORWARD;
  1987. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  1988. handler : @pd_forward;
  1989. pocall : pocall_none;
  1990. pooption : [];
  1991. mutexclpocall : [pocall_internproc];
  1992. mutexclpotype : [];
  1993. mutexclpo : [po_external,po_inline]
  1994. ),(
  1995. idtok:_OLDFPCCALL;
  1996. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1997. handler : nil;
  1998. pocall : pocall_oldfpccall;
  1999. pooption : [];
  2000. mutexclpocall : [];
  2001. mutexclpotype : [];
  2002. mutexclpo : []
  2003. ),(
  2004. idtok:_INLINE;
  2005. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2006. handler : nil;
  2007. pocall : pocall_none;
  2008. pooption : [po_inline];
  2009. mutexclpocall : [pocall_safecall];
  2010. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2011. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  2012. ),(
  2013. idtok:_INTERNCONST;
  2014. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2015. handler : @pd_internconst;
  2016. pocall : pocall_none;
  2017. pooption : [po_internconst];
  2018. mutexclpocall : [];
  2019. mutexclpotype : [potype_operator];
  2020. mutexclpo : []
  2021. ),(
  2022. idtok:_INTERNPROC;
  2023. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2024. handler : @pd_internproc;
  2025. pocall : pocall_internproc;
  2026. pooption : [];
  2027. mutexclpocall : [];
  2028. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2029. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2030. ),(
  2031. idtok:_INTERRUPT;
  2032. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2033. handler : @pd_interrupt;
  2034. pocall : pocall_oldfpccall;
  2035. pooption : [po_interrupt];
  2036. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,pocall_mwpascal,
  2037. pocall_pascal,pocall_far16,pocall_oldfpccall];
  2038. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2039. mutexclpo : [po_external,po_inline]
  2040. ),(
  2041. idtok:_IOCHECK;
  2042. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2043. handler : nil;
  2044. pocall : pocall_none;
  2045. pooption : [po_iocheck];
  2046. mutexclpocall : [pocall_internproc];
  2047. mutexclpotype : [];
  2048. mutexclpo : [po_external]
  2049. ),(
  2050. idtok:_LOCAL;
  2051. pd_flags : [pd_implemen,pd_body];
  2052. handler : nil;
  2053. pocall : pocall_none;
  2054. pooption : [po_kylixlocal];
  2055. mutexclpocall : [pocall_internproc,pocall_far16];
  2056. mutexclpotype : [];
  2057. mutexclpo : [po_external,po_exports]
  2058. ),(
  2059. idtok:_MESSAGE;
  2060. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2061. handler : @pd_message;
  2062. pocall : pocall_none;
  2063. pooption : []; { can be po_msgstr or po_msgint }
  2064. mutexclpocall : [pocall_internproc];
  2065. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2066. mutexclpo : [po_interrupt,po_inline]
  2067. ),(
  2068. idtok:_MWPASCAL;
  2069. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2070. handler : nil;
  2071. pocall : pocall_mwpascal;
  2072. pooption : [];
  2073. mutexclpocall : [];
  2074. mutexclpotype : [];
  2075. mutexclpo : []
  2076. ),(
  2077. idtok:_NEAR;
  2078. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2079. handler : @pd_near;
  2080. pocall : pocall_none;
  2081. pooption : [];
  2082. mutexclpocall : [pocall_internproc];
  2083. mutexclpotype : [];
  2084. mutexclpo : []
  2085. ),(
  2086. idtok:_NOSTACKFRAME;
  2087. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2088. handler : nil;
  2089. pocall : pocall_none;
  2090. pooption : [po_nostackframe];
  2091. mutexclpocall : [pocall_internproc];
  2092. mutexclpotype : [];
  2093. mutexclpo : []
  2094. ),(
  2095. idtok:_OVERLOAD;
  2096. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava,pd_objcclass,pd_objcprot];
  2097. handler : @pd_overload;
  2098. pocall : pocall_none;
  2099. pooption : [po_overload];
  2100. mutexclpocall : [pocall_internproc];
  2101. mutexclpotype : [];
  2102. mutexclpo : []
  2103. ),(
  2104. idtok:_OVERRIDE;
  2105. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2106. handler : @pd_override;
  2107. pocall : pocall_none;
  2108. pooption : [po_overridingmethod,po_virtualmethod];
  2109. mutexclpocall : [pocall_internproc];
  2110. mutexclpotype : [];
  2111. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2112. ),(
  2113. idtok:_PASCAL;
  2114. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2115. handler : nil;
  2116. pocall : pocall_pascal;
  2117. pooption : [];
  2118. mutexclpocall : [];
  2119. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2120. mutexclpo : [po_external]
  2121. ),(
  2122. idtok:_PUBLIC;
  2123. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2124. handler : @pd_public;
  2125. pocall : pocall_none;
  2126. pooption : [po_public,po_global];
  2127. mutexclpocall : [pocall_internproc];
  2128. mutexclpotype : [];
  2129. mutexclpo : [po_external,po_inline]
  2130. ),(
  2131. idtok:_REGISTER;
  2132. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2133. handler : nil;
  2134. pocall : pocall_register;
  2135. pooption : [];
  2136. mutexclpocall : [];
  2137. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2138. mutexclpo : [po_external]
  2139. ),(
  2140. idtok:_REINTRODUCE;
  2141. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2142. handler : @pd_reintroduce;
  2143. pocall : pocall_none;
  2144. pooption : [po_reintroduce];
  2145. mutexclpocall : [pocall_internproc];
  2146. mutexclpotype : [];
  2147. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2148. ),(
  2149. idtok:_SAFECALL;
  2150. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2151. handler : nil;
  2152. pocall : pocall_safecall;
  2153. pooption : [];
  2154. mutexclpocall : [];
  2155. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2156. mutexclpo : [po_external]
  2157. ),(
  2158. idtok:_SOFTFLOAT;
  2159. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2160. handler : nil;
  2161. pocall : pocall_softfloat;
  2162. pooption : [];
  2163. mutexclpocall : [];
  2164. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2165. { it's available with po_external because the libgcc floating point routines on the arm
  2166. uses this calling convention }
  2167. mutexclpo : []
  2168. ),(
  2169. idtok:_STATIC;
  2170. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2171. handler : @pd_static;
  2172. pocall : pocall_none;
  2173. pooption : [po_staticmethod];
  2174. mutexclpocall : [pocall_internproc];
  2175. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2176. mutexclpo : [po_interrupt,po_exports]
  2177. ),(
  2178. idtok:_STDCALL;
  2179. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2180. handler : nil;
  2181. pocall : pocall_stdcall;
  2182. pooption : [];
  2183. mutexclpocall : [];
  2184. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2185. mutexclpo : [po_external]
  2186. ),(
  2187. idtok:_SYSCALL;
  2188. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2189. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2190. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2191. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2192. handler : @pd_syscall;
  2193. pocall : pocall_syscall;
  2194. pooption : [];
  2195. mutexclpocall : [];
  2196. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2197. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2198. ),(
  2199. idtok:_VIRTUAL;
  2200. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2201. handler : @pd_virtual;
  2202. pocall : pocall_none;
  2203. pooption : [po_virtualmethod];
  2204. mutexclpocall : [pocall_internproc];
  2205. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2206. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline]
  2207. ),(
  2208. idtok:_CPPDECL;
  2209. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2210. handler : nil;
  2211. pocall : pocall_cppdecl;
  2212. pooption : [];
  2213. mutexclpocall : [];
  2214. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2215. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2216. ),(
  2217. idtok:_VARARGS;
  2218. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2219. handler : nil;
  2220. pocall : pocall_none;
  2221. pooption : [po_varargs];
  2222. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2223. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2224. mutexclpotype : [];
  2225. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2226. ),(
  2227. idtok:_COMPILERPROC;
  2228. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2229. handler : nil;
  2230. pocall : pocall_none;
  2231. pooption : [po_compilerproc];
  2232. mutexclpocall : [];
  2233. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2234. mutexclpo : [po_interrupt]
  2235. ),(
  2236. idtok:_WEAKEXTERNAL;
  2237. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2238. handler : @pd_weakexternal;
  2239. pocall : pocall_none;
  2240. { mark it both external and weak external, so we don't have to
  2241. adapt all code for external symbols to also check for weak external
  2242. }
  2243. pooption : [po_external,po_weakexternal];
  2244. mutexclpocall : [pocall_internproc,pocall_syscall];
  2245. { allowed for external cpp classes }
  2246. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2247. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2248. ),(
  2249. idtok:_ENUMERATOR;
  2250. pd_flags : [pd_interface,pd_object,pd_record];
  2251. handler : @pd_enumerator;
  2252. pocall : pocall_none;
  2253. pooption : [];
  2254. mutexclpocall : [pocall_internproc];
  2255. mutexclpotype : [];
  2256. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2257. ),(
  2258. idtok:_RTLPROC;
  2259. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2260. handler : nil;
  2261. pocall : pocall_none;
  2262. pooption : [po_rtlproc];
  2263. mutexclpocall : [];
  2264. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2265. mutexclpo : [po_interrupt]
  2266. )
  2267. );
  2268. function check_proc_directive(isprocvar:boolean):boolean;
  2269. var
  2270. i : longint;
  2271. begin
  2272. result:=false;
  2273. for i:=1 to num_proc_directives do
  2274. if proc_direcdata[i].idtok=idtoken then
  2275. begin
  2276. if ((not isprocvar) or
  2277. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2278. { don't eat a public directive in classes }
  2279. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2280. result:=true;
  2281. exit;
  2282. end;
  2283. end;
  2284. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2285. {
  2286. Parse the procedure directive, returns true if a correct directive is found
  2287. }
  2288. var
  2289. p : longint;
  2290. found : boolean;
  2291. name : TIDString;
  2292. begin
  2293. parse_proc_direc:=false;
  2294. name:=tokeninfo^[idtoken].str;
  2295. found:=false;
  2296. { Hint directive? Then exit immediatly }
  2297. if (m_hintdirective in current_settings.modeswitches) then
  2298. begin
  2299. case idtoken of
  2300. _LIBRARY,
  2301. _PLATFORM,
  2302. _UNIMPLEMENTED,
  2303. _EXPERIMENTAL,
  2304. _DEPRECATED :
  2305. exit;
  2306. end;
  2307. end;
  2308. { C directive is MacPas only, because it breaks too much existing code
  2309. on other platforms (PFV) }
  2310. if (idtoken=_C) and
  2311. not(m_mac in current_settings.modeswitches) then
  2312. exit;
  2313. { retrieve data for directive if found }
  2314. for p:=1 to num_proc_directives do
  2315. if proc_direcdata[p].idtok=idtoken then
  2316. begin
  2317. found:=true;
  2318. break;
  2319. end;
  2320. { Check if the procedure directive is known }
  2321. if not found then
  2322. begin
  2323. { parsing a procvar type the name can be any
  2324. next variable !! }
  2325. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2326. not(idtoken=_PROPERTY) then
  2327. Message1(parser_w_unknown_proc_directive_ignored,name);
  2328. exit;
  2329. end;
  2330. { check if method and directive not for object, like public.
  2331. This needs to be checked also for procvars }
  2332. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2333. (symtablestack.top.symtabletype=ObjectSymtable) and
  2334. { directive allowed for cpp classes? }
  2335. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2336. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2337. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2338. exit;
  2339. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2340. (symtablestack.top.symtabletype=recordsymtable) then
  2341. exit;
  2342. { check if method and directive not for java class }
  2343. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2344. is_javaclass(tdef(symtablestack.top.defowner)) then
  2345. exit;
  2346. { check if method and directive not for java interface }
  2347. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2348. is_javainterface(tdef(symtablestack.top.defowner)) then
  2349. exit;
  2350. { Conflicts between directives ? }
  2351. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2352. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2353. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2354. begin
  2355. Message1(parser_e_proc_dir_conflict,name);
  2356. exit;
  2357. end;
  2358. { set calling convention }
  2359. if proc_direcdata[p].pocall<>pocall_none then
  2360. begin
  2361. if (po_hascallingconvention in pd.procoptions) then
  2362. begin
  2363. Message2(parser_w_proc_overriding_calling,
  2364. proccalloptionStr[pd.proccalloption],
  2365. proccalloptionStr[proc_direcdata[p].pocall]);
  2366. end;
  2367. { check if the target processor supports this calling convention }
  2368. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2369. begin
  2370. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2371. { recover }
  2372. proc_direcdata[p].pocall:=pocall_stdcall;
  2373. end;
  2374. pd.proccalloption:=proc_direcdata[p].pocall;
  2375. include(pd.procoptions,po_hascallingconvention);
  2376. end;
  2377. if pd.typ=procdef then
  2378. begin
  2379. { Check if the directive is only for objects }
  2380. if (pd_object in proc_direcdata[p].pd_flags) and
  2381. not assigned(tprocdef(pd).struct) then
  2382. exit;
  2383. { Check if the directive is only for records }
  2384. if (pd_record in proc_direcdata[p].pd_flags) and
  2385. not assigned(tprocdef(pd).struct) then
  2386. exit;
  2387. { check if method and directive not for interface }
  2388. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2389. is_interface(tprocdef(pd).struct) then
  2390. exit;
  2391. { check if method and directive not for interface }
  2392. if is_dispinterface(tprocdef(pd).struct) and
  2393. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2394. exit;
  2395. { check if method and directive not for objcclass }
  2396. if is_objcclass(tprocdef(pd).struct) and
  2397. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2398. exit;
  2399. { check if method and directive not for objcprotocol }
  2400. if is_objcprotocol(tprocdef(pd).struct) and
  2401. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2402. exit;
  2403. { check if method and directive not for record/class helper }
  2404. if is_objectpascal_helper(tprocdef(pd).struct) and
  2405. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2406. end;
  2407. { consume directive, and turn flag on }
  2408. consume(token);
  2409. parse_proc_direc:=true;
  2410. { Check the pd_flags if the directive should be allowed }
  2411. if (pd_interface in pdflags) and
  2412. not(pd_interface in proc_direcdata[p].pd_flags) then
  2413. begin
  2414. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2415. exit;
  2416. end;
  2417. if (pd_implemen in pdflags) and
  2418. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2419. begin
  2420. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2421. exit;
  2422. end;
  2423. if (pd_procvar in pdflags) and
  2424. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2425. begin
  2426. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2427. exit;
  2428. end;
  2429. { Return the new pd_flags }
  2430. if not(pd_body in proc_direcdata[p].pd_flags) then
  2431. exclude(pdflags,pd_body);
  2432. { Add the correct flag }
  2433. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2434. { Call the handler }
  2435. if pointer(proc_direcdata[p].handler)<>nil then
  2436. proc_direcdata[p].handler(pd);
  2437. end;
  2438. function proc_get_importname(pd:tprocdef):string;
  2439. var
  2440. dllname, importname : string;
  2441. begin
  2442. result:='';
  2443. if not(po_external in pd.procoptions) then
  2444. internalerror(200412151);
  2445. { external name or number is specified }
  2446. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2447. begin
  2448. if assigned(pd.import_dll) then
  2449. dllname:=pd.import_dll^
  2450. else
  2451. dllname:='';
  2452. if assigned(pd.import_name) then
  2453. importname:=pd.import_name^
  2454. else
  2455. importname:='';
  2456. proc_get_importname:=make_dllmangledname(dllname,
  2457. importname,pd.import_nr,pd.proccalloption);
  2458. end
  2459. else
  2460. begin
  2461. { Default names when importing variables }
  2462. case pd.proccalloption of
  2463. pocall_cdecl :
  2464. begin
  2465. if assigned(pd.struct) then
  2466. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2467. else
  2468. result:=target_info.Cprefix+pd.procsym.realname;
  2469. end;
  2470. pocall_cppdecl :
  2471. begin
  2472. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2473. end;
  2474. else
  2475. begin
  2476. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2477. { but according to MacPas mode description
  2478. Cprefix should still be used PM }
  2479. if (m_mac in current_settings.modeswitches) then
  2480. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2481. end;
  2482. end;
  2483. end;
  2484. end;
  2485. procedure proc_set_mangledname(pd:tprocdef);
  2486. var
  2487. s : string;
  2488. begin
  2489. { When the mangledname is already set we aren't allowed to change
  2490. it because it can already be used somewhere (PFV) }
  2491. if not(po_has_mangledname in pd.procoptions) then
  2492. begin
  2493. if (po_external in pd.procoptions) then
  2494. begin
  2495. { External Procedures are only allowed to change the mangledname
  2496. in their first declaration }
  2497. if (pd.forwarddef or (not pd.hasforward)) then
  2498. begin
  2499. s:=proc_get_importname(pd);
  2500. if s<>'' then
  2501. begin
  2502. pd.setmangledname(s);
  2503. end;
  2504. end;
  2505. end
  2506. else
  2507. { Normal procedures }
  2508. begin
  2509. if (po_compilerproc in pd.procoptions) then
  2510. begin
  2511. pd.setmangledname(lower(pd.procsym.name));
  2512. end;
  2513. end;
  2514. end;
  2515. { Public/exported alias names }
  2516. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2517. not(po_has_public_name in pd.procoptions) then
  2518. begin
  2519. case pd.proccalloption of
  2520. pocall_cdecl :
  2521. begin
  2522. if assigned(pd.struct) then
  2523. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2524. else
  2525. begin
  2526. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2527. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2528. (po_exports in pd.procoptions) then
  2529. pd.aliasnames.insert(pd.procsym.realname)
  2530. else
  2531. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2532. end;
  2533. end;
  2534. pocall_cppdecl :
  2535. begin
  2536. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2537. end;
  2538. end;
  2539. { prevent adding the alias a second time }
  2540. include(pd.procoptions,po_has_public_name);
  2541. end;
  2542. end;
  2543. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  2544. begin
  2545. if hcc_check in flags then
  2546. begin
  2547. { set the default calling convention if none provided }
  2548. if (pd.typ=procdef) and
  2549. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2550. is_cppclass(tprocdef(pd).struct)) then
  2551. begin
  2552. { none of the explicit calling conventions should be allowed }
  2553. if (po_hascallingconvention in pd.procoptions) then
  2554. internalerror(2009032501);
  2555. if is_cppclass(tprocdef(pd).struct) then
  2556. pd.proccalloption:=pocall_cppdecl
  2557. else
  2558. pd.proccalloption:=pocall_cdecl;
  2559. end
  2560. else if not(po_hascallingconvention in pd.procoptions) then
  2561. pd.proccalloption:=current_settings.defproccall
  2562. else
  2563. begin
  2564. if pd.proccalloption=pocall_none then
  2565. internalerror(200309081);
  2566. end;
  2567. { handle proccall specific settings }
  2568. case pd.proccalloption of
  2569. pocall_cdecl,
  2570. pocall_cppdecl :
  2571. begin
  2572. { check C cdecl para types }
  2573. check_c_para(pd);
  2574. end;
  2575. pocall_far16 :
  2576. begin
  2577. { Temporary stub, must be rewritten to support OS/2 far16 }
  2578. Message1(parser_w_proc_directive_ignored,'FAR16');
  2579. end;
  2580. end;
  2581. { Inlining is enabled and supported? }
  2582. if (po_inline in pd.procoptions) and
  2583. not(cs_do_inline in current_settings.localswitches) then
  2584. begin
  2585. { Give an error if inline is not supported by the compiler mode,
  2586. otherwise only give a hint that this procedure will not be inlined }
  2587. if not(m_default_inline in current_settings.modeswitches) then
  2588. Message(parser_e_proc_inline_not_supported)
  2589. else
  2590. Message(parser_h_inlining_disabled);
  2591. exclude(pd.procoptions,po_inline);
  2592. end;
  2593. { For varargs directive also cdecl and external must be defined }
  2594. if (po_varargs in pd.procoptions) then
  2595. begin
  2596. { check first for external in the interface, if available there
  2597. then the cdecl must also be there since there is no implementation
  2598. available to contain it }
  2599. if parse_only then
  2600. begin
  2601. { if external is available, then cdecl must also be available,
  2602. procvars don't need external }
  2603. if not((po_external in pd.procoptions) or
  2604. (pd.typ=procvardef) or
  2605. { for objcclasses this is checked later, because the entire
  2606. class may be external. }
  2607. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2608. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2609. Message(parser_e_varargs_need_cdecl_and_external);
  2610. end
  2611. else
  2612. begin
  2613. { both must be defined now }
  2614. if not((po_external in pd.procoptions) or
  2615. (pd.typ=procvardef)) or
  2616. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2617. Message(parser_e_varargs_need_cdecl_and_external);
  2618. end;
  2619. end;
  2620. end;
  2621. if hcc_insert_hidden_paras in flags then
  2622. begin
  2623. { insert hidden high parameters }
  2624. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2625. { insert hidden self parameter }
  2626. insert_self_and_vmt_para(pd);
  2627. { insert funcret parameter if required }
  2628. insert_funcret_para(pd);
  2629. { Make var parameters regable, this must be done after the calling
  2630. convention is set. }
  2631. { this must be done before parentfp is insert, because getting all cases
  2632. where parentfp must be in a memory location isn't catched properly so
  2633. we put parentfp never in a register }
  2634. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2635. { insert parentfp parameter if required }
  2636. insert_parentfp_para(pd);
  2637. end;
  2638. { Calculate parameter tlist }
  2639. pd.calcparas;
  2640. end;
  2641. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2642. {
  2643. Parse the procedure directives. It does not matter if procedure directives
  2644. are written using ;procdir; or ['procdir'] syntax.
  2645. }
  2646. var
  2647. res : boolean;
  2648. begin
  2649. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2650. begin
  2651. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2652. include(pd.procoptions,po_public);
  2653. include(pd.procoptions,po_has_public_name);
  2654. include(pd.procoptions,po_global);
  2655. end;
  2656. { methods from external class definitions are all external themselves }
  2657. if (pd.typ=procdef) and
  2658. assigned(tprocdef(pd).struct) and
  2659. (tprocdef(pd).struct.typ=objectdef) and
  2660. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  2661. tprocdef(pd).make_external;
  2662. { Class constructors and destructor are static class methods in real. }
  2663. { There are many places in the compiler where either class or static }
  2664. { method flag changes the behavior. It is simplier to add them to }
  2665. { the class constructors/destructors options than to fix all the }
  2666. { occurencies. (Paul) }
  2667. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2668. begin
  2669. include(pd.procoptions,po_classmethod);
  2670. include(pd.procoptions,po_staticmethod);
  2671. end;
  2672. while token in [_ID,_LECKKLAMMER] do
  2673. begin
  2674. if try_to_consume(_LECKKLAMMER) then
  2675. begin
  2676. repeat
  2677. parse_proc_direc(pd,pdflags);
  2678. until not try_to_consume(_COMMA);
  2679. consume(_RECKKLAMMER);
  2680. { we always expect at least '[];' }
  2681. res:=true;
  2682. end
  2683. else
  2684. begin
  2685. res:=parse_proc_direc(pd,pdflags);
  2686. end;
  2687. { A procedure directive normally followed by a semicolon, but in
  2688. a const section or reading a type we should stop when _EQ is found,
  2689. because a constant/default value follows }
  2690. if res then
  2691. begin
  2692. if (block_type=bt_const_type) and
  2693. (token=_EQ) then
  2694. break;
  2695. { support procedure proc;stdcall export; }
  2696. if not(check_proc_directive((pd.typ=procvardef))) then
  2697. begin
  2698. { support "record p : procedure stdcall end;" and
  2699. "var p : procedure stdcall = nil;" }
  2700. if (pd_procvar in pdflags) and
  2701. (token in [_END,_RKLAMMER,_EQ]) then
  2702. break
  2703. else
  2704. begin
  2705. if (token=_COLON) then
  2706. begin
  2707. Message(parser_e_field_not_allowed_here);
  2708. consume_all_until(_SEMICOLON);
  2709. end;
  2710. consume(_SEMICOLON)
  2711. end;
  2712. end;
  2713. end
  2714. else
  2715. break;
  2716. end;
  2717. if (po_nostackframe in pd.procoptions) and
  2718. not (po_assembler in pd.procoptions) then
  2719. message(parser_w_nostackframe_without_assembler);
  2720. end;
  2721. procedure parse_var_proc_directives(sym:tsym);
  2722. var
  2723. pdflags : tpdflags;
  2724. pd : tabstractprocdef;
  2725. begin
  2726. pdflags:=[pd_procvar];
  2727. pd:=nil;
  2728. case sym.typ of
  2729. fieldvarsym,
  2730. staticvarsym,
  2731. localvarsym,
  2732. paravarsym :
  2733. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2734. typesym :
  2735. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2736. else
  2737. internalerror(2003042617);
  2738. end;
  2739. if pd.typ<>procvardef then
  2740. internalerror(2003042618);
  2741. { names should never be used anyway }
  2742. parse_proc_directives(pd,pdflags);
  2743. end;
  2744. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2745. var
  2746. pdflags : tpdflags;
  2747. begin
  2748. pdflags:=[pd_object];
  2749. parse_proc_directives(pd,pdflags);
  2750. end;
  2751. procedure parse_record_proc_directives(pd:tabstractprocdef);
  2752. var
  2753. pdflags : tpdflags;
  2754. begin
  2755. pdflags:=[pd_record];
  2756. parse_proc_directives(pd,pdflags);
  2757. end;
  2758. function proc_add_definition(var currpd:tprocdef):boolean;
  2759. {
  2760. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2761. forwarddef is found and reused it returns true
  2762. }
  2763. var
  2764. fwpd : tprocdef;
  2765. currparasym,
  2766. fwparasym : tsym;
  2767. currparacnt,
  2768. fwparacnt,
  2769. curridx,
  2770. fwidx,
  2771. i : longint;
  2772. po_comp : tprocoptions;
  2773. paracompopt: tcompare_paras_options;
  2774. forwardfound : boolean;
  2775. symentry: TSymEntry;
  2776. begin
  2777. forwardfound:=false;
  2778. { check overloaded functions if the same function already exists }
  2779. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2780. begin
  2781. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2782. { can happen for internally generated routines }
  2783. if (fwpd=currpd) then
  2784. begin
  2785. result:=true;
  2786. exit;
  2787. end;
  2788. { Skip overloaded definitions that are declared in other units }
  2789. if fwpd.procsym<>currpd.procsym then
  2790. continue;
  2791. { check the parameters, for delphi/tp it is possible to
  2792. leave the parameters away in the implementation (forwarddef=false).
  2793. But for an overload declared function this is not allowed }
  2794. if { check if empty implementation arguments match is allowed }
  2795. (
  2796. not(m_repeat_forward in current_settings.modeswitches) and
  2797. not(currpd.forwarddef) and
  2798. is_bareprocdef(currpd) and
  2799. not(po_overload in fwpd.procoptions)
  2800. ) or
  2801. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2802. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV)
  2803. don't check default values here, because routines that are the same except for their default
  2804. values should be reported as mismatches (since you can't overload based on different default
  2805. parameter values) }
  2806. (
  2807. (compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2808. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  2809. ) then
  2810. begin
  2811. { Check if we've found the forwarddef, if found then
  2812. we need to update the forward def with the current
  2813. implementation settings }
  2814. if fwpd.forwarddef then
  2815. begin
  2816. forwardfound:=true;
  2817. if not(m_repeat_forward in current_settings.modeswitches) and
  2818. (fwpd.proccalloption<>currpd.proccalloption) then
  2819. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2820. else
  2821. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2822. { Check calling convention }
  2823. if (fwpd.proccalloption<>currpd.proccalloption) then
  2824. begin
  2825. { In delphi it is possible to specify the calling
  2826. convention in the interface or implementation if
  2827. there was no convention specified in the other
  2828. part }
  2829. if (m_delphi in current_settings.modeswitches) then
  2830. begin
  2831. if not(po_hascallingconvention in currpd.procoptions) then
  2832. currpd.proccalloption:=fwpd.proccalloption
  2833. else
  2834. if not(po_hascallingconvention in fwpd.procoptions) then
  2835. fwpd.proccalloption:=currpd.proccalloption
  2836. else
  2837. begin
  2838. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2839. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2840. { restore interface settings }
  2841. currpd.proccalloption:=fwpd.proccalloption;
  2842. end;
  2843. end
  2844. else
  2845. begin
  2846. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2847. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2848. { restore interface settings }
  2849. currpd.proccalloption:=fwpd.proccalloption;
  2850. end;
  2851. end;
  2852. { Check static }
  2853. if (po_staticmethod in fwpd.procoptions) then
  2854. begin
  2855. if not (po_staticmethod in currpd.procoptions) then
  2856. begin
  2857. include(currpd.procoptions, po_staticmethod);
  2858. if (po_classmethod in currpd.procoptions) then
  2859. begin
  2860. { remove self from the hidden paras }
  2861. symentry:=currpd.parast.Find('self');
  2862. if symentry<>nil then
  2863. begin
  2864. currpd.parast.Delete(symentry);
  2865. currpd.calcparas;
  2866. end;
  2867. end;
  2868. end;
  2869. end;
  2870. { Check if the procedure type and return type are correct,
  2871. also the parameters must match also with the type and that
  2872. if the implementation has default parameters, the interface
  2873. also has them and that if they both have them, that they
  2874. have the same value }
  2875. if ((m_repeat_forward in current_settings.modeswitches) or
  2876. not is_bareprocdef(currpd)) and
  2877. ((compare_paras(fwpd.paras,currpd.paras,cp_all,paracompopt)<>te_exact) or
  2878. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)) then
  2879. begin
  2880. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2881. fwpd.fullprocname(false));
  2882. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2883. break;
  2884. end;
  2885. { Check if both are declared forward }
  2886. if fwpd.forwarddef and currpd.forwarddef then
  2887. begin
  2888. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2889. currpd.fullprocname(false));
  2890. end;
  2891. { internconst or internproc only need to be defined once }
  2892. if (fwpd.proccalloption=pocall_internproc) then
  2893. currpd.proccalloption:=fwpd.proccalloption
  2894. else
  2895. if (currpd.proccalloption=pocall_internproc) then
  2896. fwpd.proccalloption:=currpd.proccalloption;
  2897. { Check procedure options, Delphi requires that class is
  2898. repeated in the implementation for class methods }
  2899. if (m_fpc in current_settings.modeswitches) then
  2900. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2901. else
  2902. po_comp:=[po_classmethod,po_methodpointer];
  2903. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2904. (fwpd.proctypeoption <> currpd.proctypeoption) or
  2905. { if the implementation version has an "overload" modifier,
  2906. the interface version must also have it (otherwise we can
  2907. get annoying crashes due to interface crc changes) }
  2908. (not(po_overload in fwpd.procoptions) and
  2909. (po_overload in currpd.procoptions)) then
  2910. begin
  2911. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2912. fwpd.fullprocname(false));
  2913. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2914. { This error is non-fatal, we can recover }
  2915. end;
  2916. { Forward declaration is external? }
  2917. if (po_external in fwpd.procoptions) then
  2918. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2919. { Check parameters }
  2920. if (m_repeat_forward in current_settings.modeswitches) or
  2921. (currpd.minparacount>0) then
  2922. begin
  2923. { If mangled names are equal then they have the same amount of arguments }
  2924. { We can check the names of the arguments }
  2925. { both symtables are in the same order from left to right }
  2926. curridx:=0;
  2927. fwidx:=0;
  2928. currparacnt:=currpd.parast.SymList.Count;
  2929. fwparacnt:=fwpd.parast.SymList.Count;
  2930. repeat
  2931. { skip default parameter constsyms }
  2932. while (curridx<currparacnt) and
  2933. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2934. inc(curridx);
  2935. while (fwidx<fwparacnt) and
  2936. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2937. inc(fwidx);
  2938. { stop when one of the two lists is at the end }
  2939. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2940. break;
  2941. { compare names of parameters, ignore implictly
  2942. renamed parameters }
  2943. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2944. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2945. if not(sp_implicitrename in currparasym.symoptions) and
  2946. not(sp_implicitrename in fwparasym.symoptions) then
  2947. begin
  2948. if (currparasym.name<>fwparasym.name) then
  2949. begin
  2950. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2951. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2952. break;
  2953. end;
  2954. end;
  2955. { next parameter }
  2956. inc(curridx);
  2957. inc(fwidx);
  2958. until false;
  2959. end;
  2960. { Everything is checked, now we can update the forward declaration
  2961. with the new data from the implementation }
  2962. fwpd.forwarddef:=currpd.forwarddef;
  2963. fwpd.hasforward:=true;
  2964. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2965. { marked as local but exported from unit? }
  2966. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2967. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2968. if fwpd.extnumber=$ffff then
  2969. fwpd.extnumber:=currpd.extnumber;
  2970. while not currpd.aliasnames.empty do
  2971. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2972. { update fileinfo so position references the implementation,
  2973. also update funcretsym if it is already generated }
  2974. fwpd.fileinfo:=currpd.fileinfo;
  2975. if assigned(fwpd.funcretsym) then
  2976. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2977. if assigned(currpd.deprecatedmsg) then
  2978. begin
  2979. stringdispose(fwpd.deprecatedmsg);
  2980. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  2981. end;
  2982. { import names }
  2983. if assigned(currpd.import_dll) then
  2984. begin
  2985. stringdispose(fwpd.import_dll);
  2986. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2987. end;
  2988. if assigned(currpd.import_name) then
  2989. begin
  2990. stringdispose(fwpd.import_name);
  2991. fwpd.import_name:=stringdup(currpd.import_name^);
  2992. end;
  2993. fwpd.import_nr:=currpd.import_nr;
  2994. { for compilerproc defines we need to rename and update the
  2995. symbolname to lowercase }
  2996. if (po_compilerproc in fwpd.procoptions) then
  2997. begin
  2998. { rename to lowercase so users can't access it }
  2999. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  3000. { the mangeled name is already changed by the pd_compilerproc }
  3001. { handler. It must be done immediately because if we have a }
  3002. { call to a compilerproc before it's implementation is }
  3003. { encountered, it must already use the new mangled name (JM) }
  3004. end;
  3005. { Release current procdef }
  3006. currpd.owner.deletedef(currpd);
  3007. currpd:=fwpd;
  3008. end
  3009. else
  3010. begin
  3011. { abstract methods aren't forward defined, but this }
  3012. { needs another error message }
  3013. if (po_abstractmethod in fwpd.procoptions) then
  3014. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  3015. else
  3016. begin
  3017. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  3018. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3019. end;
  3020. end;
  3021. { we found one proc with the same arguments, there are no others
  3022. so we can stop }
  3023. break;
  3024. end;
  3025. { check for allowing overload directive }
  3026. if not(m_fpc in current_settings.modeswitches) then
  3027. begin
  3028. { overload directive turns on overloading }
  3029. if ((po_overload in currpd.procoptions) or
  3030. (po_overload in fwpd.procoptions)) then
  3031. begin
  3032. { check if all procs have overloading, but not if the proc is a method or
  3033. already declared forward, then the check is already done }
  3034. if not(fwpd.hasforward or
  3035. assigned(currpd.struct) or
  3036. (currpd.forwarddef<>fwpd.forwarddef) or
  3037. ((po_overload in currpd.procoptions) and
  3038. (po_overload in fwpd.procoptions))) then
  3039. begin
  3040. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3041. break;
  3042. end
  3043. end
  3044. else
  3045. begin
  3046. if not(fwpd.forwarddef) then
  3047. begin
  3048. if (m_tp7 in current_settings.modeswitches) then
  3049. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  3050. else
  3051. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3052. break;
  3053. end;
  3054. end;
  3055. end; { equal arguments }
  3056. end;
  3057. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  3058. list }
  3059. if not forwardfound then
  3060. begin
  3061. { can happen in Delphi mode }
  3062. if (currpd.proctypeoption = potype_function) and
  3063. is_void(currpd.returndef) then
  3064. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3065. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3066. end;
  3067. proc_add_definition:=forwardfound;
  3068. end;
  3069. end.