summary refs log tree commit diff stats
path: root/compiler/semexprs.nim
Commit message (Expand)AuthorAgeFilesLines
...
* introduce precise string '[]', '[]=' accessors; fixes #8049 (#8817)Andreas Rumpf2018-08-301-2/+2
* enforce the condition of a 'when' condition to be of type bool; refs #8603Andreas Rumpf2018-08-231-1/+1
* fixes merge conflictAraq2018-08-231-0/+5
|\
| * Don't consider tyAnd/tyNot/tyOr/tyAnything as generic (#8700)LemonBoy2018-08-221-0/+5
* | some progress on destructors for builtin seqsAndreas Rumpf2018-08-201-1/+4
|/
* fixes #8425Araq2018-08-041-1/+1
* keep the imports in runnableExamples visible in generated html doc (#8354)skilchen2018-07-191-2/+3
* fix #7405 and #8195 (#8198)Timothee Cour2018-07-161-0/+3
* enable destructors without the --newruntime switchAndreas Rumpf2018-07-041-6/+1
* Minor changes to discardable handling (#8155)LemonBoy2018-07-011-13/+0
* fix #8129 by calling `semExpr` in `semIs` if node is `strLit` (#8130)Vindaar2018-06-291-0/+2
* Merge branch 'devel' into typedesc-reformsAndreas Rumpf2018-06-261-2/+2
|\
| * More concept fixesLemonBoy2018-06-241-2/+2
* | requested pull-request changesZahary Karadjov2018-06-161-0/+1
* | fix some breakage after rebasingZahary Karadjov2018-06-161-1/+1
* | fixes #7222; fixes #5595; fixes #3747Zahary Karadjov2018-06-161-0/+30
* | Implement the `is` operator for the new static and typedesc type classesZahary Karadjov2018-06-161-20/+61
* | A minimal patch enabling the new typedesc and static types syntaxZahary Karadjov2018-06-161-6/+33
|/
* implements a --nep1:on switch to make the compiler enforce the naming convent...Andreas Rumpf2018-06-131-1/+1
* Fixes #6803Yuriy Glukhov2018-06-111-0/+3
* fixes yet another merge conflictAraq2018-06-111-2/+0
|\
| * Merge pull request #8009 from yglukhov/remove-oldIterTransfAndreas Rumpf2018-06-111-2/+0
| |\
| | * Removed oldIterTranf featureYuriy Glukhov2018-06-101-2/+0
* | | fixes merge conflictAraq2018-06-111-4/+2
|\| |
| * | fix #7997Vindaar2018-06-101-4/+2
| |/
* | fixex merge conflictsAraq2018-06-081-1/+1
|\|
| * Merge branch 'devel' into yield-in-tryYuriy Glukhov2018-05-151-231/+238
| |\
| * | Yield in tryYuriy Glukhov2018-05-091-1/+1
* | | incremental compilation: implemented basic replay logicAndreas Rumpf2018-06-021-3/+3
* | | refactoring: remove idents.legacy global variable and pass the IdentCache aro...Andreas Rumpf2018-05-271-18/+18
* | | remove more global variables in the Nim compilerAndreas Rumpf2018-05-271-10/+10
* | | make semantic checking free of globalsAndreas Rumpf2018-05-181-2/+2
* | | platform.nim doesn't use globals anymore; prepare msgs.nim for not using globalsAndreas Rumpf2018-05-181-7/+7
* | | preparations of making compiler/msgs.nim free of global variablesAndreas Rumpf2018-05-171-1/+1
* | | remove ast.emptyNode global; cleanup configuration.nimAraq2018-05-161-13/+16
| |/ |/|
* | fixes testament compilationAraq2018-05-141-5/+28
|\|
| * implement the export/except statementZahary Karadjov2018-05-071-2/+21
| * A motivating example for the new `bindSym` behavior.Zahary Karadjov2018-05-071-0/+2
| * Better support for treating templates and macros as symbols.Zahary Karadjov2018-05-071-3/+5
* | options.nim: no global variables anymoreAndreas Rumpf2018-05-131-6/+6
* | make nimsuggest compile againAndreas Rumpf2018-05-131-1/+1
* | sem pass compiles againAndreas Rumpf2018-05-121-144/+151
* | semstmts compiles againAndreas Rumpf2018-05-121-3/+3
* | more modules compile againAndreas Rumpf2018-05-121-107/+107
|/
* compiler refactoring, pass config around explicitlyAndreas Rumpf2018-05-051-3/+3
* minor speedup: concept tests still greenAndreas Rumpf2018-04-281-1/+2
* stuff that is greenAndreas Rumpf2018-04-281-1/+2
* semcheck: code cleanupsAndreas Rumpf2018-04-281-5/+1
* .experimental can now be used to enable specific featuresAndreas Rumpf2018-04-241-3/+3
* new 'var T' error messages with hyperlinks; closes #7373Andreas Rumpf2018-04-221-4/+4
           



          
         


























                                                                                
//
//
//           The Nimrod Compiler
//        (c) Copyright 2008 Andreas Rumpf
//
//    See the file "copying.txt", included in this
//    distribution, for details about the copyright.
//
unit nhashes;

{$include 'config.inc'}

interface

uses
  charsets, nsystem, strutils;

const
  SmallestSize = (1 shl 3) - 1;
  DefaultSize = (1 shl 11) - 1;
  BiggestSize = (1 shl 28) - 1;

type
  THash = type int;
  PHash = ^THash;
  THashFunc = function (str: PChar): THash;

function GetHash(str: PChar): THash;
function GetHashCI(str: PChar): THash;

function GetDataHash(Data: Pointer; Size: int): THash;

function hashPtr(p: Pointer): THash;

function GetHashStr(const s: string): THash;
function GetHashStrCI(const s: string): THash;

function getNormalizedHash(const s: string): THash;

//function nextPowerOfTwo(x: int): int;

function concHash(h: THash; val: int): THash;
function finishHash(h: THash): THash;

implementation

{@ignore}
{$ifopt Q+} { we need Q- here! }
  {$define Q_on}
  {$Q-}
{$endif}

{$ifopt R+}
  {$define R_on}
  {$R-}
{$endif}
{@emit}

function nextPowerOfTwo(x: int): int;
begin
  result := x -{%} 1;
  // complicated, to make it a nop if sizeof(int) == 4,
  // because shifting more than 31 bits is undefined in C
  result := result or (result shr ((sizeof(int)-4)* 8));
  result := result or (result shr 16);
  result := result or (result shr 8);
  result := result or (result shr 4);
  result := result or (result shr 2);
  result := result or (result shr 1);
  Inc(result)
end;

function concHash(h: THash; val: int): THash;
begin
  result := h +{%} val;
  result := result +{%} result shl 10;
  result := result xor (result shr 6);
end;

function finishHash(h: THash): THash;
begin
  result := h +{%} h shl 3;
  result := result xor (result shr 11);
  result := result +{%} result shl 15;
end;

function GetDataHash(Data: Pointer; Size: int): THash;
var
  h: THash;
  p: PChar;
  i, s: int;
begin
  h := 0;
  p := {@cast}pchar(Data);
  i := 0;
  s := size;
  while s > 0 do begin
    h := h +{%} ord(p[i]);
    h := h +{%} h shl 10;
    h := h xor (h shr 6);
    Inc(i); Dec(s)
  end;
  h := h +{%} h shl 3;
  h := h xor (h shr 11);
  h := h +{%} h shl 15;
  result := THash(h)
end;

function hashPtr(p: Pointer): THash;
begin
  result := ({@cast}THash(p)) shr 3; // skip the alignment
end;

function GetHash(str: PChar): THash;
var
  h: THash;
  i: int;
begin
  h := 0;
  i := 0;
  while str[i] <> #0 do begin
    h := h +{%} ord(str[i]);
    h := h +{%} h shl 10;
    h := h xor (h shr 6);
    Inc(i)
  end;
  h := h +{%} h shl 3;
  h := h xor (h shr 11);
  h := h +{%} h shl 15;
  result := THash(h)
end;

function GetHashStr(const s: string): THash;
var
  h: THash;
  i: int;
begin
  h := 0;
  for i := 1 to Length(s) do begin
    h := h +{%} ord(s[i]);
    h := h +{%} h shl 10;
    h := h xor (h shr 6);
  end;
  h := h +{%} h shl 3;
  h := h xor (h shr 11);
  h := h +{%} h shl 15;
  result := THash(h)
end;

function getNormalizedHash(const s: string): THash;
var
  h: THash;
  c: Char;
  i: int;
begin
  h := 0;
  for i := strStart to length(s)+strStart-1 do begin
    c := s[i];
    if c = '_' then continue; // skip _
    if c in ['A'..'Z'] then c := chr(ord(c) + (ord('a')-ord('A'))); // toLower()
    h := h +{%} ord(c);
    h := h +{%} h shl 10;
    h := h xor (h shr 6);
  end;
  h := h +{%} h shl 3;
  h := h xor (h shr 11);
  h := h +{%} h shl 15;
  result := THash(h)
end;

function GetHashStrCI(const s: string): THash;
var
  h: THash;
  c: Char;
  i: int;
begin
  h := 0;
  for i := strStart to length(s)+strStart-1 do begin
    c := s[i];
    if c in ['A'..'Z'] then c := chr(ord(c) + (ord('a')-ord('A'))); // toLower()
    h := h +{%} ord(c);
    h := h +{%} h shl 10;
    h := h xor (h shr 6);
  end;
  h := h +{%} h shl 3;
  h := h xor (h shr 11);
  h := h +{%} h shl 15;
  result := THash(h)
end;

function GetHashCI(str: PChar): THash;
var
  h: THash;
  c: Char;
  i: int;
begin
  h := 0;
  i := 0;
  while str[i] <> #0 do begin
    c := str[i];
    if c in ['A'..'Z'] then c := chr(ord(c) + (ord('a')-ord('A'))); // toLower()
    h := h +{%} ord(c);
    h := h +{%} h shl 10;
    h := h xor (h shr 6);
    Inc(i)
  end;
  h := h +{%} h shl 3;
  h := h xor (h shr 11);
  h := h +{%} h shl 15;
  result := THash(h)
end;

{@ignore}
{$ifdef Q_on}
  {$undef Q_on}
  {$Q+}
{$endif}

{$ifdef R_on}
  {$undef R_on}
  {$R+}
{$endif}
{@emit}

end.